Turn existing VBA code into a loop

anglais428

Well-known Member
Joined
Nov 23, 2009
Messages
634
Office Version
  1. 2016
Platform
  1. Windows
Hi,
I have the current VBA code below which I would like to turn into a loop:

Code:
Dim NoYrs As Long 'number of years
Dim LRbf As Long 'last row


NoYrs = Sheets("Lists").Range("E655536").End(xlUp).Row - 1
LRbf = Sheets("BF").Range("A655536").End(xlUp).Row




' Get First batch
    Sheets("BF").Select
    Range(Cells(3, 4), Cells(LRbf, (NoYrs + 3))).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Sheets("Upload sheet").Select
    Range("J2").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    
'Get second batch
    Sheets("BF").Select
    Range(Cells(3, (NoYrs+3+1)), Cells(LRbf, (NoYrs * 2 + 3))).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Sheets("Upload sheet").Select
    Range("J" & LRbf).Select
    ActiveSheet.Paste
    Application.CutCopyMode = False

Any advise?
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
To try to explain this further take the below:

Code:
    Sheets("Cluster").Select    Range(Cells(3, ((NoYrs * [B]0[/B]) + 3 + 1)), Cells(LRbf, (NoYrs * [B]1[/B] + 3))).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Sheets("Upload sheet").Select
    Range("J" & 2 + (NoLocations * [B]8[/B])).Select
    ActiveSheet.Paste
    Application.CutCopyMode = False

I have 5 'batches' I need to do. At the minute I have the code above written out 5 times and it works fine. I am wondering if I can somehow loop it so that those values in bold would increment by 1 each time it loops. So for example the 0 becomes 1; the 1 becomes 2; and the 8 become 9. And for the third run the 1 would become a 2; the 2 becomes a 3; and the 9 becomes 10. Then repeat until all 5 batches are run.

Thanks
 
Upvote 0
Have a look at http://www.excel-easy.com/vba/loop.html

You can then simply replace the numbers you want to increment with variables that for each iteration of the loop increases by one.

Something like:

Code:
Dim x As LongDim y As Long
Dim z As Long
Dim i As Long


x = 0
y = 1
z = 8


For i = 1 To 5

    Sheets("Cluster").Select Range(Cells(3, ((NoYrs * x) + 3 + 1)), Cells(LRbf, (NoYrs * y + 3))).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Sheets("Upload sheet").Select
    Range("J" & 2 + (NoLocations * z)).Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    
    x = x + 1
    y = y + 1
    z = z + 1

Next i
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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