Myriad_Rocker
Board Regular
- Joined
- Dec 1, 2004
- Messages
- 67
I have the following code executing on a report that does various things depending on what is selected. What could I do to tweak this code to make it run quicker? It takes about 30 seconds to run as it is.
The code that calls this sub is setting calculation to manual, events to false, and screen updating to false.
The code that calls this sub is setting calculation to manual, events to false, and screen updating to false.
Code:
Sub ChangeYTDFormulas()
Dim RowNum As Long
'The row we're starting on
RowNum = 16
'Making sure we're on the right sheet
Sheets("Weekly Sales Report").Select
'Get the number of rows to do
Do Until IsEmpty(Range("H" & RowNum).Value)
RowNum = RowNum + 1
Loop
RowNum = RowNum - 1 'because we found the blank row, now we need the last row that had something!
Select Case ActiveSheet.btnHighVolumeAmount.Value
Case True
ActiveSheet.Range("Y15") = "High Volume Wkly POS"
ActiveSheet.Range("Z15") = "High Volume Wk Ending"
Range("Y16").Select
ActiveCell.Formula = "=IF(ISBLANK(S16),"""",MAX(INDEX(AH16:CG16,0,MATCH(CONCATENATE(""Sum Of "",YearStart),$AH$15:$CG$15,0)):CG16))"
Range("Z16").Select
ActiveCell.Formula = "=IF(Y16="""","""",IF(Y16=0,"""",AA16))"
Range("AA16").Select
ActiveCell.Formula = "=TEXT(VLOOKUP((RIGHT(INDEX((INDEX($AH$15:$CG$15,0,MATCH(CONCATENATE(""Sum Of "",YearStart),$AH$15:$CG$15,0)):$CG$15),0," & _
"(MATCH(Y16,(INDEX(AH16:CG16,0,MATCH(CONCATENATE(""Sum Of "",YearStart),$AH$15:$CG$15,0)):CG16),0))),5)),WeekConversionYTW,3,FALSE),""mm/dd/yy"")"
Range("Y16:AA16").Select
Selection.AutoFill Destination:=Range("Y16:AA" & RowNum), Type:=xlFillValues
End Select
Select Case ActiveSheet.btnRankwithinStore.Value
Case True
ActiveSheet.Range("Y15") = "Sales Rank"
ActiveSheet.Range("Z15") = "Sales Rank within Region"
Range("Y16").Select
ActiveCell.Formula = "=IF(R16="""","""",VLOOKUP(R16,$CU$16:$EI$151,38,FALSE))"
Range("Z16").Select
ActiveCell.Formula = "=IF(R16="""","""",VLOOKUP(R16,$CU$16:$EI$151,39,FALSE))"
Range("Y16:AA16").Select
Selection.AutoFill Destination:=Range("Y16:AA" & RowNum), Type:=xlFillValues
End Select
'Changing formulas for regional #'s
Dim RegionValue As String
RegionValue = ActiveSheet.combo_Region.Value
Select Case ActiveSheet.btnPOSSales.Value
Case True
If ActiveSheet.combo_Region.Value = "(All)" Then
Range("V16").Formula = "=VLOOKUP(""Total"",SalesDataAggregate_Total,96,FALSE)" 'This is the grand total line
Range("V17").Formula = "=IF(ISERR(IF(LEFT(B17,6)="" "",VLOOKUP(TRIM(B17),SalesDataAggregate_Desc,101,FALSE)," & _
"IF(LEFT(B17,4)="" "",VLOOKUP(TRIM(B17),SalesDataAggregate_SubPack,100,FALSE),VLOOKUP(B17,SalesDataAggregate_Brand,99,FALSE)))),0," & _
"IF(ISNA(IF(LEFT(B17,6)="" "",VLOOKUP(TRIM(B17),SalesDataAggregate_Desc,101,FALSE)," & _
"IF(LEFT(B17,4)="" "",VLOOKUP(TRIM(B17),SalesDataAggregate_SubPack,100,FALSE),VLOOKUP(B17,SalesDataAggregate_Brand,99,FALSE)))),0," & _
"IF(LEFT(B17,6)="" "",VLOOKUP(TRIM(B17),SalesDataAggregate_Desc,101,FALSE)," & _
"IF(LEFT(B17,4)="" "",VLOOKUP(TRIM(B17),SalesDataAggregate_SubPack,100,FALSE),VLOOKUP(B17,SalesDataAggregate_Brand,99,FALSE)))))"
Range("X16").Formula = "=V16/VLOOKUP(""Total"",SalesDataAggregate_Total,97,FALSE)-1" 'This is the grand total line
Range("X17").Formula = "=IF(ISERR(V17/IF(LEFT(B17,6)="" "",VLOOKUP(TRIM(B17),SalesDataAggregate_Desc,102,FALSE)," & _
"IF(LEFT(B17,4)="" "",VLOOKUP(TRIM(B17),SalesDataAggregate_SubPack,101,FALSE),VLOOKUP(B17,SalesDataAggregate_Brand,100,FALSE)))-1),0," & _
"IF(ISNA(V17/IF(LEFT(B17,6)="" "",VLOOKUP(TRIM(B17),SalesDataAggregate_Desc,102,FALSE)," & _
"IF(LEFT(B17,4)="" "",VLOOKUP(TRIM(B17),SalesDataAggregate_SubPack,101,FALSE),VLOOKUP(B17,SalesDataAggregate_Brand,100,FALSE)))-1),0,V17/" & _
"IF(LEFT(B17,6)="" "",VLOOKUP(TRIM(B17),SalesDataAggregate_Desc,102,FALSE)," & _
"IF(LEFT(B17,4)="" "",VLOOKUP(TRIM(B17),SalesDataAggregate_SubPack,101,FALSE),VLOOKUP(B17,SalesDataAggregate_Brand,100,FALSE)))-1))"
Else
Range("V16").Formula = "=VLOOKUP(""*""," & RegionValue & "Agg_Brand,91,FALSE)" 'This is the grand total line
Range("V17").Formula = "=IF(ISERR(IF(LEFT(B17,6)="" "",VLOOKUP(TRIM(B17)," & RegionValue & "Agg_Desc,95,FALSE)," & _
"IF(LEFT(B17,4)="" "",VLOOKUP(TRIM(B17)," & RegionValue & "Agg_SubPack,94,FALSE),VLOOKUP(B17," & RegionValue & "Agg_Brand,93,FALSE)))),0," & _
"IF(ISNA(IF(LEFT(B17,6)="" "",VLOOKUP(TRIM(B17)," & RegionValue & "Agg_Desc,95,FALSE)," & _
"IF(LEFT(B17,4)="" "",VLOOKUP(TRIM(B17)," & RegionValue & "Agg_SubPack,94,FALSE),VLOOKUP(B17," & RegionValue & "Agg_Brand,93,FALSE)))),0," & _
"IF(LEFT(B17,6)="" "",VLOOKUP(TRIM(B17)," & RegionValue & "Agg_Desc,95,FALSE)," & _
"IF(LEFT(B17,4)="" "",VLOOKUP(TRIM(B17)," & RegionValue & "Agg_SubPack,94,FALSE),VLOOKUP(B17," & RegionValue & "Agg_Brand,93,FALSE)))))"
Range("X16").Formula = "=V16/VLOOKUP(""*""," & RegionValue & "Agg_Brand,92,FALSE)-1" 'This is the grand total line
Range("X17").Formula = "=IF(ISERR(V17/IF(LEFT(B17,6)="" "",VLOOKUP(TRIM(B17)," & RegionValue & "Agg_Desc,96,FALSE)," & _
"IF(LEFT(B17,4)="" "",VLOOKUP(TRIM(B17)," & RegionValue & "Agg_SubPack,95,FALSE),VLOOKUP(B17," & RegionValue & "Agg_Brand,94,FALSE)))-1),0," & _
"IF(ISNA(V17/IF(LEFT(B17,6)="" "",VLOOKUP(TRIM(B17)," & RegionValue & "Agg_Desc,96,FALSE)," & _
"IF(LEFT(B17,4)="" "",VLOOKUP(TRIM(B17)," & RegionValue & "Agg_SubPack,95,FALSE),VLOOKUP(B17," & RegionValue & "Agg_Brand,94,FALSE)))-1),0,V17/" & _
"IF(LEFT(B17,6)="" "",VLOOKUP(TRIM(B17)," & RegionValue & "Agg_Desc,96,FALSE)," & _
"IF(LEFT(B17,4)="" "",VLOOKUP(TRIM(B17)," & RegionValue & "Agg_SubPack,95,FALSE),VLOOKUP(B17," & RegionValue & "Agg_Brand,94,FALSE)))-1))"
End If
ActiveSheet.Range("V15") = "Rgnl POS $ - YTD"
ActiveSheet.Range("W15") = "Rgnl POS $ Indx - YTD"
ActiveSheet.Range("X15") = "Rgnl Growth vs YAGO - YTD"
Range("V17").Select
Selection.AutoFill Destination:=Range("V17:V" & RowNum)
Range("X17").Select
Selection.AutoFill Destination:=Range("X17:X" & RowNum)
End Select
Select Case ActiveSheet.btnPOSUnits.Value
Case True
If ActiveSheet.combo_Region.Value = "(All)" Then
Range("V16").Formula = "=VLOOKUP(""Total"",SalesDataAggregate_Total,166,FALSE)" 'This is the grand total line
Range("V17").Formula = "=IF(ISERR(IF(LEFT(B17,6)="" "",VLOOKUP(TRIM(B17),SalesDataAggregate_Desc,171,FALSE)," & _
"IF(LEFT(B17,4)="" "",VLOOKUP(TRIM(B17),SalesDataAggregate_SubPack,170,FALSE),VLOOKUP(B17,SalesDataAggregate_Brand,169,FALSE)))),0," & _
"IF(ISNA(IF(LEFT(B17,6)="" "",VLOOKUP(TRIM(B17),SalesDataAggregate_Desc,171,FALSE)," & _
"IF(LEFT(B17,4)="" "",VLOOKUP(TRIM(B17),SalesDataAggregate_SubPack,170,FALSE),VLOOKUP(B17,SalesDataAggregate_Brand,169,FALSE)))),0," & _
"IF(LEFT(B17,6)="" "",VLOOKUP(TRIM(B17),SalesDataAggregate_Desc,171,FALSE)," & _
"IF(LEFT(B17,4)="" "",VLOOKUP(TRIM(B17),SalesDataAggregate_SubPack,170,FALSE),VLOOKUP(B17,SalesDataAggregate_Brand,169,FALSE)))))"
Range("X16").Formula = "=V16/VLOOKUP(""Total"",SalesDataAggregate_Total,167,FALSE)-1" 'This is the grand total line
Range("X17").Formula = "=IF(ISERR(V17/IF(LEFT(B17,6)="" "",VLOOKUP(TRIM(B17),SalesDataAggregate_Desc,172,FALSE)," & _
"IF(LEFT(B17,4)="" "",VLOOKUP(TRIM(B17),SalesDataAggregate_SubPack,171,FALSE),VLOOKUP(B17,SalesDataAggregate_Brand,170,FALSE)))-1),0," & _
"IF(ISNA(V17/IF(LEFT(B17,6)="" "",VLOOKUP(TRIM(B17),SalesDataAggregate_Desc,172,FALSE)," & _
"IF(LEFT(B17,4)="" "",VLOOKUP(TRIM(B17),SalesDataAggregate_SubPack,171,FALSE),VLOOKUP(B17,SalesDataAggregate_Brand,170,FALSE)))-1),0,V17/" & _
"IF(LEFT(B17,6)="" "",VLOOKUP(TRIM(B17),SalesDataAggregate_Desc,172,FALSE)," & _
"IF(LEFT(B17,4)="" "",VLOOKUP(TRIM(B17),SalesDataAggregate_SubPack,171,FALSE),VLOOKUP(B17,SalesDataAggregate_Brand,170,FALSE)))-1))"
Else
Range("V16").Formula = "=VLOOKUP(""*""," & RegionValue & "Agg_Brand,161,FALSE)" 'This is the grand total line
Range("V17").Formula = "=IF(ISERR(IF(LEFT(B17,6)="" "",VLOOKUP(TRIM(B17)," & RegionValue & "Agg_Desc,165,FALSE)," & _
"IF(LEFT(B17,4)="" "",VLOOKUP(TRIM(B17)," & RegionValue & "Agg_SubPack,164,FALSE),VLOOKUP(B17," & RegionValue & "Agg_Brand,163,FALSE)))),0," & _
"IF(ISNA(IF(LEFT(B17,6)="" "",VLOOKUP(TRIM(B17)," & RegionValue & "Agg_Desc,165,FALSE)," & _
"IF(LEFT(B17,4)="" "",VLOOKUP(TRIM(B17)," & RegionValue & "Agg_SubPack,164,FALSE),VLOOKUP(B17," & RegionValue & "Agg_Brand,163,FALSE)))),0," & _
"IF(LEFT(B17,6)="" "",VLOOKUP(TRIM(B17)," & RegionValue & "Agg_Desc,165,FALSE)," & _
"IF(LEFT(B17,4)="" "",VLOOKUP(TRIM(B17)," & RegionValue & "Agg_SubPack,164,FALSE),VLOOKUP(B17," & RegionValue & "Agg_Brand,163,FALSE)))))"
Range("X16").Formula = "=V16/VLOOKUP(""*""," & RegionValue & "Agg_Brand,162,FALSE)-1" 'This is the grand total line
Range("X17").Formula = "=IF(ISERR(V17/IF(LEFT(B17,6)="" "",VLOOKUP(TRIM(B17)," & RegionValue & "Agg_Desc,166,FALSE)," & _
"IF(LEFT(B17,4)="" "",VLOOKUP(TRIM(B17)," & RegionValue & "Agg_SubPack,165,FALSE),VLOOKUP(B17," & RegionValue & "Agg_Brand,164,FALSE)))-1),0," & _
"IF(ISNA(V17/IF(LEFT(B17,6)="" "",VLOOKUP(TRIM(B17)," & RegionValue & "Agg_Desc,166,FALSE)," & _
"IF(LEFT(B17,4)="" "",VLOOKUP(TRIM(B17)," & RegionValue & "Agg_SubPack,165,FALSE),VLOOKUP(B17," & RegionValue & "Agg_Brand,164,FALSE)))-1),0,V17/" & _
"IF(LEFT(B17,6)="" "",VLOOKUP(TRIM(B17)," & RegionValue & "Agg_Desc,166,FALSE)," & _
"IF(LEFT(B17,4)="" "",VLOOKUP(TRIM(B17)," & RegionValue & "Agg_SubPack,165,FALSE),VLOOKUP(B17," & RegionValue & "Agg_Brand,164,FALSE)))-1))"
End If
ActiveSheet.Range("V15") = "Rgnl POS Qty - YTD"
ActiveSheet.Range("W15") = "Rgnl POS Qty Indx - YTD"
ActiveSheet.Range("X15") = "Rgnl Growth vs YAGO - YTD"
Range("V17").Select
Selection.AutoFill Destination:=Range("V17:V" & RowNum)
Range("X17").Select
Selection.AutoFill Destination:=Range("X17:X" & RowNum)
End Select
Range("B15").Select
End Sub