Loop VBA and increase values in the code

Damen

Board Regular
Joined
May 2, 2004
Messages
56
Hi there, it has been a few years since I last posted.

Is there a way that I can loop the following code 27 times, and each time it loops I want to increment every instance of DirectDebit in the code, i.e. DirectDebit01, DirectDebit 01n etc - loop - DirectDebit02, DirectDebit 02n etc - loop - all the way to DirectDebit27? Thanks so much in advance.

'DirectDebit01
Cells.Find(What:="DirectDebit01", After:=[A1], LookIn:= _
xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext _
, MatchCase:=False).Select
Selection.Offset(0, 1).Name = "DirectDebit01n"
[DirectDebit01n].Offset(1, 0).Name = "DirectDebit01TL"
[DirectDebit01TL].Offset(11, 2).Name = "DirectDebit01BR"
[DirectDebit01TL:DirectDebit01BR].Name = "DirectDebitRange01"
ThisWorkbook.Names("DirectDebit01TL").Delete
ThisWorkbook.Names("DirectDebit01BR").Delete
myvar = Left([ThisFileName].Value, 3)
Cells.Find(What:=myvar, After:=[DirectDebit01n], LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False) _
.Offset(0, 2).Name = "DirectDebit01"
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Possibly try a for i x to y loop and refer to the iteration number in a name like the below


So use a for i = 1 to 27 to loop

inside the loop use;
Code:
sDirectDebit = "DirectDebit" & Format(i, "00")
Then use that where ever you need to loop the name, i.e. "DirectDebit01BR" becomes sDirectDebt & "BR"
 
Upvote 0
Possibly try a for i x to y loop and refer to the iteration number in a name like the below


So use a for i = 1 to 27 to loop

inside the loop use;
Code:
sDirectDebit = "DirectDebit" & Format(i, "00")
Then use that where ever you need to loop the name, i.e. "DirectDebit01BR" becomes sDirectDebt & "BR"
Thanks, I'll give that a try
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,198
Members
453,022
Latest member
RobertV1609

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