vba - Excel VBA - 循环变量

标签 vba excel for-loop

我似乎无法让我的第二个 for 循环正确。我正在寻找值为“Persoonlijke prijslijst”的单元格。一旦我有了这个单元格,我需要上升两个并删除 8 个。当我调试时,它说 temp = 0 所以我认为它在我的第二个 for 循环中。

Dim i As Integer
For i = 1 To 800
    Range("C" & i).Select
    If Range("C" & i).Value = "Persoonlijke prijslijst" Then
        Dim temp As Integer
        For temp = i - 2 To temp + 8
            Range("C" & temp).EntireRow.Delete Shift:=xlToLeft
        Next temp
    End If
Next i

最佳答案

另一种不循环 800 次的方法:

 Sub testing()

Dim rng As Range
Dim fAddress As String
Dim rngRows As Range

With Sheet1.Range("C1:C800")
    Set rng = .Find("Persoonlijke prijslijst")
    If Not rng Is Nothing Then
        fAddress = rng.Address
        Do
        If rngRows Is Nothing Then
            Set rngRows = rows(rng.Row - 2 & ":" & rng.Row + 5)
        Else
            Set rngRows = Union(rngRows, rows(rng.Row - 2 & ":" & rng.Row + 5))
        End If
           Set rng = .FindNext(rng)
        Loop While Not rng Is Nothing And rng.Address <> fAddress
    End If
End With

rngRows.EntireRow.Delete

End Sub

关于vba - Excel VBA - 循环变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9704425/

相关文章:

Java迭代项目问题

python - 如何从文件夹内的 n 个 csv 文件导入数据?

python - 使用 XLWINGS 刷新数据透视表

vba - 按字符比较(差异)两个单元格中的字符串

excel - 查找最大值索引匹配匹配

c - C 中的 VBA 和 dll : Strategy for more responsive and controllable Excel performance?

xml - 使用 XSLT 从 XML 中写入数据列与 Excel 中的行

c# - Excel 工作表对象不等于同一个工作表对象

vba - ByRef 参数类型不匹配 - Excel VBA

python - 根据另一个 Dataframe for 循环的条件创建一个新的 Dataframe