Speeding up a macro

katlong

New Member
Joined
Sep 6, 2018
Messages
9
Hello, I'm self-taught with VBA coding and always looking to learn. I wrote this macro which finally works. I've done some of the basic tips for optimization, but I'm wondering if anyone has any additional suggestions for making it faster. It is possible that the macro has to open and close nearly 450 files to retrieve the data, although most of the time it has less to open and close. I have several notes in the code to myself (sorry). Please note that the macro has to work on Excel 2016 and 2013. Any help is appreciated:

Code:
Sub Promise_Date_Change_Detector()
'Turn off screen updating once macro is complete to speed up processing time
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.DisplayStatusBar = False
Dim WB As Workbook
Dim WS As Worksheet
Dim c As Integer
Dim StrtFile As Variant
Dim fso As New Scripting.FileSystemObject
Dim ian As String
Dim fileSpec As File
Dim strInfo As String
Dim wsLR As Long
Set fldr = fso.GetFolder("U:\Public\All Customer Reports\All Customers Report 2018")
'Label Column Headers , "PO#", "Sales Order", "Line #", "Promise Date","file date"
Cells(5, 1).Value = "PO#"
Cells(5, 2).Value = "Sales Order"
Cells(5, 3).Value = "Line #"
Cells(5, 4).Value = "Promise Date"
Cells(5, 5).Value = "Customer Order File"
'Build Input Box to Inquire as to PO in Question
PurchaseOrder = InputBox("What PO would you like information for?", "PO#?")
'Find Order Date in Latest file
'Retrieve file path of latest file from this workbook
Lfile = Worksheets(1).Cells(2, 6).Value
Workbooks.Open Filename:=Lfile, ReadOnly:=True
'Loop through the rows in latest file to find order date
k = Worksheets(1).Cells(Rows.Count, 1).End(xlUp).Row
For datfind = 2 To k
If Worksheets(1).Cells(datfind, 2).Value = PurchaseOrder Then
N = Worksheets(1).Cells(datfind, 14).Value
End If
Next datfind
'reformat the date to match the file names
N = Format(N, "mm-dd-yyyy")
'create the starting file file path
' Original statement doesn't work with Excel 2013
' StrtFile = Application.WorksheetFunction.Concat(fldr, "", N, " am", ".xls")
'
' This statement works
'
StrtFile = N & " am.xls"
'Loop through all the files in the customers report folder and pull out the po info
'find current bottom row of this work book
x = ThisWorkbook.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row + 1
'Set up a counter for the number of files in the folder
countfile = 0
For Each wbfile In fldr.Files
      countfile = countfile + 1
Next wbfile
'Determine the number of the file in question with respect to the total number of files
howmany = 0
For Each wbfile In fldr.Files
    howmany = howmany + 1
    If wbfile.Name = StrtFile Then
        J = howmany
    End If
Next wbfile
'Loop from starting file to the last file searching for the PO data and copy/paste that data to this workbook
c = 0
Z = 6
For Each wbfile In fldr.Files
    c = c + 1
    If c >= J Then
        If c <= countfile - 1 Then
            Application.EnableEvents = False
            Set fileSpec = fso.GetFile(wbfile)
            ian = fileSpec.Name
            ian = fldr & "" & ian
            If fso.GetExtensionName(ian) = "xls" Then
                Workbooks.Open Filename:=ian, ReadOnly:=True
                Set WS = Worksheets(1)
                Sheets("Foglio1").Activate
                With ActiveSheet
                    wsLR = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row
                End With
                For Y = 2 To wsLR
                    If WS.Cells(Y, 2).Value = PurchaseOrder Then
                        WS.Cells(Y, 2).Copy Destination:=ThisWorkbook.Worksheets(1).Cells(Z, 1)
                        WS.Cells(Y, 3).Copy Destination:=ThisWorkbook.Worksheets(1).Cells(Z, 2)
                        WS.Cells(Y, 4).Copy Destination:=ThisWorkbook.Worksheets(1).Cells(Z, 3)
                        WS.Cells(Y, 21).Copy Destination:=ThisWorkbook.Worksheets(1).Cells(Z, 4)
                        ThisWorkbook.Worksheets(1).Cells(Z, 5) = ian
                        Z = Z + 1
                    End If
                Next Y
                ActiveWorkbook.Close
            End If
        End If
    End If
Application.EnableEvents = True
Next wbfile
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.DisplayStatusBar = True
End Sub
 
Last edited by a moderator:

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hello, I'm self-taught with VBA coding and always looking to learn. I wrote this macro which finally works. I've done some of the basic tips for optimization, but I'm wondering if anyone has any additional suggestions for making it faster. It is possible that the macro has to open and close nearly 450 files to retrieve the data, although most of the time it has less to open and close. I have several notes in the code to myself (sorry). Please note that the macro has to work on Excel 2016 and 2013. Any help is appreciated:

