Macro to speedy up process when copying Data

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,603
Office Version
  1. 2021
Platform
  1. Windows
I have the following code below which is rather slow when copying the data from source sheet

The code works, but is slow


Code:
 Sub Open_workbook()
    ChDir "C:\Sales Ledgers"
    Dim LR As Long
    With Application
        .DisplayAlerts = False
        .ScreenUpdating = False
        .AskToUpdateLinks = False
        .Calculation = xlCalculationManual
        .CutCopyMode = False
    End With
    
    With Sheets("Imported Data")
        LR = .Cells(.Rows.Count, "A").End(xlUp).Row
        .Range("A1:AD" & LR).ClearContents
    End With
    
    Dim fDialog As FileDialog
    Dim varFile As Variant
    Dim nb As Workbook, tw As Workbook, ts As Worksheet
    
    Set tw = ThisWorkbook
    Set ts = tw.ActiveSheet
    
    Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
    With fDialog
        .Title = "Select Excel Files"
        .Filters.Clear
        .Filters.Add "Excel files", "*.xlsm"
        .InitialFileName = "C:\Sales Ledgers\BR1*.xlsm"
        
        If .Show = -1 Then ' User clicked OK
            For Each varFile In .SelectedItems
                Set nb = Workbooks.Open(Filename:=varFile, Local:=True)
                With nb.Sheets(3)
                    .Range("A1:AD2000").Copy
                    tw.Sheets("Imported Data").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
                    tw.Sheets("Imported Data").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial xlPasteFormats
                End With
                nb.Close False
            Next varFile
        End If
    End With
    
    With Application
        .ScreenUpdating = True
        .Calculation = xlCalculationAutomatic
        .CutCopyMode = True
        .DisplayAlerts = True
        .AskToUpdateLinks = True
    End With
    

End Sub

It would be appreciated if someone could amend code to optimise process
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Untested here.

Try replacing these two lines ;

Code:
tw.Sheets("Imported Data").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
                    tw.Sheets("Imported Data").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial xlPasteFormats

With this single line :

Code:
tw.Sheets("Imported Data").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial xlPasteAllUsingSourceTheme
 
Upvote 0
Solution

Forum statistics

Threads
1,224,814
Messages
6,181,126
Members
453,021
Latest member
Justyna P

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