【问题标题】:For...Next loop breaks when using Not() operatorFor...Next 循环在使用 Not() 运算符时中断
【发布时间】:2015-08-22 03:14:39
【问题描述】:

我正在运行一个 for...next 循环,检查数据集中的条目是否满足特定条件(在本例中为 IsNA)。但是,更改此循环中的 if-then-else 条件以检查是否不满足条件似乎会破坏 for/next 循环。即使 sub 的元素没有改变,我也会收到 Next without For 错误。

当那部分代码没有改变时,我不知道为什么它认为 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

--编辑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
    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

【问题讨论】:

  • @Matteo NNZ 发现了这个问题 (+1);此外,在尝试查找所有包含文本的单元格时,您可能会发现这很有用:并将所有数据复制到 mdsht.Range 偏移 -856

标签: vba excel for-loop next


【解决方案1】:

您需要关闭第二个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

【讨论】:

  • 传奇!这非常有效。最后一个快速的问题,这个脚本非常慢 - 我想知道它是否因为我已经将一堆变量声明为工作表。是否有另一种更快的方法来执行此操作,或者是否有其他明显使该脚本执行缓慢的其他方法?
  • 查看在循环前使用Application.ScreenUpdating并设置Application.Calculation = xlCalculationManual,然后在循环后返回Application.Calculation = xlCalculationAutomatic
猜你喜欢
  • 2021-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-27
  • 2017-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多