将Excel VBA单行宏应用于单元格范围

时间:2013-02-14 22:05:32

标签: excel-vba vba excel

有人可以解释如何让这个宏在整列上运行而不是单行单元格吗?即,检查每个单独单元的值,并在其行上的相应单元中执行所需的计算和输出。

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

1 个答案:

答案 0 :(得分:0)

类似的东西:

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

循环遍历第7列中的所有行,直到它到达空白单元格。