为什么我的VBA代码总是跳过我的第一个循环?

时间:2018-12-09 19:40:13

标签: excel vba excel-vba loops

我正在尝试使我的VBA代码遍历所有单元格,并在第二张工作表中每遇到一个特定文本时将我第二张表中已有数字的计数减少1。但是,每当我分步运行循环时,似乎都将跳过下一个j,并且每次都直接进入下一个i,即下一行。我的代码是否存在某种格式错误?

rm  ~/.local/lib/python3.5/site-packages/matplotlib* -rf

1 个答案:

答案 0 :(得分:0)

尝试一下:-

    Option Explicit

        Sub DeleteSAC()

        Dim count As Integer
        Dim i As Integer
        Dim j As Integer
        Dim ws1 As Worksheet
        Dim ws2 As Worksheet
        Dim lastRow As Long
        Dim lastcolumn As Long

        Set ws1 = Worksheets("Sheet1")
        Set ws2 = Worksheets("Sheet2")


        lastRow = ws1.Cells(Rows.count, "B").End(xlUp).Row

        lastcolumn = ws1.Cells(1, Columns.count).End(xlToLeft).Column

        count = ws2.Cells(1, 7).Value
        i = 2
        j = lastcolumn


        For i = 2 To lastRow
        For j = lastcolumn To 1 Step -1


        If ws1.Cells(i, j) = "SAC" Then
            count = count - 1
Exit for
            End If


        Next j
        Next i

        ws2.Cells(1, 7) = count


        End Sub