Code:
Sub Promise_Date_Change_Detector()
'Turn off screen updating once macro is complete to speed up processing time
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.DisplayStatusBar = False
Dim WB As Workbook
Dim WS As Worksheet
Dim c As Integer
Dim StrtFile As Variant
Dim fso As New Scripting.FileSystemObject
Dim ian As String
Dim fileSpec As File
Dim strInfo As String
Dim wsLR As Long
Set fldr = fso.GetFolder("U:\Public\All Customer Reports\All Customers Report 2018")
'Label Column Headers , "PO#", "Sales Order", "Line #", "Promise Date","file date"
Cells(5, 1).Value = "PO#"
Cells(5, 2).Value = "Sales Order"
Cells(5, 3).Value = "Line #"
Cells(5, 4).Value = "Promise Date"
Cells(5, 5).Value = "Customer Order File"
'Build Input Box to Inquire as to PO in Question
PurchaseOrder = InputBox("What PO would you like information for?", "PO#?")
'Find Order Date in Latest file
'Retrieve file path of latest file from this workbook
Lfile = Worksheets(1).Cells(2, 6).Value
Workbooks.Open Filename:=Lfile, ReadOnly:=True
'Loop through the rows in latest file to find order date
k = Worksheets(1).Cells(Rows.Count, 1).End(xlUp).Row
For datfind = 2 To k
If Worksheets(1).Cells(datfind, 2).Value = PurchaseOrder Then
N = Worksheets(1).Cells(datfind, 14).Value
End If
Next datfind
'reformat the date to match the file names
N = Format(N, "mm-dd-yyyy")
'create the starting file file path
' Original statement doesn't work with Excel 2013
' StrtFile = Application.WorksheetFunction.Concat(fldr, "", N, " am", ".xls")
'
' This statement works
'
StrtFile = N & " am.xls"
'Loop through all the files in the customers report folder and pull out the po info
'find current bottom row of this work book
x = ThisWorkbook.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row + 1
'Set up a counter for the number of files in the folder
countfile = 0
For Each wbfile In fldr.Files
      countfile = countfile + 1
Next wbfile
'Determine the number of the file in question with respect to the total number of files
howmany = 0
For Each wbfile In fldr.Files
    howmany = howmany + 1
    If wbfile.Name = StrtFile Then
        J = howmany
    End If
Next wbfile
'Loop from starting file to the last file searching for the PO data and copy/paste that data to this workbook
c = 0
Z = 6
For Each wbfile In fldr.Files
    c = c + 1
    If c >= J Then
        If c <= countfile - 1 Then
            Application.EnableEvents = False
            Set fileSpec = fso.GetFile(wbfile)
            ian = fileSpec.Name
            ian = fldr & "" & ian
            If fso.GetExtensionName(ian) = "xls" Then
                Workbooks.Open Filename:=ian, ReadOnly:=True
                Set WS = Worksheets(1)
                Sheets("Foglio1").Activate
                With ActiveSheet
                    wsLR = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row
                End With
                For Y = 2 To wsLR
                    If WS.Cells(Y, 2).Value = PurchaseOrder Then
                        WS.Cells(Y, 2).Copy Destination:=ThisWorkbook.Worksheets(1).Cells(Z, 1)
                        WS.Cells(Y, 3).Copy Destination:=ThisWorkbook.Worksheets(1).Cells(Z, 2)
                        WS.Cells(Y, 4).Copy Destination:=ThisWorkbook.Worksheets(1).Cells(Z, 3)
                        WS.Cells(Y, 21).Copy Destination:=ThisWorkbook.Worksheets(1).Cells(Z, 4)
                        ThisWorkbook.Worksheets(1).Cells(Z, 5) = ian
                        Z = Z + 1
                    End If
                Next Y
                ActiveWorkbook.Close
            End If
        End If
    End If
Application.EnableEvents = True
Next wbfile
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.DisplayStatusBar = True
End Sub

One quick and easy suggestion is to replace the loops that search of the PO to autofilter instead. It would look something like this.

Code:
Worksheets(1).Columns(2).AutoFilter Field:=1, Criteria1:=PurchaseOrder
If Worksheets(1).UsedRange.Offset(1).SpecialCells(xlCellTypeVisible).Cells(, 1).Value = PurchaseOrder Then
N = Worksheets(1).UsedRange.Offset(1).SpecialCells(xlCellTypeVisible).Cells(, 13).Value
End If
ActiveSheet.AutoFilterMode = "False"

You might need to adjust the column selection depending on your individual spreadsheet, but this should be much faster than looping through every sheet you open to search of the PO.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

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