Consulting

Results 1 to 6 of 6

Thread: Merge files in one folder to a master worksheet and update

  1. #1

    Merge files in one folder to a master worksheet and update

    Hello Everyone,

    I need help with pulling some data and was trying to figure out a more efficient way of doing it. I currently have a folder with about 20 workbooks from different agents. They go in and update a specific worksheet weekly. I'm trying to create a master worksheet that would merge all the specific worksheet into one file. I found one code that I've been using but I wasn't sure if there is a better way. I would like for the sheet to update if there has been any changes made to the agents worksheet. The problem I'm having with this code is that I can't reuse it for updating the master worksheet without having to delete that worksheet before running the module again.

    I'm not sure what I can add at the end that would allow me to maybe copy and all the date from the master to another worksheet and then delete the master sheet so I can rinse and re-use. Any help is greatly appreciated.


    Currently I'm using this code I found on this site:

    Sub Mergetracker()
    
    
    Dim MyPath As String
    Dim SourceRcount As Long, FNum As Long
    Dim mybook As Workbook, BaseWks As Worksheet
    Dim sourceRange As Range, destrange As Range
    Dim rnum As Long, CalcMode As Long
    Dim SaveDriveDir As String
    Dim FName As Variant
    
    
    'Change ScreenUpdating, Calculation and EnableEvents
    With Application
    CalcMode = .Calculation
    .Calculation = xlCalculationManual
    .ScreenUpdating = False
    .EnableEvents = False
    End With
    
    
    ' SaveDriveDir = CurDir
    ' ChDirNet "K:\Excel\Tracker"
    
    
    FName = Application.GetOpenFilename(filefilter:="Excel Files (*.xl*), *.xl*", _
    MultiSelect:=True)
    If IsArray(FName) Then
    'Add a new workbook with one sheet
    'Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
    Set BaseWks = Worksheets.Add
    BaseWks.Name = "Master"
    rnum = 2
    
    
    'Loop through all files in the array(myFiles)
    For FNum = LBound(FName) To UBound(FName)
    Set mybook = Nothing
    On Error Resume Next
    Set mybook = Workbooks.Open(FName(FNum))
    On Error GoTo 0
    If Not mybook Is Nothing Then
    On Error Resume Next
    With mybook.Worksheets("Tracker")
    .Unprotect
    LC = .Cells(.Rows.Count, "C").End(xlUp).Row
    Set sourceRange = .Range("A2:T" & LC)
    End With
    If Err.Number > 0 Then
    Err.Clear
    Set sourceRange = Nothing
    Else
    'if SourceRange use all columns then skip this file
    If sourceRange.Columns.Count >= BaseWks.Columns.Count Then
    Set sourceRange = Nothing
    End If
    End If
    On Error GoTo 0
    If Not sourceRange Is Nothing Then
    SourceRcount = sourceRange.Rows.Count
    If rnum + SourceRcount >= BaseWks.Rows.Count Then
    MsgBox "Sorry there are not enough rows in the sheet"
    BaseWks.Columns.AutoFit
    mybook.Close savechanges:=False
    GoTo ExitTheSub
    Else
    'Copy the file name in column A
    With sourceRange
    BaseWks.Cells(rnum, "A"). _
    Resize(.Rows.Count).Value = FName(FNum)
    End With
    'Set the destrange
    Set destrange = BaseWks.Range("B" & rnum)
    'we copy the values from the sourceRange to the destrange
    With sourceRange
    Set destrange = destrange. _
    Resize(.Rows.Count, .Columns.Count)
    End With
    destrange.Value = sourceRange.Value
    rnum = rnum + SourceRcount
    End If
    End If
    mybook.Close savechanges:=False
    End If
    Next FNum
    BaseWks.Columns.AutoFit
    End If
    ExitTheSub:
    
    
    'Restore ScreenUpdating, Calculation and EnableEvents
    With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .Calculation = CalcMode
    End With
    
    
    ' ChDirNet SaveDriveDir
    
    
    End Sub
    Last edited by Paul_Hossler; 10-31-2018 at 02:11 PM. Reason: Replace old vba tags with CODE tags

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    You can create a Querytable for every file in the 'integration' file.

    Sub M_snb()
      c00 = "G:\OF\"
      c01 = Dir(c00 & "consolidate*.xlsx")
      Do Until c01 = ""
        c02 = c02 & "|" & c01
        c01 = Dir
      Loop
      sn = Split(Mid(c02, 2), "|")
       
      For j = 0 To UBound(sn)
        c03 = Replace(sn(j), ".xlsx", "")
        If Evaluate("not(isref(" & c00 & "))") Then
          With Sheets.Add(, Sheets(Sheets.Count))
            .Name = c03
            .QueryTables.Add("ODBC;DSN=Excel_xlsb;DBQ=" & c00 & sn(j), .Range("A1"), "SELECT * FROM `Sheet1$`").Refresh False
          End With
        End If
      Next
    End Sub
    NB. Make sure the filepath nor the file name contains any spaces.

  3. #3
    Quote Originally Posted by snb View Post
    You can create a Querytable for every file in the 'integration' file.

    Sub M_snb()
      c00 = "G:\OF\"
      c01 = Dir(c00 & "consolidate*.xlsx")
      Do Until c01 = ""
        c02 = c02 & "|" & c01
        c01 = Dir
      Loop
      sn = Split(Mid(c02, 2), "|")
       
      For j = 0 To UBound(sn)
        c03 = Replace(sn(j), ".xlsx", "")
        If Evaluate("not(isref(" & c00 & "))") Then
          With Sheets.Add(, Sheets(Sheets.Count))
            .Name = c03
            .QueryTables.Add("ODBC;DSN=Excel_xlsb;DBQ=" & c00 & sn(j), .Range("A1"), "SELECT * FROM `Sheet1$`").Refresh False
          End With
        End If
      Next
    End Sub
    NB. Make sure the filepath nor the file name contains any spaces.
    Thank you, Where would I put this code and what changes should I make?

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Why quoting a post that's just above ?

  5. #5
    Another, perhaps easier method, is to use Data, New Query, From File, From Folder (assuming this is Excel 2016)
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

  6. #6
    Master.xlsmMaster.xlsm
    Quote Originally Posted by Jan Karel Pieterse View Post
    Another, perhaps easier method, is to use Data, New Query, From File, From Folder (assuming this is Excel 2016)
    I tried this but it didn't return the desired result. I might not have explained it that well in my original post. The VBA I have above works great it was just missing a few things. the code originally just merged all workbooks into one but removed the header from the table, I was able to tweak the above code and to do exactly what I needed by creating a worksheet that had an empty table with the header I needed and added to the code to copy the generated "DATA" sheet to the "Master" sheet and then delete the "DATA" worksheet so I can re-run whenever to update.
    I would like to know if it's possible for the file name that is returned to be shortened? Currently it returns "C:\Excel\Files\Test 1.xlsm" and would prefer just "Test 1". I've attached a test file. If there is a more efficient way of accomplishing all this, I would like to know. Thank you

    Sub Mergetracker()
    
    
    
    
    Dim MyPath As String
    Dim SourceRcount As Long, FNum As Long
    Dim mybook As Workbook, BaseWks As Worksheet
    Dim sourceRange As Range, destrange As Range
    Dim rnum As Long, CalcMode As Long
    Dim SaveDriveDir As String
    Dim FName As Variant
    
    
    
    
    'Change ScreenUpdating, Calculation and EnableEvents
    With Application
    CalcMode = .Calculation
    .Calculation = xlCalculationManual
    .ScreenUpdating = False
    .EnableEvents = False
    End With
    
    
    
    
    ' SaveDriveDir = CurDir
    ' ChDirNet "C:\Excel\"
    
    
    
    
    FName = Application.GetOpenFilename(filefilter:="Excel Files (*.xl*), *.xl*", _
    MultiSelect:=True)
    If IsArray(FName) Then
    'Add a new workbook with one sheet
    'Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
    Set BaseWks = Worksheets.Add
    BaseWks.Name = "Data"
    rnum = 2
    
    
    
    
    'Loop through all files in the array(myFiles)
    For FNum = LBound(FName) To UBound(FName)
    Set mybook = Nothing
    On Error Resume Next
    Set mybook = Workbooks.Open(FName(FNum))
    On Error GoTo 0
    If Not mybook Is Nothing Then
    On Error Resume Next
    With mybook.Worksheets("Tracker")
    .Unprotect
    LC = .Cells(.Rows.Count, "C").End(xlUp).Row
    Set sourceRange = .Range("A2:T" & LC)
    End With
    If Err.Number > 0 Then
    Err.Clear
    Set sourceRange = Nothing
    Else
    'if SourceRange use all columns then skip this file
    If sourceRange.Columns.Count >= BaseWks.Columns.Count Then
    Set sourceRange = Nothing
    End If
    End If
    On Error GoTo 0
    If Not sourceRange Is Nothing Then
    SourceRcount = sourceRange.Rows.Count
    If rnum + SourceRcount >= BaseWks.Rows.Count Then
    MsgBox "Sorry there are not enough rows in the sheet"
    BaseWks.Columns.AutoFit
    mybook.Close savechanges:=False
    GoTo ExitTheSub
    Else
    'Copy the file name in column A
    With sourceRange
    BaseWks.Cells(rnum, "A"). _
    Resize(.Rows.Count).Value = FName(FNum)
    End With
    'Set the destrange
    Set destrange = BaseWks.Range("B" & rnum)
    'we copy the values from the sourceRange to the destrange
    With sourceRange
    Set destrange = destrange. _
    Resize(.Rows.Count, .Columns.Count)
    End With
    destrange.Value = sourceRange.Value
    rnum = rnum + SourceRcount
    End If
    End If
    mybook.Close savechanges:=False
    End If
    Next FNum
    BaseWks.Columns.AutoFit
    End If
    ExitTheSub:
    
    
    
    
    'Restore ScreenUpdating, Calculation and EnableEvents
    With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .Calculation = CalcMode
    End With
    
    
    
    
    ' ChDirNet SaveDriveDir
    
    
    'Copy merged data to master sheet
    Sheets("Data").Range("A2:I9999").Copy Destination:=Sheets("Master").Range("A2")
    
    
    'Delete Data sheet
    Application.DisplayAlerts = False
    Worksheets("Data").Delete
    Application.DisplayAlerts = True
    
    
    'Delete empty rows
    Dim iCounter As Long
    With Application
    .Calculation = xlCalculationManual
    .ScreenUpdating = False
    For iCounter = Selection.Rows.Count To 1 Step -1
    If WorksheetFunction.CountA(Selection.Rows(iCounter)) = 0 Then
    Selection.Rows(iCounter).EntireRow.Delete
    End If
    
    
    Next iCounter
    .Calculation = xlCalculationAutomatic
    .ScreenUpdating = True
    End With
    
    
    
    
    End Sub

Posting Permissions

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