Loop through copy using named ranges to multiple sheets

Mark BMS

New Member
Joined
Jan 11, 2016
Messages
26
Office Version
  1. 365
Platform
  1. Windows
Hi All

Been a while since I've coded excel and never got my head around loops so I'm hoping someone will offer some assistance.

I have a workbook that has imported data on Sheet1 and Sheet2 (sheet Index not name)

Sheet 1 has 132 Col with named ranges "$A$3:$A$1442" is "TimeStamp" then all other Col "$x$3:$x$1442" (where x is Col 2 through 131) and named Meter1 through Meter131

Sheet 2 has a range of names in "A1:A132"

I have working code to add new sheets based on the range on sheet2 (although for testing purposes I am limiting this to 5 new sheets
I also have working code to copy the "Timestamp" range to all sheets except the first 2

I have the following code to copy a named range to a sheet at "B1

VBA Code:
Range("Meter1").Copy (Worksheets(3).Range("B1"))

What I'm looking to do is loop through this code for each of the sheets from sheet 3 to the last sheet increasing the named range number by 1 so in essence it would be carrying out the following

VBA Code:
Range("Meter1").Copy (Worksheets(3).Range("B1"))
Range("Meter2").Copy (Worksheets(4).Range("B1"))
Range("Meter3").Copy (Worksheets(3).Range("B1"))
    'etc, etc.....

I'm assuming it would look (roughly) something along the line of......

VBA Code:
Sub CopyPaste()
Dim m As Double, w As Double, ws As Worksheet
'I'm assuming I've declared the variable to store the number of sheets but don't know if that correct

For Each ws In Worksheets
    'this is the bit I need help with
    'to set the variable m on each loop starting at 1 and add to Meter so the Range changes to Meter1, Meter2.....Meter131
    'to set the variable w on each loop starting at 3 and insert into the Worksheets index () so the index increase Worksheet(3), Worksheet(4)......Worksheet(133)

    Range("Meter" & m).Copy (Worksheets(w).Range("B1"))
    'not sure if this is even close to the correct syntax
Next ws
    
End Sub

Any assistance is greatly appreciated
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
I'm a little unclear on what you are trying to do. Would this work?

VBA Code:
Sub CopyPaste()
    Dim m As Long
    Dim w As Long

    For m = 1 To 131
        w = m + 2

        Worksheets("Sheet1").Range("Meter" & m).Copy Worksheets(w).Range("B1")
    Next m
End Sub
 
Upvote 0
Solution
I'm a little unclear on what you are trying to do. Would this work?

VBA Code:
Sub CopyPaste()
    Dim m As Long
    Dim w As Long

    For m = 1 To 131
        w = m + 2

        Worksheets("Sheet1").Range("Meter" & m).Copy Worksheets(w).Range("B1")
    Next m
End Sub
Perfect, thank you for your help. Your answer has helped me understand the For/Next process so hopefully I'll work it out for myself next time
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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