I am working with a list reconciliation tool that was put together on Windows 7 using Excel 2003. It works well for small amounts of data, however I would like it to be able to run through about 10,000 rows of data without slowing down significantly or even freezing up. Reviewing the macro VBL, I can tell most of it was coded by recording the macro resulting in a lot of unnecessary lines of code that ideally could be condensed/looped into just a few lines (with the same output) . I was wondering if someone could review these macros keeping in mind utilizing loops and logic and suggest ways to help condense the code to make them run faster. This code gives an output of each entry in the two lists along with the number of times each occurs.
This is the macro for finding repeats with an output of "digit (x times)":
This is the macro that reconciles the two lists:
Finally, here is what the tool looks like:
[/IMG]
This is the macro for finding repeats with an output of "digit (x times)":
Code:
Sub ListRepeats()
Dim Rng16 As Range
Dim Rng18 As Range
Dim Rng19 As Range
LastRow = Cells(Rows.Count, "B").End(xlUp).Row
LastRow2 = Cells(Rows.Count, "C").End(xlUp).Row
Set Rng16 = Range("T2:T" & LastRow)
Set Rng18 = Range("AA2:AA" & LastRow2)
Set Rng19 = Range("V2:V" & LastRow2)
Application.ScreenUpdating = False
ActiveSheet.Unprotect Password:="password"
Sheets("Sheet2").Unprotect Password:="password"
Sheets("Sheet3").Unprotect Password:="password"
Columns("D:R").Select
Selection.EntireColumn.Hidden = True
Range("W1").FormulaR1C1 = "Part Number"
Range("W:X").ClearContents
LastRowB = Range("B" & Rows.Count).End(xlUp).Row
Range("B2:B" & LastRowB).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("W2"), Unique:=True
LastRowF = Range("W" & Rows.Count).End(xlUp).Row
With Range("W2:W" & LastRowF)
.Sort Key1:=Range("W2"), Order1:=xlAscending, Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
With .Offset(, 1)
.FormulaR1C1 = "=COUNTIF(R2C2:R" & LastRowB & "C2,RC[-1])"
.Value = .Value
.Cells(0).Value = "Occurrences"
End With
.Resize(, 2).EntireColumn.AutoFit
End With
Range("Y:Z").ClearContents
LastRowE = Range("C" & Rows.Count).End(xlUp).Row
Range("C2:C" & LastRowE).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("Y2"), Unique:=True
LastRowC = Range("Y" & Rows.Count).End(xlUp).Row
With Range("Y2:Y" & LastRowC)
.Sort Key1:=Range("Y2"), Order1:=xlAscending, Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
With .Offset(, 1)
.FormulaR1C1 = "=Countif(R2C2:R" & LastRowE & "C2, RC[-1])"
.Value = .Value
.Cells(0).Value = "Occurences"
End With
.Resize(, 2).EntireColumn.AutoFit
End With
ActiveWorkbook.Names.Add Name:="List1Repeats", RefersToR1C1:= _
"=OFFSET(Sheet1!R2C29, 0, 0, COUNTA(Sheet1!C29), 1)"
ActiveWorkbook.Names.Add Name:="List1Repeats", RefersToR1C1:= _
"=OFFSET(Sheet1!R2C29, 0, 0, COUNTA(Sheet1!C29), 1)"
ActiveWorkbook.Names.Add Name:="List2Repeats", RefersToR1C1:= _
"=OFFSET(Sheet1!R2C27, 0, 0, COUNTA(Sheet1!C27), 1)"
ActiveWorkbook.Names.Add Name:="List2Repeats", RefersToR1C1:= _
"=OFFSET(Sheet1!R2C27, 0, 0, COUNTA(Sheet1!C27), 1)"
Sheets("Sheet2").Select
Range("D1").Select
ActiveCell.FormulaR1C1 = "=Sheet1!R[1]C[19] & "" ("""
Range("D1").Select
Selection.AutoFill Destination:=Range("D1:D" & LastRow), Type:=xlFillDefault
Range("D1:D" & LastRow).Select
Range("E1").Select
ActiveCell.FormulaR1C1 = "=VALUE(Sheet1!R[1]C[19])"
Range("E1").Select
Selection.AutoFill Destination:=Range("E1:E" & LastRow), Type:=xlFillDefault
Range("E1:E" & LastRow).Select
Range("F1").Select
ActiveCell.FormulaR1C1 = "=RC[-1]&"" times)"""
Range("F1").Select
Selection.AutoFill Destination:=Range("F1:F" & LastRow), Type:=xlFillDefault
Range("F1:F" & LastRow).Select
Range("G1").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-2]>=2, RC[-3]&RC[-1], """")"
Range("G1").Select
Selection.AutoFill Destination:=Range("G1:G" & LastRow), Type:=xlFillDefault
Range("G1:G" & LastRow).Select
Range("H1").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-1]="""", R[1]C, RC[-1])"
Range("H1").Select
Selection.AutoFill Destination:=Range("H1:H" & LastRow), Type:=xlFillDefault
Range("H1:H" & LastRow).Select
Range("I1").Select
ActiveCell.FormulaR1C1 = "=ISNUMBER(MATCH(RC[-1],R1C8:H0,0))"
Range("I2").Select
ActiveCell.FormulaR1C1 = "=ISNUMBER(MATCH(RC[-1],R1C8:R[-1]C[-1],0))"
Range("I2").Select
Selection.AutoFill Destination:=Range("I2:I" & LastRow), Type:=xlFillDefault
Range("I2:I" & LastRow).Select
Range("J1").Select
ActiveCell.FormulaR1C1 = "=IF(AND( RC[-2]<>0, RC[-1]=FALSE), RC[-2], """")"
Range("J1").Select
Selection.AutoFill Destination:=Range("J1:J" & LastRow), Type:=xlFillDefault
Range("J1:J" & LastRow).Select
Sheets("Sheet1").Select
Range("AC2").Select
ActiveCell.FormulaR1C1 = _
"=IF(AND(Sheet2!R[-1]C[-19]<>Sheet1!R[-1]C, Sheet2!R[-1]C[-19]<>""""), Sheet2!R[-1]C[-19], Sheet2!RC[-19])"
Range("AC2").Select
Selection.AutoFill Destination:=Range("AC2:AC" & LastRow), Type:=xlFillDefault
Range("AC2:AC" & LastRow).Select
Range("AD1").Select
ActiveCell.FormulaR1C1 = "=COUNTIF(List1Repeats, ""*"")"
Range("AD2").Select
Selection.FormulaArray = _
"=IF(ROWS(R1C29:R[-1]C[-1])>R1C30,"""",INDEX(List1Repeats,SMALL(IF(List1Repeats<>"""",ROW(List1Repeats)-ROW(R2C7)+1),ROWS(R1C29:R[-1]C[-1]))))"
Selection.AutoFill Destination:=Range("AD2:AD" & LastRow), Type:=xlFillDefault
Range("AD2:AD" & LastRow).Select
Range("T2").Select
ActiveCell.FormulaR1C1 = _
"=IF(ISERROR(IF(RC[10]=""#NUM!"", """", RC[10])), """", RC[10])"
Range("T2").Select
Selection.AutoFill Destination:=Rng16, Type:=xlFillDefault
Rng16.Select
Sheets("Sheet3").Select
Range("D1").Select
ActiveCell.FormulaR1C1 = "=Sheet1!R[1]C[21] & "" ("""
Range("D1").Select
Selection.AutoFill Destination:=Range("D1:D" & LastRow2), Type:=xlFillDefault
Range("D1:D" & LastRow2).Select
Range("E1").Select
ActiveCell.FormulaR1C1 = "=VALUE(Sheet1!R[1]C[21])"
Range("E1").Select
Selection.AutoFill Destination:=Range("E1:E" & LastRow2), Type:=xlFillDefault
Range("E1:E" & LastRow2).Select
Range("F1").Select
ActiveCell.FormulaR1C1 = "=RC[-1]&"" times)"""
Range("F1").Select
Selection.AutoFill Destination:=Range("F1:F" & LastRow2), Type:=xlFillDefault
Range("F1:F" & LastRow2).Select
Range("G1").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-2]>=2, RC[-3]&RC[-1], """")"
Range("G1").Select
Selection.AutoFill Destination:=Range("G1:G" & LastRow2), Type:=xlFillDefault
Range("G1:G" & LastRow2).Select
Range("H1").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-1]="""", R[1]C, RC[-1])"
Range("H1").Select
Selection.AutoFill Destination:=Range("H1:H" & LastRow2), Type:=xlFillDefault
Range("H1:H" & LastRow2).Select
Range("I1").Select
ActiveCell.FormulaR1C1 = "=ISNUMBER(MATCH(RC[-1],R1C8:H0,0))"
Range("I2").Select
ActiveCell.FormulaR1C1 = "=ISNUMBER(MATCH(RC[-1],R1C8:R[-1]C[-1],0))"
Range("I2").Select
Selection.AutoFill Destination:=Range("I2:I" & LastRow2), Type:=xlFillDefault
Range("I2:I" & LastRow2).Select
Range("J1").Select
ActiveCell.FormulaR1C1 = "=IF(AND( RC[-2]<>0, RC[-1]=FALSE), RC[-2], """")"
Range("J1").Select
Selection.AutoFill Destination:=Range("J1:J" & LastRow2), Type:=xlFillDefault
Range("J1:J" & LastRow2).Select
Sheets("Sheet1").Select
Range("AA2").Select
ActiveCell.FormulaR1C1 = _
"=IF(AND(Sheet3!R[-1]C[-17]<>Sheet1!R[-1]C, Sheet3!R[-1]C[-17]<>""""), Sheet3!R[-1]C[-17], Sheet3!RC[-17])"
Range("AA2").Select
Selection.AutoFill Destination:=Rng18, Type:=xlFillDefault
Rng18.Select
Range("AB2").Select
ActiveCell.FormulaR1C1 = "=COUNTIF(List2Repeats, ""*"")"
Range("AB3").Select
Selection.FormulaArray = _
"=IF(ROWS(R1C27:R[-2]C[-1])>R2C28,"""",INDEX(List2Repeats,SMALL(IF(List2Repeats<>"""",ROW(List2Repeats)-ROW(R2C7)+1),ROWS(R1C27:R[-2]C[-1]))))"
Selection.AutoFill Destination:=Range("AB3:AB" & LastRow2), Type:=xlFillDefault
Range("AB3:AB" & LastRow2).Select
Range("V2").Select
ActiveCell.FormulaR1C1 = _
"=IF(ISERROR(IF(R[1]C[6]=""#NUM!"", """", R[1]C[6])), """", R[1]C[6])"
Range("V2").Select
Selection.AutoFill Destination:=Rng19, Type:=xlFillDefault
Rng19.Select
Columns("W:AD").Select
Selection.EntireColumn.Hidden = True
Range("B2").Select
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 1
Sheets("Sheet2").Protect Password:="password", DrawingObjects:=True, Contents:=True, Scenarios:=True
Sheets("Sheet3").Protect Password:="password", DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveSheet.Protect Password:="password", DrawingObjects:=True, Contents:=True, Scenarios:=True
End Sub
This is the macro that reconciles the two lists:
Code:
Sub ReconcileListsSeparate()
Application.ScreenUpdating = False
ActiveSheet.Unprotect Password:="password"
Sheets("Sheet2").Unprotect Password:="password"
Sheets("Sheet3").Unprotect Password:="password"
Columns("D:R").Select
Selection.EntireColumn.Hidden = True
Dim i, LastRowA, LastRowF
Dim LastRowC, LastRowE
Dim LastRow As Long
Dim Rng1 As Range
Dim Rng2 As Range
Dim Rng3 As Range
Dim Rng4 As Range
Dim Rng5 As Range
Dim Rng6 As Range
Dim Rng7 As Range
Dim Rng8 As Range
Dim Rng9 As Range
Dim Rng10 As Range
Dim Rng11 As Range
Dim Rng12 As Range
Dim Rng13 As Range
Dim Rng14 As Range
Dim Rng15 As Range
Dim Rng16 As Range
Dim Rng17 As Range
Dim Rng18 As Range
LastRow = Cells(Rows.Count, "B").End(xlUp).Row
LastRow2 = Cells(Rows.Count, "C").End(xlUp).Row
Set Rng1 = Range("D2:D" & LastRow)
Set Rng2 = Range("E2:E" & LastRow2)
Set Rng3 = Range("F2:F" & LastRow)
Set Rng4 = Range("G2:G" & LastRow2)
Set Rng5 = Range("H2:H" & LastRow)
Set Rng6 = Range("I2:I" & LastRow2)
Set Rng7 = Range("K2:K" & LastRow)
Set Rng8 = Range("L2:L" & LastRow)
Set Rng9 = Range("M2:M" & LastRow)
Set Rng10 = Range("N2:N" & LastRow)
Set Rng11 = Range("O2:O" & LastRow2)
Set Rng12 = Range("P2:P" & LastRow2)
Set Rng13 = Range("Q2:Q" & LastRow2)
Set Rng14 = Range("R2:R" & LastRow2)
Set Rng15 = Range("S2:S" & LastRow)
Set Rng16 = Range("T2:T" & LastRow)
Set Rng17 = Range("U2:U" & LastRow2)
Set Rng18 = Range("AA2:AA" & LastRow2)
Set Rng19 = Range("V2:V" & LastRow2)
Range("D2").Select
ActiveCell.FormulaR1C1 = "=ISNUMBER(MATCH(RC[-2],C[-1], 0))"
Range("D2").Select
Selection.AutoFill Destination:=Rng1, Type:=xlFillDefault
Rng1.Select
Range("E2").Select
ActiveCell.FormulaR1C1 = "=ISNUMBER(MATCH(RC[-2],C[-3], 0))"
Range("E2").Select
Selection.AutoFill Destination:=Rng2, Type:=xlFillDefault
Rng2.Select
Range("F2").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-2]=FALSE, RC[-4], """")"
Range("F2").Select
Selection.AutoFill Destination:=Rng3, Type:=xlFillDefault
Rng3.Select
Range("G2").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-2]=FALSE, RC[-4], """")"
Range("G2").Select
Selection.AutoFill Destination:=Rng4, Type:=xlFillDefault
Rng4.Select
ActiveWorkbook.Names.Add Name:="List1Only", RefersToR1C1:= _
"=OFFSET(Sheet1!R2C6, 0, 0, COUNTA(Sheet1!C6), 1)"
ActiveWorkbook.Names.Add Name:="List2Only", RefersToR1C1:= _
"=OFFSET(Sheet1!R2C7, 0, 0, COUNTA(Sheet1!C7), 1)"
ActiveWorkbook.Names.Add Name:="List2Only", RefersToR1C1:= _
"=OFFSET(Sheet1!R2C7, 0, 0, COUNTA(Sheet1!C7), 1)"
ActiveWindow.ScrollRow = 1
Range("J2").Select
ActiveCell.FormulaR1C1 = "=CountA(List1Only) - COUNTIF(List1Only, ""*"")"
Range("J3").Select
ActiveCell.FormulaR1C1 = "=CountA(List2Only) - COUNTIF(List2Only, ""*"")"
Range("H2").Select
Selection.FormulaArray = _
"=IF(ROWS(R1C4:R[-1]C[-4])>R2C10,"""",INDEX(List1Only,SMALL(IF(List1Only<>"""",ROW(List1Only)-ROW(R2C6)+1),ROWS(R1C4:R[-1]C[-4]))))"
Selection.AutoFill Destination:=Rng5, Type:=xlFillDefault
Rng5.Select
Range("I2").Select
Selection.FormulaArray = _
"=IF(ROWS(R1C5:R[-1]C[-4])>R3C10,"""",INDEX(List2Only,SMALL(IF(List2Only<>"""",ROW(List2Only)-ROW(R2C7)+1),ROWS(R1C5:R[-1]C[-4]))))"
Selection.AutoFill Destination:=Rng6, Type:=xlFillDefault
Rng6.Select
Range("S2").Select
ActiveCell.FormulaR1C1 = _
"=IF(ISERROR(IF(RC[-11]=""#NUM!"", """", RC[-11])), """", RC[-11])"
Range("S2").Select
Selection.AutoFill Destination:=Rng15, Type:=xlFillDefault
Rng15.Select
Range("U2").Select
ActiveCell.FormulaR1C1 = _
"=IF(ISERROR(IF(RC[-12]=""#NUM!"", """", RC[-12])), """", RC[-12])"
Range("U2").Select
Selection.AutoFill Destination:=Rng17, Type:=xlFillDefault
Rng17.Select
Columns("W:AD").Select
Selection.EntireColumn.Hidden = True
Range("B2").Select
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 1
Sheets("Sheet2").Protect Password:="password", DrawingObjects:=True, Contents:=True, Scenarios:=True
Sheets("Sheet3").Protect Password:="password", DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveSheet.Protect Password:="password", DrawingObjects:=True, Contents:=True, Scenarios:=True
End Sub
Finally, here is what the tool looks like:
Last edited: