VBA is there a way to speed up?

BenGee

Board Regular
Joined
Mar 5, 2016
Messages
196
Hi

My code below loops through 30+ workbooks saved in a server folder, and copy/pastes all data within each into a corresponding worksheet (each workbook has a matching worksheet name). It works but takes a bit of time.

Is there a way to improve the speed in which it executes? For example, avoiding selecting a range, copying and pasting? Any ideas or suggestions please;

My code;
VBA Code:
Dim ws As Worksheet
Dim PathOfWorkbboks
Dim objFolder As Object
Dim objFile As Object
Dim Main
Dim ShtName, objName

Main = "Group reporting.xlsm"
         
Windows(Main).Activate
PathOfWorkbboks = "[REDACTED]"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(PathOfWorkbboks)
 
    For x = 1 To Sheets.Count
        With Sheets(x)
        Sheets(x).Activate
        ShtName = Mid(Sheets(x).Name, 1) & ".xlsx"
            For Each objFile In objFolder.Files
            objName = objFSO.Getfilename(objFile.Path)
            If objName = ShtName Then
               Workbooks.Open objFile
               Sheets("sheet1").Select
               Cells.Select
               Selection.Copy
               Windows(Main).Activate
               Range("A1:Q50").Select
               ActiveSheet.Paste
               Application.CutCopyMode = False
               Workbooks(objName).Close savechanges:=False
            End If
            Next
        End With
    Next x
    
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.StatusBar = True

Thanks in advance
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
You might try something like:

VBA Code:
Dim wbMain as Workbook
set wbMain = Workbooks("Group reporting.xlsm") ' use ThisWorkbook if it's the one with the code in
         
PathOfWorkbboks = "[REDACTED]"

    For x = 1 To wbMain.Sheets.Count
        With wbMain.Sheets(x)

        ShtName = Mid(.Name, 1) & ".xlsx"
        Dim filePath as string
        filepath = PathOfWorkbboks & shtname
            If Dir(filepath) <> vbnullstring then
            dim wb as workbook
              set wb = Workbooks.Open(filepath)
               wb.Sheets("sheet1").usedrange.Copy Destination:=.Range("A1")
               wb.Close savechanges:=False
            End If
        End With
    Next x
 
Upvote 0
Solution
Does this work any better?

VBA Code:
Sub CopyAndSuch()
    Dim wb As Workbook
    Dim wbSource As Workbook
    Dim ws As Worksheet
    Dim PathOfWorkbboks
    Dim objFSO As Object
    Dim objFolder As Object
    Dim objFile As Object
    Dim Main As String
    Dim ShtName As String
    Dim objName As Variant
    
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    Main = "Group reporting.xlsm"
    Set wb = Workbooks(Main)
    
    PathOfWorkbboks = "[REDACTED]"
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(PathOfWorkbboks)

    For Each ws In wb.Worksheets
        With ws
            'ws.Activate
            ShtName = Mid(ws.Name, 1) & ".xlsx"
            For Each objFile In objFolder.Files
                objName = objFSO.Getfilename(objFile.Path)
                If objName = ShtName Then
                    Set wbSource = Workbooks.Open(objFile)
                    wbSource.Sheets("sheet1").UsedRange.Cells.Copy
                    ws.Range("A1:Q50").Paste
                    Application.CutCopyMode = False
                    Workbooks(objName).Close savechanges:=False
                End If
            Next
        End With
    Next x

    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
End Sub
 
Upvote 0
Thank you both for your suggestions.

RoryA, your solution has saved me some time. Thank you!
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,212
Members
452,618
Latest member
Tam84

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