vba Loop copy paste macro across 31 sheets

Deliverable7

New Member
Joined
Apr 9, 2016
Messages
33
Hi

I have a spreadsheet with 31 pages each containing daily input data and a messy range of calculations. I have modified the current spreadsheet to include complex data ranges (on a new tab) from which i pull up some graphs.

I now need to use this current spreadsheet as a template and to import data from previous months, each which is stored in separate spreadsheets. I have created a simple macro that can copy the input fields from one open tab on the new month file and paste into the corresponding tab on the template file.

So in summary I currently operate by:
'June.xls tab '1' open, Template.xls tab '1' open - execute the macro which copies the data ranges from June '1' to Template '1'.
I then open the respective tab '2's on each file and run the macro again, then for 3, then for 4 and so on.

I need to do this 31 times for each month (and probably need to convert 12 months of data).

Is there a way to run the macro once looping through until the input data from June '1' through to June '31' is copied across to the corresponding tabs on the Template file?

Thanks in anticipation.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Deliverable7,

You might consider...

Code:
Sub AnotherLoop()
Dim wb As Workbook, wbSource As Workbook
Dim ws As Worksheet, wsSource As Worksheet
Dim fNameAndPath As Variant
Set wb = ThisWorkbook

fNameAndPath = Application.GetOpenFilename(FileFilter:="Excel Files (*.XLS?), *.XLS?", Title:="Select File To Open")
If fNameAndPath = False Then Exit Sub
Set wbSource = Workbooks.Open(Filename:=fNameAndPath)

For Each ws In wb.Worksheets
    For Each wsSource In wbSource.Sheets
        If ws.Name = wsSource.Name Then
            '''' Place your copy/paste code here
            '''' For example: wsSource.Range("A1:G20").Copy Destination:=ws.Range("A1")
            Exit For
        End If
    Next wsSource
Next ws
End Sub

The code will open a file dialog box and prompt you to choose a file/workbook, then will proceed to copy/paste data from/to matching worksheets.

Cheers,

tonyyy
 
Upvote 0
Hi tonyyy
Thanks for your feedback.
Can i restrict the range of sheets to copy/past to say '1', '2','3' and so on until '31'.
Thanks
Colin
 
Upvote 0
Aren't the sheets in the template workbook named 1, 2, 3, etc?
 
Upvote 0
Yes both workbooks contain the 1 to 31 sheets plus others. I can control the number of additional sheets in the template file however as the June file is controlled by others they can add (or remove) some of these additional sheets. Apologies for omitting this in the initial request.
 
Upvote 0
Change this line...

Code:
If ws.Name = wsSource.Name Then

to...

Code:
If ws.Name = wsSource.Name And IsNumeric(CLng(ws.Name)) Then

Assuming your other sheets have text names and not pure number names then this will exclude them from being copied.
 
Upvote 0
And we can actually eliminate the CLng from the line...

Code:
If ws.Name = wsSource.Name And IsNumeric(ws.Name) Then
 
Upvote 0
Hi tonyyy
That's exactly what I'm after.

Thank you so much. The macro is going to be very useful.

Just for future reference how would the code need to be modified to cater for non-numeric sheet names i.e. if I needed to copy paste between matching sheets say 'Week 1', 'Week 2', 'Week 3' and so on. I'm sure to come across other variations on the sheet names.

Regards
 
Upvote 0
Thank you so much. The macro is going to be very useful.

You're very welcome.

...how would the code need to be modified to cater for non-numeric sheet names...

Just revert back to the original code...

Code:
If ws.Name = wsSource.Name Then
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,195
Members
449,072
Latest member
DW Draft

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