Excel VBA For循环不迭代if语句

时间:2011-05-31 14:59:33

标签: excel vba if-statement for-loop

我很欣赏这是一个业余的问题,但我不熟悉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。然后通过循环递增它们。

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

谢谢!

2 个答案:

答案 0 :(得分:4)

您可以在此处将数量分配给对象(范围):

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

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

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

分配对象时使用“Set”,因此您不需要它。 “Value”是默认属性,但无论如何我更愿意包含它。

稍微改写一下代码:

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

答案 1 :(得分: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)