Copying data based on ws name

struf

Board Regular
Joined
Jul 2, 2002
Messages
107
I need to copy data from one wb with multiple ws to another wb based on the name of the ws

First wb has 50 ws, one for each state. EX: C:\ qtr1.xlsx

In another folder, i have 50 wb , again one for each state (EX: C:\2011\ AL.xlsx), which happens to have 4 ws

I would like to copy the data on each individual in C:\ qtr1.xlsx to the appropriate wb in

C:\2011, and to make it more interesting, I need it copied to the second tab , let's call it "supplies". The Supplies tab is the same in each Wb

Now because I do not want to have to open each wb, i have found several macros that will open and close these wb once i find another macro to do all the copying. So i am not worrying about that at this time

I have tried looking at lookups, indirects, etc, but to tell you the truth, i don't even know where to start anymore.

While an answer to my problem would be fantastic, I would be very happy to get any sort of guidence so i can continue to research it.

Many thanks in advance. ANy direction is extremely appriciated.

Regards,

struf
 
Last edited:

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Where do you want to copy the data from each worksheet to on the supplies worksheet of the individual state workbook?
Code:
Option Explicit
 
Sub test()
Dim wbSrc As Workbook
Dim wbDst As Workbook
Dim wsSrc As Worksheet
Dim wsDst As Worksheet
Dim strFileName As String
Dim strPath As String
 
   Set wbSrc = ThisWorkbook    ' assumes code is in qrt1.xlsx
 
    strPath = "C:\2011\"
 
    For Each wsSrc In wbSrc.Worksheets
 
        strFileName = wsSrc.Name & ".xlsx"    ' assumes worksheet names in qtr1.xlsx match names of individual worksheets
 
        Set wbDst = Workbooks.Open(strPath & strFileName)
 
        Set wsDst = wbDst.Worksheets("supplies")
 
        ' very simple copy for testing
        wsSrc.Range("A1").CurrentRegion.Copy wsDst.Range("A" & Rows.Count).End(xlUp).Offset(1)
 
    Next wsSrc
 
End Sub

This works, but as you can see the copy is pretty simple.

It just copies the current region of each sheet below any data in the 'supplies' worksheet.
 
Upvote 0
It can copy the entire sheet to the "supplies" tab. I have headers in both, but they are the same. I will play and see how she does.

Many thanks for you reply.. I love this board!
 
Upvote 0
So could you just copy the entire sheet into the relevant workbook, delete the existing supplies worksheet and rename the copied worksheet?
 
Upvote 0
Well this seems to work it goes in place of the code in the loop.
Code:
        strFileName = wsSrc.Name & ".xlsx"    ' assumes worksheet names in qtr1.xlsx match names of individual worksheets

        Set wbDst = Workbooks.Open(strPath & strFileName)

        Set wsDst = wbDst.Worksheets("supplies")
        
        Application.DisplayAlerts = False
        
        wsDst.Delete
        
        Application.DisplayAlerts = False
               
        wsSrc.Copy Before:=wbDst.Worksheets(1)
        
        wbDst.Worksheets(1).Name = "supplies"
 
Upvote 0
Two things I noticed.

1) i need the copied sheet from C:\qtr1.xlsx to go to the second tab.
and

2) every now and then there will be a state that does not ahve anything in C:\qtr1.xlsx.
Can we pass over that without stopping the macro?
 
Last edited:
Upvote 0
1 In the code that copies the worksheet change Before to After.

Then in the next line, which renames the copied worksheet, change 1 to 2.

2 Why would that stop the macro?

It goes through each worksheet in qtr1.xlsx, so if there are only 10 state worksheets in that workbook it will only open, copy etc the corresponding 10 workbooks.

I actually checked that by only having 10 worksheets in 10 qtr1.xlsx but a workbook for each state in C:\2011.

Or do you mean something else? eg for some states the worksheet will have no data so you don't want to copy it.
 
Upvote 0
Its not your code, its my data that needs tweaking. Everyhting is working perfectly.
Thank you for your time and patience.
 
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,787
Members
452,942
Latest member
VijayNewtoExcel

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