"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:

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
For i = A to M you can use:

For i = 65 to 77
Range(Chr(i) & 105) = ....
'code
Next i

For any column letter(s) this will return the column letter(s) - I've used "YJ" (column 660) for this example:

Split(Columns(660).Address(0, 0), ":")(0)

so you can use
For colNum = 3 to 660
........
 
Upvote 0
JoeMo,

You kind of lost me...

For i = 65 to 77 (where did the 65 and 77 come from and what do they stand for?)

Here is the code I tried for the first part and it seems to work. Thank You!!!! But I don't understand it.

Code:
For i = 65 To 77

Sheets("DailyDisp").Range(Chr(i) & 104).AutoFill Destination:=Sheets("DailyDisp").Range(Chr(i) & 104).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


For the second part (C to YJ), I am really confused.

This is what I have so far....

Code:
For colNum = 3 To 660Split(Columns(660).Address(0, 0), ":")(0).Sheets("Master Data").Range(Chr(i) & 105).AutoFill Destination:=Sheets("Master Data").Range(Chr(i) & 105).Resize(Sheets("Employees").Range("ap2").Value, 1)


'Next i

I know I didn't do this right. Getting an invalid next error.
 
Upvote 0
Ok, I figured the 65 to 77 thing out. Since you used Chr.... 65 = A & 77 = M

Still unsure with the 2nd part. Have been trying to mess with it and looking up stuff about split functions.

This is what i have....

Code:
For colNum = 3 To 660colNum = Split(Columns(660).Address(0, 0), ":")(0)


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

It is causing a run time error
 
Upvote 0
This doesn't look right:
Code:
For colNum = 3 To 660colNum = Split(Columns(660).Address(0, 0), ":")(0)
It appears that you have multiple lines of code blended together on one line.
Is that really how you have this?
 
Upvote 0
Ok, I figured the 65 to 77 thing out. Since you used Chr.... 65 = A & 77 = M

Still unsure with the 2nd part. Have been trying to mess with it and looking up stuff about split functions.

This is what i have....

Code:
For colNum = 3 To 660colNum = Split(Columns(660).Address(0, 0), ":")(0)


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

It is causing a run time error
Try it like this:
Code:
Dim colNum as long, colLetter as String
For colNum = 3 to 660
colLetter = Split(Columns(colNum).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
 
Upvote 0
no, sorry about that.

it actually looks like:

Code:
For colNum = 3 To 660
colNum = Split(Columns(660).Address(0, 0), ":")(0)


Sheets("Master Data").Range("105" & colNum).AutoFill Destination:=Sheets("Master Data").Range("105" & colNum).Resize(Sheets("Employees").Range("ap2").Value, 1)
Next colNum
 
Upvote 0
If you use the Cells property rather than the Range property you can pass a number as the Column argument eg Cells(104, i).
 
Upvote 0
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!!!
 
Upvote 0

Forum statistics

Threads
1,213,531
Messages
6,114,167
Members
448,554
Latest member
Gleisner2

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