+ Reply to Thread
Results 1 to 3 of 3

Excel VBA, Consolidate into Master Sheet

Hybrid View

  1. #1
    Registered User
    Join Date
    07-10-2017
    Location
    KSA
    MS-Off Ver
    2013
    Posts
    1

    Question Excel VBA, Consolidate into Master Sheet

    Hi,

    I'm trying to consolidate multiple workbooks into the a master sheet, and I'm trying to do the following
    Loop through all excel files in directory
    Copy specific cells in Datasheet1 from all excel files and transpose into columns A:C in the mastersheet
    Copy all rows in Datasheet2 from all excel files to the master sheet into columns D:G
    Repeat the transposed cells into all copied rows
    Copy any new data from new sheets into last row+1


    I managed to find the loop function online that serves the purpose, however i'm having trouble with the copy/paste
    Any help is appreciated

    Attached is sample excel for data and mastersheet

    Sample Data:

    File 1 Name: DataFile 1
    Sheet 1: DataSheet1

    Column 1 Column 2
    Name File1 - A
    Number File1 - B
    Data File1 - C

    Sheet2: Datasheet2
    Column 1 Column 2 Column 3 Column 4 Column 5
    File1 - D File1 - E File1 - F File1 - G File1 - H
    File1 - I File1 - J File1 - K File1 - L File1 - M

    All files in the directory have the same structure

    Master Sheet in specified workbook
    Column 1 Column 2 Column 3 Column 4 Column 5 Column 6 Column 7 Column 8
    File1 - A File1 - B File1 - C File1 - D File1 - E File1 - F File1 - G File1 - H
    File1 - A File1 - B File1 - C File1 - I File1 - J File1 - K File1 - L File1 - M
    File2 - A File2 - B File2 - C File2 - D File2 - E File2 - F File2 - G File2 - H


    Sub LoopAllExcelFilesInFolder()
    'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them
    
    Dim wb As Workbook
    Dim myPath As String
    Dim myFile As String
    Dim myExtension As String
    Dim FldrPicker As FileDialog
    
    'Optimize Macro Speed
      Application.ScreenUpdating = False
      Application.EnableEvents = False
      Application.Calculation = xlCalculationManual
    
    'Retrieve Target Folder Path From User
      Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
    
        With FldrPicker
          .Title = "Select A Target Folder"
          .AllowMultiSelect = False
            If .Show <> -1 Then GoTo NextCode
            myPath = .SelectedItems(1) & "\"
        End With
    
    'In Case of Cancel
    NextCode:
      myPath = myPath
      If myPath = "" Then GoTo ResetSettings
    
    'Target File Extension (must include wildcard "*")
      myExtension = "*.xls*"
    
    'Target Path with Ending Extention
      myFile = Dir(myPath & myExtension)
    
    'Loop through each Excel file in folder
      Do While myFile <> ""
        'Set variable equal to opened workbook
          Set wb = Workbooks.Open(Filename:=myPath & myFile)
          
        'Copy Data from workbook into master sheet
        
        
        'Save and Close Workbook
          wb.Close SaveChanges:=True
          
        'Ensure Workbook has closed before moving on to next line of code
          DoEvents
    
        'Get next file name
          myFile = Dir
      Loop
    
    ResetSettings:
      'Reset Macro Optimization Settings
        Application.EnableEvents = True
        Application.Calculation = xlCalculationAutomatic
        Application.ScreenUpdating = True
    
    End Sub

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Excel VBA, Consolidate into Master Sheet

    It would be much easier to use Power Query...
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Excel VBA, Consolidate into Master Sheet

    Here's one approach, using Power Query. This uses a Parameters table to store the folder path (you could of course use VBA to edit this value, if you want to use the folder picker dialog)

    There are four queries.

    1. fnGetParameter
    (ParameterName as text) =>
    let
        Source = Excel.CurrentWorkbook(){[Name="tbParameters"]}[Content],
        Row = Table.SelectRows(Source, each ([Parameter] = ParameterName)),
        Value = if Table.IsEmpty(Row) = true
            then null
            else Record.Field(Row{0},"Value")
    in
        Value
    2. fnDataSheet1
    (filepath) =>
    let
        Source = Excel.Workbook(File.Contents(filepath), null, true),
        Sheet = Source{[Item="DataSheet1",Kind="Sheet"]}[Data],
        #"Transposed Table" = Table.Transpose(Sheet),
        #"Promoted Headers" = Table.PromoteHeaders(#"Transposed Table", [PromoteAllScalars=true])
    in
        #"Promoted Headers"
    3. fnDataSheet2
    (filepath) =>
    let
        Source = Excel.Workbook(File.Contents(filepath), null, true),
        Sheet = Source{[Item="DataSheet2",Kind="Sheet"]}[Data]
    in
        Sheet
    4. CombineAllFiles
    let
        Source = Folder.Files(fnGetParameter("Folder Path")),
        #"Merged Columns" = Table.CombineColumns(Source,{"Folder Path", "Name"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"FilePath"),
        #"Removed Other Columns" = Table.SelectColumns(#"Merged Columns",{"FilePath"}),
        #"Invoked Custom Function" = Table.AddColumn(#"Removed Other Columns", "Custom1", each fnDataSheet1([FilePath])),
        #"Invoked Custom Function1" = Table.AddColumn(#"Invoked Custom Function", "Custom2", each fnDataSheet2([FilePath])),
        #"Expanded Custom1" = Table.ExpandTableColumn(#"Invoked Custom Function1", "Custom1", {"Name", "Number", "Data"}, {"Name", "Number", "Data"}),
        #"Expanded Custom2" = Table.ExpandTableColumn(#"Expanded Custom1", "Custom2", {"Column1", "Column2", "Column3", "Column4", "Column5"}, {"Column1", "Column2", "Column3", "Column4", "Column5"}),
        #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom2",{"FilePath"})
    in
        #"Removed Columns"
    This will combine the data from all files in the folder specified in the parameters table, according to the specification in your first post. Now you can simply "Refresh" the data table, and it will combine the data from all source files.

    See attached file for worked example.
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Consolidate to Master sheet
    By kannan1847 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-23-2015, 12:52 AM
  2. Automatically consolidate data to one master sheet
    By piyush23y in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-26-2014, 01:11 PM
  3. [SOLVED] Consolidate Dynamic Range from two worksheets into one Master Sheet
    By Laylow in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 11-12-2013, 09:46 PM
  4. Unable to consolidate all sheet to master sheet
    By sbehera in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-03-2013, 02:48 PM
  5. [SOLVED] Consolidate Data from 1 Range on 1 Sheet in Multiple Workbooks to Master Workbook Sheet
    By Jennasis in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-10-2013, 06:11 AM
  6. Consolidate all the sheets into one master sheet
    By vignesh rocks in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-09-2013, 08:41 AM
  7. Replies: 0
    Last Post: 07-20-2012, 04:44 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1