Macro to loop through multiple rows of cell references

paul_sykes00

Board Regular
Joined
May 7, 2012
Messages
53
Hi there,

I have a excel sheet1 in which various cells reference cellvalues from the first row of cells on sheet2. (eg. Cell B15 on sheet1 mighthave the formula =sheet2!A1)

I am trying to create a macro that will run my existingmacro (lets call it macroX) then change the references in sheet1 to the secondrow of cells on sheet2 and repeat through all rows of cells until no more datais included on the row in sheet2.

Hope that makes sense. Thanks in advance for your help

 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Welcome to the Forum!

I'd do it like this:

Sheet1!B15: =INDEX(Sheet2!A:A,No)
where No is a range name, which you can set manually to 1 or 2 or 3 ....

Code:
Sub MyLoop()

    Dim i As Long
    
    Application.ScreenUpdating = False
    
    For i = 1 To Worksheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
        Range("No").Value = i
        Call MacroX
    Next i
    
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
Thanks for your response Stephen. Marco just seems to stop wen it gets to Range("No").Value = i

Any idea what I am doing wrong?
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

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