How to Repeat Calculation for Multiple Range using Excel VBA?

steve1011

New Member
Joined
May 5, 2012
Messages
2
Hi, I've been trying to repeat the calculations for a range using excel VBA

Here is the formula

If Range("H9") = "Repair" Then
Range("K9") = Range ("K9") + Range("G9")

If Range("H9") = "Service" Then
Range("L9") = Range ("L9") + Range("G9")

End If

Can someone please shed some lights on how to repeat this for a range, says 9 to 20?

Thanks a lots

Regards,
Steve
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Try...

Code:
[font=Courier New][color=darkblue]Option[/color] [color=darkblue]Explicit[/color]

[color=darkblue]Sub[/color] test()

    [color=darkblue]Dim[/color] i [color=darkblue]As[/color] [color=darkblue]Long[/color]

    [color=darkblue]For[/color] i = 9 [color=darkblue]To[/color] 20
        [color=darkblue]Select[/color] [color=darkblue]Case[/color] Cells(i, "H").Value
            [color=darkblue]Case[/color] "Repair"
                Cells(i, "K").Value = Cells(i, "K").Value + Cells(i, "G").Value
            [color=darkblue]Case[/color] "Service"
                Cells(i, "L").Value = Cells(i, "L").Value + Cells(i, "G").Value
        [color=darkblue]End[/color] [color=darkblue]Select[/color]
    [color=darkblue]Next[/color] i
    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
[/font]

However, if you don't want this to be case-sensitive, try...

Code:
[font=Courier New][color=darkblue]Option[/color] [color=darkblue]Explicit[/color]

[color=darkblue]Sub[/color] test()

    [color=darkblue]Dim[/color] i [color=darkblue]As[/color] [color=darkblue]Long[/color]

    [color=darkblue]For[/color] i = 9 [color=darkblue]To[/color] 20
        [color=darkblue]Select[/color] [color=darkblue]Case[/color] U[color=darkblue]Case[/color](Cells(i, "H").Value)
            [color=darkblue]Case[/color] "REPAIR"
                Cells(i, "K").Value = Cells(i, "K").Value + Cells(i, "G").Value
            Case "SERVICE"
                Cells(i, "L").Value = Cells(i, "L").Value + Cells(i, "G").Value
        [color=darkblue]End[/color] [color=darkblue]Select[/color]
    [color=darkblue]Next[/color] i
    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
[/font]
 
Upvote 0
try:
Code:
Sub test()
For rws = 9 To 20
    If Range("H" & rws) = "Repair" Then
        Range("K" & rws) = Range("K" & rws) + Range("G" & rws)
    ElseIf Range("H" & rws) = "Service" Then
        Range("L" & rws) = Range("L" & rws) + Range("G" & rws)
    End If
Next rws
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,380
Members
448,955
Latest member
BatCoder

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top