2015-06-07 54 views
0

我正在運行for ... next循環,用於檢查數據集中的條目是否滿足特定條件(在本例中爲IsNA)。然而,改變這個循環中的if-then-else條件來檢查一個條件是否被滿足似乎會中斷for/next循環。即使該子元素沒有更改,我也會收到Next for For error。For ...使用Not()運算符時的下一個循環中斷

我迷失了它爲什麼認爲當代碼的那部分沒有改變時for循環中沒有下一個。

--Original工作代碼 -

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 

- 編輯。如果Statements--

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 
+0

@Matteo NNZ發現了問題(+1);當您嘗試查找所有包含文本的單元格時,您可能會發現這很有用:ssht.Range(「B860:B874」)。SpecialCells(xlCellTypeConstants,xlTextValues)。選擇,而不是增加找到的範圍,並將所有數據複製到mdsht.Range偏移量-856 –

回答

2

您需要關閉第二塊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關鍵字,如果兩個條件(似乎)都排除其他條件r:

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 
+0

圖例!這工作完美。最後一個簡短的問題是,這個腳本非常慢 - 我想知道它是否因爲我宣佈了一堆變量作爲工作表。有沒有另外一種方法可以做得更快,或者有其他一些明顯的讓腳本執行速度慢的方法? –

+0

在循環之前使用'Application.ScreenUpdating'並設置'Application.Calculation = xlCalculationManual',然後在循環之後返回到'Application.Calculation = xlCalculationAutomatic'。 – ChipsLetten