2017-08-28 39 views
0

我有一个相当复杂的For循环与If声明里面和另一个For循环里面。并给予一定的标准(即If InStr(1, q.Value, "Total"))在第二秒内我想结束整个If声明并转移到Next C结束如果/下一个C语句与For循环/ VBA

我明白,这是像洋葱一样分层,可能不是一个简单的出路。

For Each C In copyRng 

     If IsNumeric(C) And C.Value <> "0" And Len(C) <> 0 And C.Value <> "2017" Then 

      Set rowRange = xSheet.Range(C, C.EntireColumn.Cells(1)) 'set range from cell up to the top cell of the comment/ Fix the 2017 thing 

      For Each q In rowRange 'Loop through that range and find the Account number just above it and set it as rowSrc 
       If InStr(1, q.Value, "C-") And Not ISIN(C, uniqueVal) Then Set rowSrc = q 
       If InStr(1, q.Value, "Total") Then End If 'At this point I want to leave the entire If Statement and move on to the next C 
      Next q 


      Set colSrc = C.EntireRow.Offset(0).Cells(1) 'find alert connected with the number 
      numCol = DestSh.Cells.Find(colSrc.Value, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Column 'look for the column in which the same alert is listed 
      numRow = DestSh.Cells.Find(rowSrc.Value, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Row 'look for row in which the same account is listed 

      'Set destination 
      Set destRng = DestSh.Cells(numRow, numCol) 

      'Copy to destination Range 
      C.Copy destRng 

     End If 

    Next C 
+1

'If'语句不工作这样。 If'Then..Then End If'实际上只是说'If ... Then:Do Nothing:End If',因为'End If'只是'If'块的最后一行。同样,你的行'If ... Then _'也是有问题的,因为那些是行连续标记。不要像那样使用它们。它使你的代码难以阅读和调试。继续下一个C的唯一方法是将你不想运行的代码打包到if'If Not Instr()Then'的后面,然后,由于条件不满足,下一个C将运行。 –

+0

@BrandonBarney感谢您的帮助。我真的不确定If语句是如何工作的,现在它变得更有意义。 – ShieldData

回答

1

你需要Exit For,然后在循环后,把剩下的代码在另一个If块:

For Each C In copyRng 
    If IsNumeric(C) And C.Value <> "0" And Len(C) <> 0 And C.Value <> "2017" Then 
     Set rowRange = xSheet.Range(C, C.EntireColumn.Cells(1)) 

     For Each q In rowRange 
      If InStr(1, q.Value, "C-") And Not ISIN(C, uniqueVal) Then Set rowSrc = q 
      If InStr(1, q.Value, "Total") Then Exit For ' Exit loop prematurely 
     Next q 
     If q is Nothing Then ' Skip if loop was exited prematurely 
      Set colSrc = C.EntireRow.Offset(0).Cells(1) 
      numCol = DestSh.Cells.Find(colSrc.Value, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Column 
      numRow = DestSh.Cells.Find(rowSrc.Value, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Row 

      Set destRng = DestSh.Cells(numRow, numCol) 
      C.Copy destRng 
     End If 
    End If 
Next C 
+0

感谢您的帮助。我意识到If语句与我所想象的不同。我决定尝试一下你的代码,并收到错误信息“Object variable or With-Variable not identified”。有没有一个快速解决这个问题,或者这可能是我的其他代码更大的问题。如果'下一个q'线可能下移? – ShieldData

+0

你在哪里定义了'q',并在哪一行发现错误?注意:不,下一个q不应该下移。 – trincot

+0

如果InStr(1,q.Value,“Total”)= 0,我得到这一行上的错误然后'跳过,如果循环过早退出',并且我在代码开始处定义了q作为范围。 – ShieldData

0

删除Then后的每个下划线并重写整个代码。逻辑被改变。你几乎没有按照你认为的方式去其他地方。

检查: VBA - How the colon `:` works in VBA code with condition

一般情况下,您的代码不工作,你认为它的工作方式。检查以下内容:

Public Sub TestMe() 

    If 1 = 2 Then _ 
     Debug.Print "I am true - 1=2" 
     Debug.Print "I should be also true" 

    If 2 = 3 Then _ 
     Debug.Print "I am true 2=3" 

End Sub 
+1

非常感谢。我需要停止使用'Then _'来确保代码更简洁。 – ShieldData

+0

@FSchildorfer - 作为一个2.步骤尝试把'Option Explicit'放在代码的顶部。就像'如果InStr(1,q.Value,“Total”)Then End If'不应该被允许编译。 – Vityata

+0

我绝对会。 – ShieldData