2013-02-14 238 views
0

有人可以解釋如何讓這個宏在整列上運行,而不是單行單元格嗎?即檢查每個單獨單元的值並且在其行上的相應單元中執行所需的計算和輸出。將Excel VBA單行宏應用於單元格範圍

Sub Calculate_Costs() 
If Cells(2, 7) = "One Man" And Cells(2, 6) >= Cells(2, 5) Then 
Cells(2, 8) = 6.5 + ((Cells(2, 6) - 20) * 0.23) 
ElseIf Cells(2, 7) = "One Man" And Cells(2, 6) < Cells(2, 5) Then 
Cells(2, 8) = 6.5 + ((Cells(2, 5) - 20) * 0.23) 
ElseIf Cells(2, 7) = "Two Man" And Cells(2, 6) >= Cells(2, 5) Then 
Cells(2, 8) = 38 + ((Cells(2, 6) - 50) * 0.38) 
ElseIf Cells(2, 7) = "Two Man" And Cells(2, 6) < Cells(2, 5) Then 
Cells(2, 8) = 38 + ((Cells(2, 5) - 50) * 0.38) 
Else 
Cells(2, 14) = "This is not working" 
End If 
End Sub 

回答

0

是這樣的:通過7列中的所有行

Dim row As Integer 
row = 1 
While Cells(row, 7) <> "" 
    //Your original code here but with 2 replaced with row 
    row = row + 1 
Wend 

循環,直到遇到一個空白單元格。

+0

它確實有效。非常感謝 – Pradeep 2013-02-15 12:26:27

相關問題