【问题标题】:Do While loop not incrementing properly in VBADo While 循环在 VBA 中不能正确递增
【发布时间】:2020-02-27 14:50:17
【问题描述】:

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

    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 工作表将崩溃。感谢您的帮助!

【问题讨论】:

  • C = C + 1 And Sum = Sum + 1 And Counter = Counter + 1 不是这样的...删除And 并将每个放在自己的行中。
  • Worksheets("Values").Cells(Counter, 54).Value &lt;&gt; "A" Or "B" Or "C" 并没有按照你的想法去做。
  • 这里不需要循环 - 只需使用 Application.CountIf 来获取 A、B、C 的计数。

标签: vba loops do-while


【解决方案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 &lt;&gt; "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 公式?

【讨论】:

  • 另一个想法:Select Case 在这里会很棒。
  • 我的最后一点逻辑可能是错误的 - 请聪明人检查一下!
  • 或同意@BigBen - Select Case 更容易掌握。
  • @BigBen - 同意,建议重写。
  • 这似乎有效,但我的计算列中有错误需要修复。非常感谢大家的帮助!
【解决方案2】:

它必须是一个 Do While 循环吗?您可以使用特殊的单元格函数来获取包含数据的最后一行,然后执行有限的 for 循环。

Dim LastDataRow As 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

下一个

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-09
    • 1970-01-01
    • 2023-03-20
    相关资源
    最近更新 更多