"next i" loop instead of repeated code

mgabriel2490

New Member
Joined
Mar 1, 2016
Messages
41
i started out with code like this...

Code:
Sheets("DailyDisp").Range("a104").AutoFill Destination:=Sheets("DailyDisp").Range("A104").Resize(Sheets("Employees").Range("ap2").Value, 1)
 
Sheets("DailyDisp").Range("b104").AutoFill Destination:=Sheets("DailyDisp").Range("b104").Resize(Sheets("Employees").Range("ap2").Value, 1)
 
Sheets("DailyDisp").Range("c104").AutoFill Destination:=Sheets("DailyDisp").Range("c104").Resize(Sheets("Employees").Range("ap2").Value, 1)
 
Sheets("DailyDisp").Range("g104").AutoFill Destination:=Sheets("DailyDisp").Range("g104").Resize(Sheets("Employees").Range("ap2").Value, 1)
 
Sheets("DailyDisp").Range("h104").AutoFill Destination:=Sheets("DailyDisp").Range("h104").Resize(Sheets("Employees").Range("ap2").Value, 1)


but i want to look something like....
Code:
For i = a To m

Sheets("DailyDisp").Range("104" & i).AutoFill Destination:=Sheets("DailyDisp").Range("104" & i).Resize(Sheets("Employees").Range("ap2").Value, 1)
'Sheets("DailyDisp").Range("A104").AutoFill Destination:=Sheets("DailyDisp").Range("A104").Resize(Sheets("Employees").Range("ap2").Value, 1)
Next i


Additionally i have....

Code:
'Sheets("Master Data").Range("c105").AutoFill Destination:=Sheets("Master Data").Range("c105").Resize(Sheets("Employees").Range("ap2").Value, 1)
 
'Sheets("Master Data").Range("d105").AutoFill Destination:=Sheets("Master Data").Range("d105").Resize(Sheets("Employees").Range("ap2").Value, 1)
 
'Sheets("Master Data").Range("e105").AutoFill Destination:=Sheets("Master Data").Range("e105").Resize(Sheets("Employees").Range("ap2").Value, 1)
 
'Sheets("Master Data").Range("f105").AutoFill Destination:=Sheets("Master Data").Range("f105").Resize(Sheets("Employees").Range("ap2").Value, 1)
 
'Sheets("Master Data").Range("h105").AutoFill Destination:=Sheets("Master Data").Range("h105").Resize(Sheets("Employees").Range("ap2").Value, 1)


that goes on and on through column YJ, so i need something like...

Code:
For i = c To YJSheets("Master Data").Range("105" & i).AutoFill Destination:=Sheets("Master Data").Range("105" & i).Resize(Sheets("Employees").Range("ap2").Value, 1)
'Sheets("Master Data").Range("c105").AutoFill Destination:=Sheets("Master Data").Range("c105").Resize(Sheets("Employees").Range("ap2").Value, 1)
Next i


what am i missing with these "i loops" ?

thank you all for your help in advance
 
Last edited:
oh it is a beautiful thing!!!

JoeMo... I cannot thank you enough!

Andrew Poulsom, thank you for the info... I will keep that in mind.

Everyone have a great weekend!!!
You are welcome.

Andrew has offered the simplest solution to your second question, but I thought you might want to see how to loop through column letters using numbers since that was the essence of your second question.
 
Upvote 0

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Welp....

I spoke to soon....

Code:
Dim colNum As Long, colLetter As String
For colNum = 3 To 660
colLetter = Split(Columns(660).Address(0, 0), ":")(0)


Sheets("Master Data").Range(colLetter & 105).AutoFill Destination:=Sheets("Master Data").Range(colLetter & 105).Resize(Sheets("Employees").Range("ap2").Value, 1)
Next colNum


Ran the macro and it stopped at column HA. And I don't have a clue why. There is no change in consistency for data, formulas, etc.
 
Upvote 0
I used Andrew's method...

Code:
For i = 3 To 660
Sheets("Master Data").Cells(105, i).AutoFill Destination:=Sheets("Master Data").Cells(105, i).Resize(Sheets("Employees").Range("ap2").Value, 1)
Next i


And I think it worked fully. Not sure why it didn't the other way, but would like to know if anyone has an idea.


Again....thank you both!
 
Upvote 0
Welp....

I spoke to soon....

Code:
Dim colNum As Long, colLetter As String
For colNum = 3 To 660
[COLOR=#ff0000][B]colLetter = Split(Columns(660)[/B][/COLOR].Address(0, 0), ":")(0)


Sheets("Master Data").Range(colLetter & 105).AutoFill Destination:=Sheets("Master Data").Range(colLetter & 105).Resize(Sheets("Employees").Range("ap2").Value, 1)
Next colNum


Ran the macro and it stopped at column HA. And I don't have a clue why. There is no change in consistency for data, formulas, etc.
That's not the code I posted in post #6, the red font is not right - replace 660 with colNum.
 
Upvote 0

Forum statistics

Threads
1,215,575
Messages
6,125,624
Members
449,240
Latest member
lynnfromHGT

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