Pull data from multiple workbooks to populate new workbook

Mikeymike_W

Board Regular
Joined
Feb 25, 2016
Messages
171
Hi,

I want to create a spreadsheet that will pull data from a form that my clients use.

There will be many of these forms and each one will have a slightly different file name but be stored in the same folder (C:\Users\MWa\Desktop\VBA Test\filename)

I want to copy the exact same cells from sheet 3 in the form to the new spreadsheet.

There will be many of these forms within the folder at one time. Can the code read all of these and add a new line in the spreadsheet for each form with one click?

I realise I'm asking a lot but I'm hoping the excel gods are watching over me :)

Many thanks in advance,

Mike
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Give this a go.
Code:
Sub ConsolidateWbks()

    Dim Pth As String
    Dim MstSht As Worksheet
    Dim fname As String
    
Application.ScreenUpdating = False

    Pth = "C:\Users\MWa\Desktop\VBA Test\"
    Set MstSht = ThisWorkbook.Sheets("[COLOR=#ff0000]Test1[/COLOR]")
    fname = Dir(Pth & "*xls*")
    Do While Len(fname) > 0
        Workbooks.Open (Pth & fname)
        With Workbooks(fname)
            .Sheets("[COLOR=#ff0000]Sheet3[/COLOR]").Range("[COLOR=#ff0000]A1:F5[/COLOR]").copy MstSht.Range("A" & Rows.Count).End(xlUp).Offset(1)
            .Close , False
        End With
        fname = Dir
    Loop

End Sub
You'll need to change the sheet names & range to suit.
 
Upvote 0
Hi Fluff,

Thanks for your help, the code youprovided works well but I'm encountering some unforeseen issues.

The cells that are being copied contain a formula to populate it with a value. The code you provided seems to copy the formula in the cell rather than the value in the cell, is there a way around this?

Thanks for your help :)

Mike
 
Upvote 0
I also meant to ask one other thing.

Is there a way to suppress the save changes prompt?
At the moment it asks me for each workbook open which is quite frustrating.

I tried adding, ThisWorkbook.Saved = True to the code but it didn't seem to do anything.

Thanks again,

Mike
 
Upvote 0
Hi,

I've managed to workout how to copy the value of the cell by altering the code as follows:

Code:
.Sheets("Global FACT").Range("B2").Copy
MstSht.Range("E" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues

I still need help with the save prompt though
 
Last edited:
Upvote 0
As you are only copying 1 cell you can use this
Code:
            MstSht.Range("E" & Rows.Count).End(xlUp).Offset(1).Value = .Sheets("Global FACT").Range("B2").Value
            Application.DisplayAlerts = False
            .Close , False
            Application.DisplayAlerts = True
It avoids copying data to the clipboard & back again. This should also prevent the prompt from coming up.
 
Upvote 0
Yup this works a charm thanks :)

One new issue though is that I don't just have one cell, I have about 30 cells that I need to copy. I thought I could just keep repeating the code and change the range but I quickly realised that it adds a new line for each cell that it copies... would you know how to adjust the code to allow for multiple cells in various locations?

Thanks again,

Mike
 
Upvote 0
What cells do you want to copy & where do you want them copied to?
 
Upvote 0
There are two sheets within the one workbook I want to copy the cells from.
3 cells on a sheet named "CRF" the cells are C12, C16 and G4.
The other cells are on a sheet named "Global" and the range is B2:B35 as well as cells C36.

I want all of these cells copied to a new row each cell assigned to a different column within that row... this will be within a separate workbook where the code will run from.

Thanks again,

Mike :)
 
Last edited:
Upvote 0
Ok, try this
Code:
Sub ConsolidateWbks()

    Dim Pth As String
    Dim MstSht As Worksheet
    Dim fname As String
    Dim Rng As Range
    
Application.ScreenUpdating = False

    Pth = "C:\Users\MWa\Desktop\VBA Test\"
    Set MstSht = ThisWorkbook.Sheets("Test1")
    fname = Dir(Pth & "*xls*")
    Do While Len(fname) > 0
        Workbooks.Open (Pth & fname)
        With Workbooks(fname)
            Set Rng = MstSht.Range("E" & Rows.Count).End(xlUp).Offset(1)
            Rng.Resize(, 34).Value = Application.Transpose(.Sheets("Global FACT").Range("B2:B35").Value)
            Rng.Offset(, 35).Value = .Sheets("Global FACT").Range("C36").Value
            Rng.Offset(, 36).Value = .Sheets("CRF").Range("C12").Value
            Rng.Offset(, 37).Value = .Sheets("CRF").Range("C16").Value
            Rng.Offset(, 38).Value = .Sheets("CRF").Range("G4").Value
            Application.DisplayAlerts = False
            .Close , False
            Application.DisplayAlerts = True
        End With
        fname = Dir
    Loop

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,243
Members
449,075
Latest member
staticfluids

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