For …使用Not()运算符时,下一个循环会中断

我正在运行for … next循环来检查数据集中的条目是否满足特定条件(在本例中为IsNA)。 然而,改变这个循环中的if-then-else条件来检查一个条件是否被满足,似乎会中断for / next循环。 即使sub的元素没有改变,我也会收到Next for For error。

我为什么认为在for循环中没有下一个代码没有改变的时候我迷失了方向。

– 原创工作守则 –

Option Explicit Dim i As Double Dim a As Range Public ssht As Worksheet Public susht As Worksheet Public mdsht As Worksheet Public LastRow As Long Dim testcell As Long Public Sub MissingDataSetCopy() 'Part Bii 'Find rows with NA error Application.ScreenUpdating = False Dim i, j As Integer j = 4 'Finds current range on Summary worksheet Set ssht = ThisWorkbook.Worksheets("sandbox") Set mdsht = ThisWorkbook.Worksheets("MissingData") Set susht = ThisWorkbook.Worksheets("summary") 'Copies data to sandbox sheet as values susht.UsedRange.copy ssht.Range("A1").PasteSpecial (xlPasteValues) LastRow = ssht.Range("A4").CurrentRegion.Rows.Count Dim testcell As Double Dim numchk As Boolean 'For...Next look call ISNUMBER test For i = 860 To 874 If Application.WorksheetFunction.IsNA(ssht.Range("B" & i)) Then mdsht.Range("B" & j & ":H" & j).Value = ssht.Range("A" & i - 1 & ":G" & i - 1).Value j = j + 1 mdsht.Range("B" & j & ":H" & j).Value = ssht.Range("A" & i & ":G" & i).Value j = j + 1 mdsht.Range("B" & j & ":H" & j).Value = ssht.Range("A" & i + 1 & ":G" & i + 1).Value j = j + 1 End If Next i Dim fnd As Variant Dim rplc As Variant fnd = "#N/A" rplc = "=NA()" mdsht.Cells.Replace what:=fnd, Replacement:=rplc, _ LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _ SearchFormat:=False, ReplaceFormat:=False End Sub 

– 如果陈述 –

 For i = 860 To 874 If Application.WorksheetFunction.IsNA(ssht.Range("B" & i)) And Application.WorksheetFunction.IsNA(ssht.Range("B" & i - 1)) Then mdsht.Range("B" & j & ":H" & j).Value = ssht.Range("A" & i & ":G" & i).Value j = j + 1 If Application.WorksheetFunction.IsNA(ssht.Range("B" & i)) And Not (Application.WorksheetFunction.IsNA(ssht.Range("B" & i - 1))) Then mdsht.Range("B" & j & ":H" & j).Value = ssht.Range("A" & i - 1 & ":G" & i - 1).Value j = j + 1 mdsht.Range("B" & j & ":H" & j).Value = ssht.Range("A" & i & ":G" & i).Value j = j + 1 End If Next i 

你需要closures第二个If块:

 For i = 860 To 874 If Application.WorksheetFunction.IsNA(ssht.Range("B" & i)) And Application.WorksheetFunction.IsNA(ssht.Range("B" & i - 1)) Then mdsht.Range("B" & j & ":H" & j).Value = ssht.Range("A" & i & ":G" & i).Value j = j + 1 End If '<-- it was not closed If Application.WorksheetFunction.IsNA(ssht.Range("B" & i)) And Not (Application.WorksheetFunction.IsNA(ssht.Range("B" & i - 1))) Then mdsht.Range("B" & j & ":H" & j).Value = ssht.Range("A" & i - 1 & ":G" & i - 1).Value j = j + 1 mdsht.Range("B" & j & ":H" & j).Value = ssht.Range("A" & i & ":G" & i).Value j = j + 1 End If Next i 

或者使用ElseIf关键字,如果这两个条件(似乎)互不包括:

 For i = 860 To 874 If Application.WorksheetFunction.IsNA(ssht.Range("B" & i)) And Application.WorksheetFunction.IsNA(ssht.Range("B" & i - 1)) Then mdsht.Range("B" & j & ":H" & j).Value = ssht.Range("A" & i & ":G" & i).Value j = j + 1 ElseIf Application.WorksheetFunction.IsNA(ssht.Range("B" & i)) And Not (Application.WorksheetFunction.IsNA(ssht.Range("B" & i - 1))) Then mdsht.Range("B" & j & ":H" & j).Value = ssht.Range("A" & i - 1 & ":G" & i - 1).Value j = j + 1 mdsht.Range("B" & j & ":H" & j).Value = ssht.Range("A" & i & ":G" & i).Value j = j + 1 End If Next i 
Interesting Posts