Do While循环在VBA中无法正确递增

时间:2020-02-27 14:50:17

标签: vba loops do-while

我编写了一个小程序来扫描电子表格,并根据列中的值计算不同项目类型的总数,然后将结果打印到特定的列位置。我的代码在下面

    Sub How_Many_items()

'define variables. A, B, C refer to letter coding. O is other. counter is for the count
    Dim A As Integer
    Dim B As Integer
    Dim C As Integer
    Dim O As Integer
    Dim Sum As Integer
    Dim Counter As Integer

'resetting variables
    A = 0
    B = 0
    C = 0
    O = 0
    Sum = 0
    Counter = 2
    'do while loop to continue going down rows until out of new data
    'if loops to accumulate values
    Worksheets("Values").Select

Do While Worksheets("Values").Cells(Counter, 54) <> Empty
    If Worksheets("Values").Cells(Counter, 54).Value = "A" Then
    A = A + 1 And Sum = Sum + 1 And Counter = Counter + 1

    ElseIf Worksheets("Values").Cells(Counter, 54).Value = "B" Then
    B = B + 1 And Sum = Sum + 1 And Counter = Counter + 1

    ElseIf Worksheets("Values").Cells(Counter, 54).Value = "C" Then
    C = C + 1 And Sum = Sum + 1 And Counter = Counter + 1

    ElseIf Worksheets("Values").Cells(Counter, 54).Value <> "A" Or "B" Or "C" Then
    Sum = Sum + 1 And Counter = Counter + 1
    End If

Loop

'print values in PivotTables worksheet

Worksheets(PivotTables).Cells(I, 20) = Sum
Worksheets(PivotTables).Cells(I, 21) = A
Worksheets(PivotTables).Cells(I, 22) = B
Worksheets(PivotTables).Cells(I, 23) = C


  End Sub

基于查看此站点上的其他信息,我实际上不需要worksheets()。select操作,但是我认为这不会使程序停顿。

单步执行程序时,它将停滞在循环中;当前正在使用的数据在感兴趣的第一列中带有“ B”,但它从未增加到下一行,这使我认为由于某种原因它实际上并没有增加计数或B。我正在扫描的列也是使用VLOOKUP的计算列,但是我不知道这是否会影响我的工作。如果我尝试运行该程序,Excel工作表将崩溃。感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

更多的是评论,但不能这么简单地描述,但是您需要将操作分成几行,即

If Worksheets("Values").Cells(Counter, 54).Value = "A" Then
    A = A + 1
    Sum = Sum + 1
    Counter = Counter + 1

ElseIf Worksheets("Values").Cells(Counter, 54).Value <> "A" Or "B" Or "C" Then

需要成为

ElseIf NOT(Worksheets("Values").Cells(Counter, 54).Value = "A" Or Worksheets("Values").Cells(Counter, 54).Value ="B" Or Worksheets("Values").Cells(Counter, 54).Value = "C") Then

您可以使用With子句或变量来缩短代码。

实际上,您的代码可以简化为(我认为)为

Sub How_Many_items()

'define variables. A, B, C refer to letter coding. O is other. counter is for the count
Dim A As Long
Dim B As Long
Dim C As Long
Dim O As Long
Dim Sum As Long
Dim Counter As Long

Counter = 2
'do while loop to continue going down rows until out of new data
'if loops to accumulate values
With Worksheets("Values")
    Do While .Cells(Counter, 54) <> Empty
        If .Cells(Counter, 54).Value = "A" Then
            A = A + 1
        ElseIf .Cells(Counter, 54).Value = "B" Then
            B = B + 1
        ElseIf .Cells(Counter, 54).Value = "C" Then
            C = C + 1
        End If
        sum = Sum + 1
        Counter = Counter + 1
    Loop
End With

为什么不使用COUNTIF公式?

答案 1 :(得分:0)

是否必须是“ Do While循环”?您可以使用特殊的单元格功能获取包含数据的最后一行,并进行有限的for循环。

将LastDataRow设置为Long'变量,以存储行号

LastDataRow = Cells.SpecialCells(xlCellTypeLastCell).Row

对于i = 1到LastDataRow

If Worksheets("Values").Cells(Counter, 54).Value = "A" Then
A = A + 1 And Sum = Sum + 1 And Counter = Counter + 1

ElseIf Worksheets("Values").Cells(Counter, 54).Value = "B" Then
B = B + 1 And Sum = Sum + 1 And Counter = Counter + 1

ElseIf Worksheets("Values").Cells(Counter, 54).Value = "C" Then
C = C + 1 And Sum = Sum + 1 And Counter = Counter + 1

ElseIf Worksheets("Values").Cells(Counter, 54).Value <> "A" Or "B" Or "C" Then
Sum = Sum + 1 And Counter = Counter + 1
End If

下一个我