chriscorpion786
Board Regular
- Joined
- Apr 3, 2011
- Messages
- 112
- Office Version
- 365
- Platform
- Windows
Hi All,
I have a workbook where i am extracting some rows and dumping it in the "RunMacros" worksheet. I then remove the duplicates and dump the data in the "Inventory"sheet. When running on the formulas, the macro runs forever, i have around 300,000 rows. Can you help to understand why is it taking so long, is there any other method to cut down the time taken.
Thanks in advance.
Chriscorpion786
Sub Inventory()'
Dim lastrow As Long
lastrow = Worksheets("SoldParts").Cells(Rows.Count, 6).End(xlUp).Row
Worksheets("Inventory").Activate
Rows("7:7").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete shift:=xlUp
Sheets("SoldParts").Select
Range("F2:G" & lastrow).Copy Destination:=Sheets("RunMacros").Range("A2")
Sheets("RunMacros").Select
ActiveSheet.Range("A1:B" & lastrow).RemoveDuplicates Columns:=1, Header:= _
xlYes
Application.DisplayAlerts = True
Columns("A:B").Select
' ActiveWorkbook.Worksheets("RunMacros").Sort.SortFields.Clear
' ActiveWorkbook.Worksheets("RunMacros").Sort.SortFields.Add Key:=Range( _
' "A2:A1048000"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
' xlSortNormal
With ActiveWorkbook.Worksheets("RunMacros").Sort
.SetRange Range("A1:B" & lastrow)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("A2:B2").Select
Range(Selection, Selection.End(xlDown)).Copy Destination:=Sheets("Inventory").Range("I7")
' Sheets("Inventory").Select
' Range("I7").Select
' Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
' :=False, Transpose:=False
Range("H7").Select
ActiveCell.FormulaR1C1 = _
"=IF(RC9="""","""",IF(ISNA(VLOOKUP(RC9,Stock!C4:C17,13,FALSE)),"""",VLOOKUP(RC9,Stock!C4:C17,13,FALSE)))"
Range("K7").Select
ActiveCell.FormulaR1C1 = _
"=IF(RC9="""","""",IF(ISNA(VLOOKUP(RC9,Stock!C4:C16,2,FALSE)),"""",VLOOKUP(RC9,Stock!C4:C16,2,FALSE)))"
Range("L7").Select
ActiveCell.FormulaR1C1 = _
"=IF(RC9="""","""",IF(ISNA(VLOOKUP(RC9,Stock!C4:C16,8,FALSE)),"""",VLOOKUP(RC9,Stock!C4:C16,8,FALSE)))"
Range("M7").Select
ActiveCell.FormulaR1C1 = _
"=IF(RC9="""","""",IF(ISNA(VLOOKUP(RC9,Stock!C4:C16,9,FALSE)),"""",VLOOKUP(RC9,Stock!C4:C16,9,FALSE)))"
Range("N7").Select
Range("O7").Select
ActiveCell.FormulaR1C1 = _
"=IF(RC9="""","""",IF(ISNA(VLOOKUP(RC9,Stock!C4:C16,10,FALSE)),"""",VLOOKUP(RC9,Stock!C4:C16,10,FALSE)))"
Range("P7").Select
ActiveCell.FormulaR1C1 = _
"=IF(RC9="""","""",IF(ISNA(VLOOKUP(RC9,Stock!C4:C16,11,FALSE)),"""",VLOOKUP(RC9,Stock!C4:C16,11,FALSE)))"
Range("Q7").Select
ActiveCell.FormulaR1C1 = _
"=IF(RC9="""","""",IF(ISNA(VLOOKUP(RC9,Stock!C4:C16,12,FALSE)),"""",VLOOKUP(RC9,Stock!C4:C16,12,FALSE)))"
Range("R7").Select
ActiveCell.FormulaR1C1 = _
"=IF(AND(RC9<>"""",SUMIF(SoldParts!C3,RC9&R6C18,SoldParts!C15)>0),SUMIF(SoldParts!C3,RC9&R6C18,SoldParts!C15),"""")"
Range("S7").Select
ActiveCell.FormulaR1C1 = _
"=IF(AND(RC9<>"""",SUMIF(SoldParts!C3,RC9&R6C19,SoldParts!C15)>0),SUMIF(SoldParts!C3,RC9&R6C19,SoldParts!C15),"""")"
Range("T7").Select
ActiveCell.FormulaR1C1 = _
"=IF(AND(RC9<>"""",SUMIF(SoldParts!C3,RC9&R6C20,SoldParts!C15)>0),SUMIF(SoldParts!C3,RC9&R6C20,SoldParts!C15),"""")"
Range("U7").Select
ActiveCell.FormulaR1C1 = _
"=IF(AND(RC9<>"""",SUMIF(SoldParts!C3,RC9&R6C21,SoldParts!C15)>0),SUMIF(SoldParts!C3,RC9&R6C21,SoldParts!C15),"""")"
Range("Inventory[#Headers]").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("H1").Select
End Sub
I have a workbook where i am extracting some rows and dumping it in the "RunMacros" worksheet. I then remove the duplicates and dump the data in the "Inventory"sheet. When running on the formulas, the macro runs forever, i have around 300,000 rows. Can you help to understand why is it taking so long, is there any other method to cut down the time taken.
Thanks in advance.
Chriscorpion786
Sub Inventory()'
Dim lastrow As Long
lastrow = Worksheets("SoldParts").Cells(Rows.Count, 6).End(xlUp).Row
Worksheets("Inventory").Activate
Rows("7:7").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete shift:=xlUp
Sheets("SoldParts").Select
Range("F2:G" & lastrow).Copy Destination:=Sheets("RunMacros").Range("A2")
Sheets("RunMacros").Select
ActiveSheet.Range("A1:B" & lastrow).RemoveDuplicates Columns:=1, Header:= _
xlYes
Application.DisplayAlerts = True
Columns("A:B").Select
' ActiveWorkbook.Worksheets("RunMacros").Sort.SortFields.Clear
' ActiveWorkbook.Worksheets("RunMacros").Sort.SortFields.Add Key:=Range( _
' "A2:A1048000"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
' xlSortNormal
With ActiveWorkbook.Worksheets("RunMacros").Sort
.SetRange Range("A1:B" & lastrow)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("A2:B2").Select
Range(Selection, Selection.End(xlDown)).Copy Destination:=Sheets("Inventory").Range("I7")
' Sheets("Inventory").Select
' Range("I7").Select
' Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
' :=False, Transpose:=False
Range("H7").Select
ActiveCell.FormulaR1C1 = _
"=IF(RC9="""","""",IF(ISNA(VLOOKUP(RC9,Stock!C4:C17,13,FALSE)),"""",VLOOKUP(RC9,Stock!C4:C17,13,FALSE)))"
Range("K7").Select
ActiveCell.FormulaR1C1 = _
"=IF(RC9="""","""",IF(ISNA(VLOOKUP(RC9,Stock!C4:C16,2,FALSE)),"""",VLOOKUP(RC9,Stock!C4:C16,2,FALSE)))"
Range("L7").Select
ActiveCell.FormulaR1C1 = _
"=IF(RC9="""","""",IF(ISNA(VLOOKUP(RC9,Stock!C4:C16,8,FALSE)),"""",VLOOKUP(RC9,Stock!C4:C16,8,FALSE)))"
Range("M7").Select
ActiveCell.FormulaR1C1 = _
"=IF(RC9="""","""",IF(ISNA(VLOOKUP(RC9,Stock!C4:C16,9,FALSE)),"""",VLOOKUP(RC9,Stock!C4:C16,9,FALSE)))"
Range("N7").Select
Range("O7").Select
ActiveCell.FormulaR1C1 = _
"=IF(RC9="""","""",IF(ISNA(VLOOKUP(RC9,Stock!C4:C16,10,FALSE)),"""",VLOOKUP(RC9,Stock!C4:C16,10,FALSE)))"
Range("P7").Select
ActiveCell.FormulaR1C1 = _
"=IF(RC9="""","""",IF(ISNA(VLOOKUP(RC9,Stock!C4:C16,11,FALSE)),"""",VLOOKUP(RC9,Stock!C4:C16,11,FALSE)))"
Range("Q7").Select
ActiveCell.FormulaR1C1 = _
"=IF(RC9="""","""",IF(ISNA(VLOOKUP(RC9,Stock!C4:C16,12,FALSE)),"""",VLOOKUP(RC9,Stock!C4:C16,12,FALSE)))"
Range("R7").Select
ActiveCell.FormulaR1C1 = _
"=IF(AND(RC9<>"""",SUMIF(SoldParts!C3,RC9&R6C18,SoldParts!C15)>0),SUMIF(SoldParts!C3,RC9&R6C18,SoldParts!C15),"""")"
Range("S7").Select
ActiveCell.FormulaR1C1 = _
"=IF(AND(RC9<>"""",SUMIF(SoldParts!C3,RC9&R6C19,SoldParts!C15)>0),SUMIF(SoldParts!C3,RC9&R6C19,SoldParts!C15),"""")"
Range("T7").Select
ActiveCell.FormulaR1C1 = _
"=IF(AND(RC9<>"""",SUMIF(SoldParts!C3,RC9&R6C20,SoldParts!C15)>0),SUMIF(SoldParts!C3,RC9&R6C20,SoldParts!C15),"""")"
Range("U7").Select
ActiveCell.FormulaR1C1 = _
"=IF(AND(RC9<>"""",SUMIF(SoldParts!C3,RC9&R6C21,SoldParts!C15)>0),SUMIF(SoldParts!C3,RC9&R6C21,SoldParts!C15),"""")"
Range("Inventory[#Headers]").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("H1").Select
End Sub