VBA Pull data from closed multiple workbooks into Master workbook/sheet

breg523

New Member
Joined
Nov 17, 2014
Messages
4
Hi everyone,

Not to savvy with VBA data gymnastics however I am more familiar with excel formulation. I have a bunch of workbooks in folder 'C:\Users\BCR\Desktop\TSA' i am only interested in one sheet 'Custom Award File' and only interested in columns A-I. The first row is row title and i want to append the data into a master sheet. the title of the workbook files are dynamic, based on the date and other random naming convention. My question is, is there a macro/vba that can copy data from all the closed workbooks in sheet 'Custom Award File' into a master workbook. the sheet name does not change in any of the files. Please let me know if you have any questions. There are about 10-30 files in the folder that get updated progressively throughout the month.
 
This macro assumes that the source workbooks are the only files in your folder and that they have an "xlsx" externsion. Make sure that a sheet named "Master" exists in your destination workbook and that the first row in that sheet contains your headers from A:I.
Code:
Sub CopyRange()
    Application.ScreenUpdating = False
    Dim desWS As Worksheet, srcWB As Workbook
    Set desWS = ThisWorkbook.Sheets("Master")
    Dim LastRow As Long
    Const strPath As String = "C:\Users\BCR\Desktop\TSA\"
    ChDir strPath
    strExtension = Dir(strPath & "*.xlsx")
    Do While strExtension <> ""
        Set srcWB = Workbooks.Open(strPath & strExtension)
        With srcWB.Sheets("Custom Award File")
            LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            .Range("A2:I" & LastRow).Copy desWS.Cells(desWS.Rows.Count, "A").End(xlUp).Offset(1, 0)
        End With
        srcWB.Close False
        strExtension = Dir
    Loop
    Application.ScreenUpdating = True
End Sub

wow....Thanks mumps. This code works for me too! But I have one question, after pulling data (column B to E of worksheet “Sheet1”) from multiple workbooks in a folder, I realize I need the date of the data in each workbook to be added in the first column of the destination worksheet. The date of the data is stored in Cell B2 of the same worksheet “Notes” of each workbook. Possible you can help by modifying the existing vba code for this additional requirement? I know how to copy date for 1 row, but I need it to be populated for all the rows for each workbook. Thanks very much in advance!
 
Upvote 0

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Please use code tags to post your version of the code.
 
Upvote 0
Please use code tags to post your version of the code.
Hi mumps, i am new to this forum so i am trying to post like this, not sure it will be good or not. I removed LastRow code because my data currently all has 5 rows (actually now is from B5 to E8 & B13 to E13). But going forward, my rows on source workbook is uncertain (so will just take column B to E), so I need help to know how to copy date for multiple rows depending on each file's length.

VBA Code:
Sub ImportAccrual()

 Application.ScreenUpdating = False
    Dim desWS As Worksheet, srcWB As Workbook
    Set desWS = ThisWorkbook.Sheets("Data")
    Dim LastRow As Long
    Const strPath As String = "C:\Users\A\Downloads\New\"
    ChDir strPath
    strExtension = Dir(strPath & "*.xlsx")
    Do While strExtension <> ""
        Set srcWB = Workbooks.Open(strPath & strExtension)
        With srcWB.Sheets("Sheet1")
            
            .Range("B2").Copy desWS.Cells(desWS.Rows.Count, "A").End(xlUp).Offset(1, 0)
            .Range("B2").Copy desWS.Cells(desWS.Rows.Count, "A").End(xlUp).Offset(1, 0)
            .Range("B2").Copy desWS.Cells(desWS.Rows.Count, "A").End(xlUp).Offset(1, 0)
            .Range("B2").Copy desWS.Cells(desWS.Rows.Count, "A").End(xlUp).Offset(1, 0)
            .Range("B2").Copy desWS.Cells(desWS.Rows.Count, "A").End(xlUp).Offset(1, 0)
            
            .Range("B5:E8").Copy
            desWS.Cells(desWS.Rows.Count, "B").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
 
            .Range("B13:E13").Copy
            desWS.Cells(desWS.Rows.Count, "B").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues

            
        End With
        srcWB.Close False
        strExtension = Dir
    Loop
    Application.ScreenUpdating = True


End Sub
 
Upvote 0
Is there any data in B9:E12? Could the ranges to copy change to B5:E20 and B24:E24? What data do you currently have in B13:E13?
It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach a screenshot (not a picture) of your sheet. Alternately, you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary).
 
Upvote 0
Is there any data in B9:E12? Could the ranges to copy change to B5:E20 and B24:E24? What data do you currently have in B13:E13?
It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach a screenshot (not a picture) of your sheet. Alternately, you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary).
Hi Mumps, here it is. Let me know if it is still not clear.

My current data:
28-Jul-22​
DailyMonthlyYearly
product A
10​
100​
150​
product B
15​
-20​
200​
product C
100​
500​
700​
product D
500​
400​
900​
adjustment
-200​
-500​
-1000​

Future data:
28-Jul-22​
DailyMonthlyYearly
product A
10​
100​
150​
product B
15​
-20​
200​
product C
100​
500​
700​
product D
500​
400​
900​
product E
200​
-100​
-400​
….….….….
….….….….
….….….….
….….….….
adjustment
-200​
-500​
-1000​
 
Upvote 0
I'm sorry but the data you posted has no column letters or rows numbers so that makes it difficult to modify the code. Did you try the XL2BB add-in? If not, upload your file as I suggested in Post #25.
 
Upvote 0
I'm sorry but the data you posted has no column letters or rows numbers so that makes it difficult to modify the code. Did you try the XL2BB add-in? If not, upload your file as I suggested in Post #25.

Does this looks ok? Thanks!

Current data:
temp.xlsx
G
19
Sheet3


Future data:
temp.xlsx
J
16
Sheet4
 
Upvote 0
Unfortunately no. Use the "minisheet" option in the add-in.
 
Upvote 0
Unfortunately no. Use the "minisheet" option in the add-in.
strange, i was using minisheet.
How about now? Thanks.

Current:
temp.xlsx
BCDE
228-Jul-22
3
4Daily MonthlyYearly
5product A10100150
6product B15-20200
7product C100500700
8product D500400900
9
10
11
12
13adjustment-200-500-1000
Sheet3


Future:
temp.xlsx
BCDE
228-Jul-22
3
4Daily MonthlyYearly
5product A10100150
6product B15-20200
7product C100500700
8product D500400900
9product E200-100-400
10….….….….
11….….….….
12….….….….
13….….….….
14adjustment-200-500-1000
Sheet4
 
Upvote 0

Forum statistics

Threads
1,223,705
Messages
6,173,996
Members
452,542
Latest member
Bricklin

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