2011-05-31 34 views
1

我很欣賞這是一個業餘問題,但我不習慣VB和它的語法。Excel VB for循環沒有遍歷if語句

我試圖根據數量(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 

使用斷點,我可以看到,當有它成功地轉移到第一「然後」語句數量值,但隨後似乎剛剛返回到循環的開始,完全丟失了另外兩條輸出線。

我的語法正確嗎?我在邏輯中錯過了什麼嗎?

我明白它可能很難通過認爲這無需片材看到的數據列等

「I」被設置爲3的數量列第一個值是在小區C中,3與C,2中的描述和C,4中的價格。這些通過循環遞增。

任何幫助,這將不勝感激。

謝謝!

+1

是否'outputX = outputX + 1'出現執行?我問,因爲你只提到「完全錯過了其他兩條輸出線」。其中不包括輸出增量X – 2011-05-31 15:05:21

+0

是的,它也漏掉了輸出X的增量。它永遠不會前進: 工作表(「Quote」)。單元格(outputX,2)=數量 – Frank 2011-05-31 15:34:56

+0

我已更新代碼以獲取地址bpgergo關於增量問題的觀點。 – Frank 2011-05-31 15:37:16

回答

4

在這裏,你是assigni NG數量到對象(範圍):

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

如果你不是想要得到的細胞然後使用:

qty = Worksheets("ProductList").Cells(i, 3).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 
2
Set qty = Worksheets("ProductList").Cells(i, i) 

This!將遍歷對角線片的,就是喜歡 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) 
+0

'顯示它啊謝謝!我不敢相信我沒有看到! – Frank 2011-05-31 15:31:54

+0

但是,原始問題仍然存在,斷點顯示它永遠不會超過該行: 工作表(「Quote」)。Cells(outputX,outputY)= qty – Frank 2011-05-31 15:32:37