loop VBA

JimmyJammy

New Member
Joined
Mar 17, 2021
Messages
2
Office Version
  1. 365
Hi, i'm a beginner to excel/vba and a little stuck on how to create the following using a loop command.

At present my VBA has list (long) of instructions;

Range("B3").Select
ActiveCell.Formula = "=SUM(Sheet1!C5-Sheet1!C4)"
Range("B4").Select
ActiveCell.Formula = "=SUM(Sheet1!E5-Sheet1!E4)"
Range("B5").Select
ActiveCell.Formula = "=SUM(Sheet1!G5-Sheet1!G4)"
Range("B6").Select
ActiveCell.Formula = "=SUM(Sheet1!I5-Sheet1!I4)"
Range("B7").Select
ActiveCell.Formula = "=SUM(Sheet1!K5-Sheet1!K4)"
Range("B8").Select
etc

Any help or advice would be greatly appreciated.

Thanks,

Jim
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Here is my version.
You only need the references to "Sheet1!" if your references to the Column B are not on Sheet 1.
Without naming the sheet its going to assume the active sheet.

VBA Code:
Sub FormulaLoop()

    Dim sumCol As Long
    Dim resultRowFirst As Long
    Dim resultRowLast As Long
    Dim iRow As Long
    
    resultRowFirst = 3
    resultRowLast = 10
    sumCol = 3
    
    For iRow = resultRowFirst To resultRowLast
    
        Range("B" & iRow).Formula = "=SUM(Sheet1!" & Cells(5, sumCol).Address(False, False) & " - Sheet1!" & Cells(4, sumCol).Address(False, False) & ")"
        sumCol = sumCol + 2
        
    Next iRow

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,944
Messages
6,122,391
Members
449,080
Latest member
Armadillos

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