【发布时间】:2017-08-28 20:58:59
【问题描述】:
我有一个相当复杂的For 循环,里面有一个If 语句和另一个For 循环。并在第二个 For 循环中给出特定标准(即If InStr(1, q.Value, "Total")),我想结束整个If 语句并移至Next C。
我知道这就像洋葱一样分层,可能没有简单的出路。
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
【问题讨论】:
-
If语句不能那样工作。您的If..Then End If行实际上只是在说If...Then : Do Nothing : End If,因为End If只是If块上的最后一行。同样,您的行If...Then _是有问题的,因为它们是行继续标记。不要那样使用它们。它使您的代码难以阅读和调试。继续下一个 C 的唯一方法是将您不想运行的代码包装在 ifIf Not Instr() Then的反向中,然后,由于条件不满足,下一个 C 将运行。 -
@BrandonBarney 感谢您的帮助。我真的不确定 If 语句是如何工作的,现在它更有意义了。