VBA Macro to copy specific cells in different workbooks to a single master workbook

MarkR3003

New Member
Joined
Aug 11, 2019
Messages
25
Hi

Dont know where to start with this request....

I have many excel files all of exactly the same structure with different data but each one is saved / named differently - lets call these files 'Form 1.xls', 'Form 2.xls' and 'Form 3.xls' - see attached

I want to be able to open each file called 'Form 1 etc' in turn and use a personal macro to copy specific cells in this file and their values into a master workbook into a common table to bring all the information together - lets call this file 'Master.xls' - see attached

Form 1
Form 1.JPG


Form 2
Form 2.JPG


Form 3
Form 3.JPG


Master
Master.JPG


Appreciate any advice or VBA code that I could start with


Thanks
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
See if you can adapt this code for a similar request:

 
Upvote 0
Thanks John/w

The solution works well and has solved my problem
Just one question though...the macro deletes the column headers from the destination sheet (destSheet) when the matching workbook data has been pasted in. Is there any amendment to the VBA that I can use to retain the column headers in the destSheet?
Thanks

Welcome to MrExcel forums.

Try this macro, changing the workbooks folder and filespec and destination sheet name where indicated.
VBA Code:
Public Sub Copy_Values_From_Workbooks()

    Dim matchWorkbooks As String
    Dim destSheet As Worksheet, r As Long
    Dim folderPath As String
    Dim wbFileName As String
    Dim fromWorkbook As Workbook
  
    'Folder path and wildcard workbook files to import cells from
  
    matchWorkbooks = "C:\folder\path\*.xls"                                             'CHANGE THIS
  
    'Define destination sheet
  
    Set destSheet = ActiveWorkbook.Worksheets("Summary")                                'CHANGE THIS
  
    destSheet.Cells.Clear
    r = 0
  
    Application.ScreenUpdating = False
          
    folderPath = Left(matchWorkbooks, InStrRev(matchWorkbooks, "\"))
    wbFileName = Dir(matchWorkbooks)
    While wbFileName <> vbNullString
        Set fromWorkbook = Workbooks.Open(folderPath & wbFileName)
        With fromWorkbook.Worksheets(1)
            destSheet.Range("B8:G8").Offset(r).Value = .Range("Q10:V10").Value
            destSheet.Range("H8:M8").Offset(r).Value = .Range("Q13:V13").Value
            destSheet.Range("N8:S8").Offset(r).Value = .Range("Q16:V16").Value
            destSheet.Range("A8").Offset(r).Value = .Range("B14").Value
            r = r + 1
        End With
        fromWorkbook.Close savechanges:=False
        DoEvents
        wbFileName = Dir
    Wend
  
    Application.ScreenUpdating = True
  
    MsgBox "Finished"
  
End Sub
 
Upvote 0
the macro deletes the column headers from the destination sheet (destSheet) when the matching workbook data has been pasted in.
I can't see any code which deletes the column headers.

Post your adaptation of the macro.
 
Upvote 0
Thanks for the reply

I have only tested it with two cells to copy into a Masterdata table using the macro below


Public Sub Copy_Values_From_Workbooks()

Dim matchWorkbooks As String
Dim destSheet As Worksheet, r As Long
Dim folderPath As String
Dim wbFileName As String
Dim fromWorkbook As Workbook

'Folder path and wildcard workbook files to import cells from

matchWorkbooks = "C:\Users\M5070839\Documents\1. SG Insulation UK\07. Commercial Support Calculator\Dev File\Source Files\*.xls" 'CHANGE THIS

'Define destination sheet

Set destSheet = ActiveWorkbook.Worksheets("MasterData") 'CHANGE THIS

destSheet.Cells.Clear
r = 0

Application.ScreenUpdating = False

folderPath = Left(matchWorkbooks, InStrRev(matchWorkbooks, "\"))
wbFileName = Dir(matchWorkbooks)
While wbFileName <> vbNullString
Set fromWorkbook = Workbooks.Open(folderPath & wbFileName)
With fromWorkbook.Worksheets(1)
destSheet.Range("A4").Offset(r).Value = .Range("C13").Value
destSheet.Range("B4").Offset(r).Value = .Range("C3").Value
r = r + 1
End With
fromWorkbook.Close savechanges:=False
DoEvents
wbFileName = Dir
Wend

Application.ScreenUpdating = True

MsgBox "Finished"

End Sub
 
Upvote 0
Just one final question...

If I need the code to take the copy cells from a specific tab in each of the matchWorkbook files, how would I amend this line of code to add the worksheet tab name which is the same in each of the matchWorkbook files

matchWorkbooks = "C:\Users\M5070839\Documents\1. SG Insulation UK\07. Commercial Support Calculator\Dev File\Source Files\*.xls" - how to add the worksheet tab name?
 
Upvote 0
Hi John_w
Would you be able to advice how I can copy data from a specific worksheet tab in the matchWorkbook coding as each workbook has multiple tabs and I think that the macro is looking at the first worksheet in each workbook rather than the worksheet that I need it to
Many thanks for your help again
 
Upvote 0
Change:
VBA Code:
With fromWorkbook.Worksheets(1)
to:
VBA Code:
With fromWorkbook.Worksheets("name of sheet")
 
Upvote 0
Solution

Forum statistics

Threads
1,215,851
Messages
6,127,302
Members
449,374
Latest member
analystvar

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