[VBA] [Data Consolidation][Speed up]

zeromax1

Board Regular
Joined
Mar 20, 2020
Messages
52
Office Version
  1. 365
Platform
  1. Windows
Hi Professional, I would like to ask the help for the VBA code speed up & improvement.

I have to consolidate the sub-workbooks to my master workbook. Each workbooks have the same tab name to contain the data.

Eg: I have to copy the data in column F from "Monday" Tab in sub workbooks to my master workbook in the column B of "Monday" Tab and subtract the number.
I have to copy the data in column C from "Monday" Tab in sub workbooks to my master workbook in the column F of "Monday" Tab.

1234567890.jpg
I found a VBA code to consolidate the data, but the speed seems too slow and the code is not perfect.

For the convenient, I skip to paste the similar code of "Tuesday","Wednesday", etc tab.

VBA Code:
Sub Consolidatfiles()

On Error Resume Next

    Application.ScreenUpdating = False

    Dim FileName As Variant, wkbSource As Workbook, wkbDest As Workbook
    Set wkbDest = ThisWorkbook
    
    FileName = Application.GetOpenFilename("Excel Files (*.xlsx*),*.xlsx*", , "Select Excel Files", , True)
    
    If Not IsArray(FileName) Then
        MsgBox "No File Selected"
        Exit Sub
    End If
    
    For i = LBound(FileName) To UBound(FileName)
        Set wkbSource = Workbooks.Open(FileName(i))
        With wkbSource
            .Worksheets("Monday").Range("C5:C" & Range("C" & Rows.Count).End(xlUp).Row).Copy _
            Destination:=wkbDest.Worksheets("Monday").range("F7").End(xlUp)

            .Worksheets("Monday").Range("F5:F" & Range("F" & Rows.Count).End(xlUp).Row-1).Copy _
            Destination:=wkbDest.Worksheets("Monday").range("F7").End(xlUp).PasteSpecial Paste:=xlPasteAll, Operation:=xlSubtract 

            wkbSource.Close savechanges = False
        End With
    Next i
    
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
  
    
End Sub

Thank you very much.
 
I'm glad to help you. Thanks for the feedback.
Hi DanteAmor, I would like to modify the code to copy a range data from A2 to Z & lastrow of a another workbook to the existing workbook. Would you give a help for this question?

Just copy the data to the existing workbook with no change. The data include number, date, text etc.

1605063224301.png


VBA Code:
Sub Consolidatfiles()
  Dim wb1 As Workbook, sh1 As Worksheet
  Dim FileName As Variant, sheetName As Variant, arr As Variant
  Dim i As Long, j As Long, lr1 As Long, lr2 As Long
  Dim a() As Variant, b() As Variant
  
  Application.ScreenUpdating = False
  
  FileName = Application.GetOpenFilename("Excel Files (*.xlsx*),*.xlsx*", , "Select Excel Files", , True)
  
  If Not IsArray(FileName) Then
    MsgBox "No File Selected"
    Exit Sub
  End If
  
  arr = Array("Matched")
  
  For i = LBound(FileName) To UBound(FileName)
    Set wb1 = Workbooks.Open(FileName(i))
    
    For Each sheetName In arr
      Set sh1 = wb1.Sheets(sheetName)
      Erase a
      lr1 = sh1.Range("A2:Z" & Rows.Count).End(3).Row

      
      With ThisWorkbook.Sheets(sheetName)
        lr2 = .Range("A" & Rows.Count).End(3).Row + 1
        .Range("A" & lr2).Resize(UBound(a)).Value = sh1.Range("A2").Resize(UBound(a)).Value
      End With
    Next sheetName
    wb1.Close False
  Next i
  
  Application.ScreenUpdating = True
End Sub
 
Upvote 0

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hi DanteAmor, I would like to modify the code to copy a range data from A2 to Z & lastrow of a another workbook to the existing workbook. Would you give a help for this question?

Just copy the data to the existing workbook with no change. The data include number, date, text etc.
I updated the code by below:

VBA Code:
If Not IsArray(FileName) Then
        MsgBox "No File Selected"
        Exit Sub
    End If
     
    For i = LBound(FileName) To UBound(FileName)
        Set wkbSource = Workbooks.Open(FileName(i))

        With ThisWorkbook

            .Worksheets(1).Range("A4:Z" & Range("C" & Rows.Count).End(xlUp).Row).Value = _
            wkbSource.Worksheets(1).Range("A2:Z" & Range("C" & Rows.Count).End(xlUp).Row).Value
            
            wkbSource.Close savechanges = False
            
        End With
    Next i
 
Upvote 0

Forum statistics

Threads
1,214,548
Messages
6,120,146
Members
448,948
Latest member
spamiki

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