I wrote a macro that is taking 20+ minutes to run on other computers, but when I run it on mine it only takes 5 mins. It is a larger macro and I am new to the VBA coding world. I was wondering if I could condense it down so that it can run faster not only for my computer but other computers too.
The purpose of my macro is to take multiple sheets in one workbook and consolidate them into one sheet in the same workbook that I titled as "Consolidated". A problem with the Excel file is that some columns are left blank or the title of the columns are different from other sheets that I am copying and pasting to the master sheet, so I added an iferror
to sheets that have blank cells so that I can use the end down function in the final part of the macro where I copy each column from each sheet into the master consolidation sheet. If you need to explain better please let me know.
Sub Macro1()
Dim i As Integer
Dim r As Long, c As Long
Application.ScreenUpdating = False
Sheets("CIP Summary").Select
Sheets.Add
ActiveSheet.Name = "Consolidated"
ActiveCell.FormulaR1C1 = "Company"
Range("B1").Select
ActiveCell.FormulaR1C1 = "Location"
Range("C1").Select
ActiveCell.FormulaR1C1 = "Store"
Range("D1").Select
ActiveCell.FormulaR1C1 = "RCT/Voucher"
Range("E1").Select
ActiveCell.FormulaR1C1 = "Vendor"
Range("F1").Select
ActiveCell.FormulaR1C1 = "Vendor Name"
Range("G1").Select
ActiveCell.FormulaR1C1 = "Date"
Range("H1").Select
ActiveCell.FormulaR1C1 = "Reference"
Range("I1").Select
ActiveCell.FormulaR1C1 = "Amount"
Range("J1").Select
ActiveCell.FormulaR1C1 = "Period"
Range("K1").Select
ActiveCell.FormulaR1C1 = "JE"
Range("L1").Select
ActiveCell.FormulaR1C1 = "Project"
Range("M1").Select
ActiveCell.FormulaR1C1 = "Expected Open Date"
Range("N1").Select
ActiveCell.FormulaR1C1 = "Comment"
Range("N1").Select
Range(Selection, Selection.End(xlToLeft)).Select
Selection.Font.Bold = True
For i = 15 To Worksheets.Count
For c = 1 To 14
For r = 5 To 1000
If IsError(Sheets(i).Cells(r, c)) Then
Sheets(i).Cells(r, c).Value = "N/A"
ElseIf Sheets(i).Cells(r, c) = "" Then
Sheets(i).Cells(r, c).Value = "N/A"
End If
Next r
Next c
Next i
Dim xWs As Worksheet
Dim Rng As Range
Dim lastRow As String
Dim myPath As String
'company
Sheets(15).Select
Set Cell = Range("A1:N4").Find("Company", LookAt:=xlPart)
Cell.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Worksheets("Consolidated").Select
Range("A1").Select
Range(Selection, Cells(Rows.Count, Selection.Column).End(xlDown)).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
For i = 16 To Worksheets.Count
Sheets(i).Select
Set Cell = Range("A1:N5").Find("Company", LookAt:=xlPart)
Cell.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Worksheets("Consolidated").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
Next i
'location
Sheets(15).Select
Set Cell = Range("A1:N4").Find("location", LookAt:=xlPart)
Cell.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Worksheets("Consolidated").Select
Range("B1").Select
Range(Selection, Cells(Rows.Count, Selection.Column).End(xlDown)).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
For i = 16 To Worksheets.Count
Sheets(i).Select
Set Cell = Range("A1:N5").Find("location", LookAt:=xlPart)
Cell.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Worksheets("Consolidated").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
Next i
'Store
Sheets(15).Select
Set Cell = Range("A1:N4").Find("store", LookAt:=xlPart)
Cell.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Worksheets("Consolidated").Select
Range("C1").Select
Range(Selection, Cells(Rows.Count, Selection.Column).End(xlDown)).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
For i = 16 To Worksheets.Count
Sheets(i).Select
Set Cell = Range("A1:N5").Find("store", LookAt:=xlPart)
Cell.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Worksheets("Consolidated").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
Next i
'RCT
Sheets(15).Select
Set Cell = Range("A1:N4").Find("RCT", LookAt:=xlPart)
Cell.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Worksheets("Consolidated").Select
Range("D1").Select
Range(Selection, Cells(Rows.Count, Selection.Column).End(xlDown)).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
For i = 16 To Worksheets.Count
Sheets(i).Select
Set Cell = Range("A1:N5").Find("RCT", LookAt:=xlPart)
Cell.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Worksheets("Consolidated").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
Next i
'Vendor
Sheets(15).Select
Set Cell = Range("A1:N4").Find("Vendor", LookAt:=xlPart)
Cell.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Worksheets("Consolidated").Select
Range("E1").Select
Range(Selection, Cells(Rows.Count, Selection.Column).End(xlDown)).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
For i = 16 To Worksheets.Count
Sheets(i).Select
Set Cell = Range("A1:N5").Find("Vendor", LookAt:=xlPart)
Cell.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Worksheets("Consolidated").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
Next i
'Vendor Name
Sheets(15).Select
Set Cell = Range("A1:N4").Find("Vendor Name", LookAt:=xlPart)
Cell.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Worksheets("Consolidated").Select
Range("F1").Select
Range(Selection, Cells(Rows.Count, Selection.Column).End(xlDown)).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
For i = 16 To Worksheets.Count
Sheets(i).Select
Set Cell = Range("A1:N5").Find("Vendor Name", LookAt:=xlPart)
Cell.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Worksheets("Consolidated").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
Next i
'Date
Sheets(15).Select
Set Cell = Range("A1:N4").Find("date", LookAt:=xlPart)
Cell.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Worksheets("Consolidated").Select
Range("g1").Select
Range(Selection, Cells(Rows.Count, Selection.Column).End(xlDown)).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
For i = 16 To Worksheets.Count
Sheets(i).Select
Set Cell = Range("A1:N5").Find("date", LookAt:=xlPart)
Cell.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Worksheets("Consolidated").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
Next i
'Reference
Sheets(15).Select
Set Cell = Range("A1:N4").Find("reference", LookAt:=xlPart)
Cell.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Worksheets("Consolidated").Select
Range("H1").Select
Range(Selection, Cells(Rows.Count, Selection.Column).End(xlDown)).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
For i = 16 To Worksheets.Count
Sheets(i).Select
Set Cell = Range("A1:N5").Find("reference", LookAt:=xlPart)
Cell.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Worksheets("Consolidated").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
Next i
'amount
Sheets(15).Select
Set Cell = Range("A1:N4").Find("amount", LookAt:=xlPart)
Cell.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Worksheets("Consolidated").Select
Range("I1").Select
Range(Selection, Cells(Rows.Count, Selection.Column).End(xlDown)).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
For i = 16 To Worksheets.Count
Sheets(i).Select
Set Cell = Range("A1:N5").Find("amount", LookAt:=xlPart)
Cell.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Worksheets("Consolidated").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
Next i
'period
Sheets(15).Select
Set Cell = Range("A1:N4").Find("period", LookAt:=xlPart)
Cell.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Worksheets("Consolidated").Select
Range("J1").Select
Range(Selection, Cells(Rows.Count, Selection.Column).End(xlDown)).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
For i = 16 To Worksheets.Count
Sheets(i).Select
Set Cell = Range("A1:N5").Find("period", LookAt:=xlPart)
Cell.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Worksheets("Consolidated").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
Next i
'JE
Sheets(15).Select
Set Cell = Range("A1:N4").Find("JE", LookAt:=xlPart)
Cell.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Worksheets("Consolidated").Select
Range("K1").Select
Range(Selection, Cells(Rows.Count, Selection.Column).End(xlDown)).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
For i = 16 To Worksheets.Count
Sheets(i).Select
Set Cell = Range("A1:N5").Find("JE", LookAt:=xlPart)
Cell.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Worksheets("Consolidated").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
Next i
'project
Sheets(15).Select
Set Cell = Range("A1:N4").Find("Project", LookAt:=xlPart)
Cell.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Worksheets("Consolidated").Select
Range("L1").Select
Range(Selection, Cells(Rows.Count, Selection.Column).End(xlDown)).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
For i = 16 To Worksheets.Count
Sheets(i).Select
Set Cell = Range("A1:N5").Find("project", LookAt:=xlPart)
Cell.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Worksheets("Consolidated").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
Next i
'expected open date
Sheets(15).Select
Set Cell = Range("A1:N4").Find("expected", LookAt:=xlPart)
Cell.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Worksheets("Consolidated").Select
Range("M1").Select
Range(Selection, Cells(Rows.Count, Selection.Column).End(xlDown)).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
For i = 16 To Worksheets.Count
Sheets(i).Select
Set Cell = Range("A1:N5").Find("expected", LookAt:=xlPart)
Cell.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Worksheets("Consolidated").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
Next i
'comment
Sheets(15).Select
Set Cell = Range("A1:N4").Find("comment", LookAt:=xlPart)
Cell.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Worksheets("Consolidated").Select
Range("N1").Select
Range(Selection, Cells(Rows.Count, Selection.Column).End(xlDown)).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
For i = 16 To Worksheets.Count
Sheets(i).Select
Set Cell = Range("A1:N5").Find("comment", LookAt:=xlPart)
Cell.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Worksheets("Consolidated").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
Next i
End Sub