【问题标题】:Excel VB For loop is not iterating through if statementExcel VB For循环没有遍历if语句
【发布时间】:2011-09-05 13:58:07
【问题描述】:

我很欣赏这是一个业余问题,但我不习惯 VB 及其语法。

我正在尝试根据数量 (QTY) 列中是否存在值,将信息从一个工作表 (ProductList) 拉到另一个工作表 (Quote)。

这是我的方法:

Private Sub cmdProductListContinue_Click()

    'Declare variabless
    Dim i, duration, qty, outputX, outputY

    'Set initial values
    duration = 120 'Used to determine number of iterations in for loop (no. of QTY cells we are to check)
    i = 3 'Used as cell co-ordinates to pull information from
    outputX = 17 'Used as cell co-ordinates to output information

    'Populate invoice with product info by iterating through all QTY cells and pulling across info if needed
    For i = 3 To duration
        'Reset quantity to zero each time
        qty = 0
        'Set quantity to the value in the QTY cell
        Set qty = Worksheets("ProductList").Cells(i, 3)
            'If there is a quantity value present
            If qty > 0 Then
                'Insert quantity value into appropriate cell in quote sheet
                Worksheets("Quote").Cells(outputX, 2) = qty
                'Insert description into quote sheet
                Worksheets("Quote").Cells(outputX, 3) = Worksheets("ProductList").Cells(i, 2)
                'Insert unit price into quote sheet
                Worksheets("Quote").Cells(outputX, 4) = Worksheets("ProductList").Cells(i, 4)
                'Increment the output co-ordinates to the next line
                outputX = outputX + 1
            End If
    Next i

    'Open quote sheet
    Sheets("Quote").Select

End Sub

使用断点我可以看到,当有一个数量值时,它成功地移动到第一个“Then”语句,但似乎只是返回到循环的开头,完全错过了其他两个输出行。

我的语法正确吗?我的逻辑是否遗漏了什么?

我很感激如果没有表格来查看数据列等,可能很难考虑清楚。

'i' 设置为 3,因为 Quantity 列的第一个值位于单元格 C,3 中,描述在 C,2 中,价格在 C,4 中。然后这些值通过循环递增。

对此的任何帮助将不胜感激。

谢谢!!

【问题讨论】:

  • outputX = outputX + 1 似乎执行了吗?我问是因为您只提到“完全错过了其他两条输出线”。其中不包括 outputX 的增量
  • 是的,它也错过了 outputX 的增量。它永远不会过去: Worksheets("Quote").Cells(outputX, 2) = qty
  • 我已更新代码以解决 bpgergo 关于我的增量问题的观点。
  • 如果你想为单元格赋值,不要依赖默认属性,而是用qty = CInt(Worksheets("ProductList").Cells(i, 3).value)显示它

标签: excel vba if-statement for-loop


【解决方案1】:

在这里您将 qty 分配给 对象(范围):

Set qty = Worksheets("ProductList").Cells(i, 3)

如果您想获取单元格,请使用:

qty = Worksheets("ProductList").Cells(i, 3).Value

“Set”在分配对象时使用,所以这里不需要它。 “值”是默认属性,但我更喜欢包含它。

对您的代码稍作修改:

Private Sub cmdProductListContinue_Click()

Dim i, duration, qty, outputX
Dim wsQuote As Worksheet, wsProd As Worksheet

    Set wsQuote = Worksheets("Quote")
    Set wsProd = Worksheets("ProductList")

    duration = 120
    outputX = 17

    For i = 3 To duration
        qty = wsProd.Cells(i, 3).Value
        If qty > 0 Then
            With wsQuote.Rows(outputX)
                .Cells(2).Value = qty
                .Cells(3).Value = wsProd.Cells(i, 2).Value
                .Cells(4).Value = wsProd.Cells(i, 4).Value
                outputX = outputX + 1
            End With
        End If
    Next i

    wsQuote.Activate

End Sub

【讨论】:

    【解决方案2】:
    Set qty = Worksheets("ProductList").Cells(i, i)
    

    这个!将遍历工作表的对角线,就像 C3、D4、E5等

    你可能想要类似的东西

    Set qty = Worksheets("ProductList").Cells(i, 3)
    

    Set qty = Worksheets("ProductList").Cells(3, i)
    

    还要检查对 ProductList 表的其他引用(行尾):

    'Insert description into quote sheet
    Worksheets("Quote").Cells(outputX, outputY + 1) = Worksheets("ProductList").Cells(i, i - 1)
    'Insert unit price into quote sheet
    Worksheets("Quote").Cells(outputX, outputY + 2) = Worksheets("ProductList").Cells(i, i + 1)
    

    【讨论】:

    • 但是,原来的问题仍然存在,断点显示它永远不会超过行:Worksheets("Quote").Cells(outputX, outputY) = qty
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    • 1970-01-01
    • 2020-05-02
    • 2014-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多