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

Some videos you may like

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
17,388
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
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
........
 

mgabriel2490

New Member
Joined
Mar 1, 2016
Messages
41
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.
 

mgabriel2490

New Member
Joined
Mar 1, 2016
Messages
41
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
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
55,916
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

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?
 

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
17,388
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
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
 

mgabriel2490

New Member
Joined
Mar 1, 2016
Messages
41

ADVERTISEMENT

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
 

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
17,388
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
See my reply in post #6.
 

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
If you use the Cells property rather than the Range property you can pass a number as the Column argument eg Cells(104, i).
 

mgabriel2490

New Member
Joined
Mar 1, 2016
Messages
41
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!!!
 

Watch MrExcel Video

Forum statistics

Threads
1,122,469
Messages
5,596,316
Members
414,053
Latest member
Dual Showman

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
Top