2013-02-14 193 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

相关问题