加快我目前的准则

时间:2017-06-29 20:37:32

标签: excel excel-vba excel-formula excel-2010 vba

我首先要为此倾倒大量代码而道歉。我被分配建立一个计划屏幕,我可以在其中提取有关销售,生产和库存的相应信息。作为所有这一切的前言,这段代码对我有用,并且完全符合我的需要。

但问题是运行所需的时间。最初,它需要大约5-7分钟,但我已经能够将它减少到1-2分钟,具体取决于所使用的计算机。我尝试过多次改变,不能减少时间。

Sub FillInventoryAcross()
'This code is the formulas for the Total Inventory, Sales, and Production Data. Just addition formulas.
Call SpeedUp
Dim strFormulas(1 To 3) As Variant

With ThisWorkbook.Sheets("Inventory")
    strFormulas(1) = "=SUM(C15,C20,C25,C30,C35,C40)"
    strFormulas(2) = "=SUM(C16,C21,C26,C31,C36,C41)"
    strFormulas(3) = "=SUM(C18,C23,C28,C33,C38,C43)"

    .Range("C11:W11").formula = strFormulas(1)
    .Range("C11:W11").FillRight
    .Range("C12:W12").formula = strFormulas(2)
    .Range("C12:W12").FillRight
    .Range("C13:W13").formula = strFormulas(3)
    .Range("C13:W13").FillRight
End With
Call SpeedDown
End Sub

Sub FillInventoryPerLocation()
'This code will fill in the inventory per location. It will add up all of the sales, movement and production per plant along with the prior days inventory.
Call SpeedUp
Dim strformula(1 To 12) As Variant

With ThisWorkbook.Sheets("Inventory")
strformula(1) = "=IFERROR(IF(TRIM(Inventori!$D:$D)=""L95"",INDEX(Inventori!$E:$E,MATCH(Inventory!$M$3,Inventori!$B:$B,0))+SUM(C16:C19)+C46,0),0)"
strformula(2) = "=IFERROR(IF(TRIM(Inventori!$D:$D)=""L90"",INDEX(Inventori!$E:$E,MATCH(Inventory!$M$3,Inventori!$B:$B,0)),0)+SUM(C21:C24),0)"
strformula(3) = "=IFERROR(IF(TRIM(Inventori!$D:$D)=""L91"",INDEX(Inventori!$E:$E,MATCH(Inventory!$M$3,Inventori!$B:$B,0)),0)+SUM(C26:C29),0)"
strformula(4) = "=IFERROR(IF(TRIM(Inventori!$D:$D)=""L93"",INDEX(Inventori!$E:$E,MATCH(Inventory!$M$3,Inventori!$B:$B,0)),0)+SUM(C31:C34),0)"
strformula(5) = "=IFERROR(IF(TRIM(Inventori!$D:$D)=""L94"",INDEX(Inventori!$E:$E,MATCH(Inventory!$M$3,Inventori!$B:$B,0)),0)+SUM(C36:C39),0)"
strformula(6) = "=IFERROR(IF(TRIM(Inventori!$D:$D)=""A78"",INDEX(Inventori!$E:$E,MATCH(Inventory!$M$3,Inventori!$B:$B,0)),0)+SUM(C41:C44),0)"
strformula(7) = "=C15+Sum(D16:D19)+D46"
strformula(8) = "=C20+sum(D21:D24)"
strformula(9) = "=C25+sum(D26:D29)"
strformula(10) = "=C30+sum(D31:D34)"
strformula(11) = "=C35+sum(D36:D39)"
strformula(12) = "=C40+sum(D41:D44)"
.Range("C15").formula = strformula(1)
.Range("C20").formula = strformula(2)
.Range("C25").formula = strformula(3)
.Range("C30").formula = strformula(4)
.Range("C35").formula = strformula(5)
.Range("C40").formula = strformula(6)
.Range("D15:W15").formula = strformula(7)
.Range("D15:W15").FillRight
.Range("D20:W20").formula = strformula(8)
.Range("D20:W20").FillRight
.Range("D25:W25").formula = strformula(9)
.Range("D25:W25").FillRight
.Range("D30:W30").formula = strformula(10)
.Range("D30:W30").FillRight
.Range("D35:W35").formula = strformula(11)
.Range("D35:W35").FillRight
.Range("D40:W40").formula = strformula(12)
.Range("D40:W40").FillRight
End With
Call SpeedDown
End Sub

Sub SumIfSales()
'This code will pull up all of the sales information for a product. Just a Sumif looking up information that matches Date/SKU. After the code is in the starting cell, it is then dragged accross for all of the other dates.
Call SpeedUp
Dim strformula(1 To 6) As Variant
With ThisWorkbook.Sheets("Inventory")
strformula(1) = "=(SUMIFS(Sales!$I$1:$I$200000,Sales!$B$1:$B$200000,""L95"",Sales!$D$1:$D$200000,CONCATENATE(Inventory!C$8,Inventory!$M$3),Sales!$AD$1:$AD$200000,""VN"")*-1)"
strformula(2) = "=(SUMIFS(Sales!$I$1:$I$200000,Sales!$B$1:$B$200000,""L90"",Sales!$D$1:$D$200000,CONCATENATE(Inventory!C$8,Inventory!$M$3),Sales!$AD$1:$AD$200000,""VN"")*-1)"
strformula(3) = "=(SUMIFS(Sales!$I$1:$I$200000,Sales!$B$1:$B$200000,""L91"",Sales!$D$1:$D$200000,CONCATENATE(Inventory!C$8,Inventory!$M$3),Sales!$AD$1:$AD$200000,""VN"")*-1)"
strformula(4) = "=(SUMIFS(Sales!$I$1:$I$200000,Sales!$B$1:$B$200000,""L93"",Sales!$D$1:$D$200000,CONCATENATE(Inventory!C$8,Inventory!$M$3),Sales!$AD$1:$AD$200000,""VN"")*-1)"
strformula(5) = "=(SUMIFS(Sales!$I$1:$I$200000,Sales!$B$1:$B$200000,""L94"",Sales!$D$1:$D$200000,CONCATENATE(Inventory!C$8,Inventory!$M$3),Sales!$AD$1:$AD$200000,""VN"")*-1)"
strformula(6) = "=(SUMIFS(Sales!$I$1:$I$200000,Sales!$B$1:$B$200000,""A78"",Sales!$D$1:$D$200000,CONCATENATE(Inventory!C$8,Inventory!$M$3),Sales!$AD$1:$AD$200000,""VN"")*-1)"
.Range("C16:W16").formula = strformula(1)
.Range("C16:W16").FillRight
.Range("C21:W21").formula = strformula(2)
.Range("C21:W21").FillRight
.Range("C26:W26").formula = strformula(3)
.Range("C26:W26").FillRight
.Range("C31:W31").formula = strformula(4)
.Range("C31:W31").FillRight
.Range("C36:W36").formula = strformula(5)
.Range("C36:W36").FillRight
.Range("C41:W41").formula = strformula(6)
.Range("C41:W41").FillRight
End With
Call SpeedDown
End Sub

Sub SumIfMovement()
'This code works in a similar way to the prior code, but looks to match Date/SKU to find product movement.
Call SpeedUp
Dim strformula(1 To 6) As Variant
With ThisWorkbook.Sheets("Inventory")
strformula(1) = "=(SUMIFS(Sales!$I$1:$I$200000,Sales!$D$1:$D$200000,CONCATENATE(Inventory!C8,Inventory!$M$3),Sales!$AD$1:$AD$200000,""VT"",Sales!$O$1:$O$200000,""95"")-SUMIFS(Sales!$I$1:$I$200000,Sales!$D$1:$D$200000,CONCATENATE(Inventory!C8,Inventory!$M$3),Sales!$AD$1:$AD$200000,""VT"",Sales!$B$1:$B$200000,""L95""))"
strformula(2) = "=(SUMIFS(Sales!$I$1:$I$200000,Sales!$D$1:$D$200000,CONCATENATE(Inventory!C8,Inventory!$M$3),Sales!$AD$1:$AD$200000,""VT"",Sales!$O$1:$O$200000,""90"")-SUMIFS(Sales!$I$1:$I$200000,Sales!$D$1:$D$200000,CONCATENATE(Inventory!C8,Inventory!$M$3),Sales!$AD$1:$AD$200000,""VT"",Sales!$B$1:$B$200000,""L90""))"
strformula(3) = "=(SUMIFS(Sales!$I$1:$I$200000,Sales!$D$1:$D$200000,CONCATENATE(Inventory!C8,Inventory!$M$3),Sales!$AD$1:$AD$200000,""VT"",Sales!$O$1:$O$200000,""91"")-SUMIFS(Sales!$I$1:$I$200000,Sales!$D$1:$D$200000,CONCATENATE(Inventory!C8,Inventory!$M$3),Sales!$AD$1:$AD$200000,""VT"",Sales!$B$1:$B$200000,""L91""))"
strformula(4) = "=(SUMIFS(Sales!$I$1:$I$200000,Sales!$D$1:$D$200000,CONCATENATE(Inventory!C8,Inventory!$M$3),Sales!$AD$1:$AD$200000,""VT"",Sales!$O$1:$O$200000,""93"")-SUMIFS(Sales!$I$1:$I$200000,Sales!$D$1:$D$200000,CONCATENATE(Inventory!C8,Inventory!$M$3),Sales!$AD$1:$AD$200000,""VT"",Sales!$B$1:$B$200000,""L93""))"
strformula(5) = "=(SUMIFS(Sales!$I$1:$I$200000,Sales!$D$1:$D$200000,CONCATENATE(Inventory!C8,Inventory!$M$3),Sales!$AD$1:$AD$200000,""VT"",Sales!$O$1:$O$200000,""94"")-SUMIFS(Sales!$I$1:$I$200000,Sales!$D$1:$D$200000,CONCATENATE(Inventory!C8,Inventory!$M$3),Sales!$AD$1:$AD$200000,""VT"",Sales!$B$1:$B$200000,""L94""))"
strformula(6) = "=(SUMIFS(Sales!$I$1:$I$200000,Sales!$D$1:$D$200000,CONCATENATE(Inventory!C8,Inventory!$M$3),Sales!$AD$1:$AD$200000,""VT"",Sales!$O$1:$O$200000,""78"")-SUMIFS(Sales!$I$1:$I$200000,Sales!$D$1:$D$200000,CONCATENATE(Inventory!C8,Inventory!$M$3),Sales!$AD$1:$AD$200000,""VT"",Sales!$B$1:$B$200000,""A78""))"
.Range("C17:W17").formula = strformula(1)
.Range("C17:W17").FillRight
.Range("C22:W22").formula = strformula(2)
.Range("C22:W22").FillRight
.Range("C27:W27").formula = strformula(3)
.Range("C27:W27").FillRight
.Range("C32:W32").formula = strformula(4)
.Range("C32:W32").FillRight
.Range("C37:W37").formula = strformula(5)
.Range("C37:W37").FillRight
.Range("C42:W42").formula = strformula(6)
.Range("C42:W42").FillRight
End With
Call SpeedDown
End Sub

Sub SumIfProduction()
'This code yet again works like the other two codes, but for production.
Call SpeedUp
Dim strformula(1 To 6) As Variant
With ThisWorkbook.Sheets("Inventory")
strformula(1) = "=(SUMIFS(Production!$Q$1:$Q$100000,Production!$B$1:$B$100000,CONCATENATE(Inventory!C$8,Inventory!$M$3),Production!$I$1:$I$100000,""P95""))"
strformula(2) = "=(SUMIFS(Production!$Q$1:$Q$100000,Production!$B$1:$B$100000,CONCATENATE(Inventory!C$8,Inventory!$M$3),Production!$I$1:$I$100000,""P90""))"
strformula(3) = "=(SUMIFS(Production!$Q$1:$Q$100000,Production!$B$1:$B$100000,CONCATENATE(Inventory!C$8,Inventory!$M$3),Production!$I$1:$I$100000,""P91""))"
strformula(4) = "=(SUMIFS(Production!$Q$1:$Q$100000,Production!$B$1:$B$100000,CONCATENATE(Inventory!C$8,Inventory!$M$3),Production!$I$1:$I$100000,""P93""))"
strformula(5) = "=(SUMIFS(Production!$Q$1:$Q$100000,Production!$B$1:$B$100000,CONCATENATE(Inventory!C$8,Inventory!$M$3),Production!$I$1:$I$100000,""P94""))"
strformula(6) = "=(SUMIFS(Production!$Q$1:$Q$100000,Production!$B$1:$B$100000,CONCATENATE(Inventory!C$8,Inventory!$M$3),Production!$I$1:$I$100000,""A78""))"
.Range("C18:W18").formula = strformula(1)
.Range("C18:W18").FillRight
.Range("C23:W23").formula = strformula(2)
.Range("C23:W23").FillRight
.Range("C28:W28").formula = strformula(3)
.Range("C28:W28").FillRight
.Range("C33:W33").formula = strformula(4)
.Range("C33:W33").FillRight
.Range("C38:W38").formula = strformula(5)
.Range("C38:W38").FillRight
.Range("C43:W43").formula = strformula(6)
.Range("C43:W43").FillRight
End With
Call SpeedDown
End Sub

Sub DailySalesHistory()
'This code works to look up the Sales History by day for a given product. Takes each starting Monday and will add a day to it accross until Sunday, then the next week starts. Does the Date/SKU thing like the other sections. It then multiplies the end value by -1 to make the sales values positive, as the user would like to see them as.
Call SpeedUp
Dim strformula(1 To 7) As Variant
With ThisWorkbook.Sheets("Inventory")
strformula(1) = "=(SUMIFS(Sales!$I$1:$I$200000,Sales!$D$1:$D$200000,CONCATENATE(Inventory!$B51,Inventory!$M$3),Sales!$AD$1:$AD$200000,""VN""))"
strformula(2) = "=(SUMIFS(Sales!$I$1:$I$200000,Sales!$D$1:$D$200000,CONCATENATE(Inventory!$B51+1,Inventory!$M$3),Sales!$AD$1:$AD$200000,""VN""))"
strformula(3) = "=(SUMIFS(Sales!$I$1:$I$200000,Sales!$D$1:$D$200000,CONCATENATE(Inventory!$B51+2,Inventory!$M$3),Sales!$AD$1:$AD$200000,""VN""))"
strformula(4) = "=(SUMIFS(Sales!$I$1:$I$200000,Sales!$D$1:$D$200000,CONCATENATE(Inventory!$B51+3,Inventory!$M$3),Sales!$AD$1:$AD$200000,""VN""))"
strformula(5) = "=(SUMIFS(Sales!$I$1:$I$200000,Sales!$D$1:$D$200000,CONCATENATE(Inventory!$B51+4,Inventory!$M$3),Sales!$AD$1:$AD$200000,""VN""))"
strformula(6) = "=(SUMIFS(Sales!$I$1:$I$200000,Sales!$D$1:$D$200000,CONCATENATE(Inventory!$B51+5,Inventory!$M$3),Sales!$AD$1:$AD$200000,""VN""))"
strformula(7) = "=(SUMIFS(Sales!$I$1:$I$200000,Sales!$D$1:$D$200000,CONCATENATE(Inventory!$B51+6,Inventory!$M$3),Sales!$AD$1:$AD$200000,""VN""))"
.Range("D51").formula = strformula(1)
.Range("D51:D108").FillDown
.Range("E51").formula = strformula(2)
.Range("E51:E108").FillDown
.Range("F51").formula = strformula(3)
.Range("F51:F108").FillDown
.Range("G51").formula = strformula(4)
.Range("G51:G108").FillDown
.Range("H51").formula = strformula(5)
.Range("H51:H108").FillDown
.Range("I51").formula = strformula(6)
.Range("I51:I108").FillDown
.Range("J51").formula = strformula(7)
.Range("J51:J108").FillDown
End With
Call SpeedDown
End Sub

Sub WeeklySalesHistory()
'This code will take all of the valuse returned in the prior code and add them together. This will give the user the total sales of a product for a given week.
Call SpeedUp
Worksheets("Inventory").Range("K51").formula = "=SUM(D51:J51)"
Range("K51:K108").FillDown
Call SpeedDown
End Sub

Sub TopDaysoftheWeek()
'This code will bring up the days of the week for three weeks. Starts with Sunday and ends with Saturday. First formula finds the Sunday, the other formulas just adds 1 to the day.
Call SpeedUp
Worksheets("Inventory").Range("C8").formula = "=TODAY()-WEEKDAY(TODAY(),2)"
Worksheets("Inventory").Range("D8").formula = "=C8+1"
Range("D8:W8").FillRight
Call SpeedDown
End Sub

Sub InventoryInfo()
'This code runs vlookups on the inputted SKU number to pull up corresponding information. If the cell is blank, it will tell the user what will come up. If there is an error, it will reflect that.
Call SpeedUp
Worksheets("Inventory").Range("B6").formula = "=IF(ISBLANK($M$3),""SKU Number"",INDEX(ItemMaster!$B:$B,MATCH(Inventory!$M$3,ItemMaster!$B:$B,0)))"
Worksheets("Inventory").Range("C6").formula = "=IF(ISBLANK($M$3),""Product Name"",IF(ISTEXT(INDEX(ItemMaster!$D:$D,MATCH(Inventory!$M$3,ItemMaster!$B:$B,0))),INDEX(ItemMaster!$D:$D,MATCH(Inventory!$M$3,ItemMaster!$B:$B,0)),INDEX(ItemMaster!$C:$C,MATCH(Inventory!$M$3,ItemMaster!$B:$B,0))))"
Worksheets("Inventory").Range("F6").formula = "=IF(ISBLANK($M$3),""Pieces Per Case in"",INDEX(ItemMaster!$I:$I,MATCH(Inventory!$M$3,ItemMaster!$B:$B,0)))&"" pieces"""
Worksheets("Inventory").Range("I6").formula = "=IF(ISBLANK($M$3),""Pieces Per Case in "",(ROUND(INDEX(ItemMaster!$J:$J,MATCH(Inventory!$M$3,ItemMaster!$B:$B,0))*35.274,2)))&"" Oz"""""
Worksheets("Inventory").Range("L6").FormulaArray = "=IF(ISBLANK($M$3),""Date of Last Run"",MAX(IF(Production!$H:$H=Inventory!$M$3,Production!$N:$N)))"
Worksheets("Inventory").Range("N6").formula = "=IF(ISBLANK($M$3),""Line of Last Run"",INDEX(Production!$E:$E,MATCH(Inventory!$M$3,Production!$H:$H,0)))"
Worksheets("Inventory").Range("P6").formula = "=IF(ISBLANK($M$3),""Allergen Codes"",""Codes: ""&$AI$5)"
Worksheets("Inventory").Range("Q6").formula = "=IF(ISBLANK($M$3),"""",$AI$6)"
Worksheets("Inventory").Range("R6").formula = "=IF(ISBLANK($M$3),"""",$AI$7)"
Worksheets("Inventory").Range("S6").formula = "=IF(ISBLANK($M$3),"""",$AI$8)"
Worksheets("Inventory").Range("T6").formula = "=IF(ISBLANK($M$3),"""",$AI$9)"
Worksheets("Inventory").Range("U6").formula = "=IF(ISBLANK($M$3),"""",$AI$10)"
Worksheets("Inventory").Range("V6").formula = "=IF(ISBLANK($M$3),"""",$AI$11)"
Worksheets("Inventory").Range("W6").formula = "=IF(ISBLANK($M$3),"""",$AI$12)"
Worksheets("Inventory").Range("E10").formula = "=IF(ISBLANK($M$3),""Cases Per Dough"",IF(ISNA(VLOOKUP(NUMBERVALUE($M$3),CPD!$A$1:$D$381,2,FALSE)),0,VLOOKUP(NUMBERVALUE($M$3),CPD!$A$1:$D$381,2,FALSE)))"
Worksheets("Inventory").Range("J10").formula = "=IF(ISBLANK($M$3),""Lines Product Was Run On"",""Lines: ""&$AO$5)"
Worksheets("Inventory").Range("K10").formula = "=IF(ISBLANK($M$3),"""",IF(OR(ISERR(AO6),ISNA(AO6)),"""",AO6))"
Worksheets("Inventory").Range("L10").formula = "=IF(ISBLANK($M$3),"""",IF(OR(ISERR(AO7),ISNA(AO7)),"""",AO7))"
Worksheets("Inventory").Range("M10").formula = "=IF(ISBLANK($M$3),"""",IF(OR(ISERR(AO8),ISNA(AO8)),"""",AO8))"
Worksheets("Inventory").Range("N10").formula = "=IF(ISBLANK($M$3),"""",IF(OR(ISERR(AO9),ISNA(AO9)),"""",AO9))"
Worksheets("Inventory").Range("O10").formula = "=IF(ISBLANK($M$3),""Average Cases Sold Per Week"", SUM(K56:K67)/12)"
Worksheets("Inventory").Range("R10").formula = "=IF(ISBLANK($M$3),""Average Cases Sold Per Day"", $O$10/7)"
Worksheets("Inventory").Range("U10").formula = "=IF(ISBLANK($M$3),""Days of Inventory Remaining"",(INDEX(Inventori!$F:$F,MATCH(Inventory!$M$3,Inventori!$B:$B,0)))/R10)"
Call SpeedDown
End Sub

Sub HiddenFormulas()
'This code runs some of the hidden formulas used to calculate and find factors for the inventory screen. The user will not be allowed to see or interact with them.
Call SpeedUp
Worksheets("Inventory").Range("AF5").formula = "=INDEX(ItemMaster!$K:$K,MATCH($M$3,ItemMaster!$B:$B,0))"
Worksheets("Inventory").Range("AF6").formula = "=INDEX(ItemMaster!$L:$L,MATCH($M$3,ItemMaster!$B:$B,0))"
Worksheets("Inventory").Range("AF7").formula = "=INDEX(ItemMaster!$M:$M,MATCH($M$3,ItemMaster!$B:$B,0))"
Worksheets("Inventory").Range("AF8").formula = "=INDEX(ItemMaster!$N:$N,MATCH($M$3,ItemMaster!$B:$B,0))"
Worksheets("Inventory").Range("AF9").formula = "=INDEX(ItemMaster!$O:$O,MATCH($M$3,ItemMaster!$B:$B,0))"
Worksheets("Inventory").Range("AF10").formula = "=INDEX(ItemMaster!$P:$P,MATCH($M$3,ItemMaster!$B:$B,0))"
Worksheets("Inventory").Range("AF11").formula = "=INDEX(ItemMaster!$Q:$Q,MATCH($M$3,ItemMaster!$B:$B,0))"
Worksheets("Inventory").Range("AF12").formula = "=INDEX(ItemMaster!$R:$R,MATCH($M$3,ItemMaster!$B:$B,0))"
Worksheets("Inventory").Range("AI5").formula = "=IF($AF$5=""X"",""O"","""")"
Worksheets("Inventory").Range("AI6").formula = "=IF(AF6=""X"",""A"","""")"
Worksheets("Inventory").Range("AI7").formula = "=IF(AF7=""X"",""B"","""")"
Worksheets("Inventory").Range("AI8").formula = "=IF(AF8=""X"",""C"","""")"
Worksheets("Inventory").Range("AI9").formula = "=IF(AF9=""X"",""AB"","""")"
Worksheets("Inventory").Range("AI10").formula = "=IF(AF10=""X"",""AC"","""")"
Worksheets("Inventory").Range("AI11").formula = "=IF(AF11=""X"",""BC"","""")"
Worksheets("Inventory").Range("AI12").formula = "=IF(AF12=""X"",""ABC"","""")"
Worksheets("Inventory").Range("AL5").FormulaArray = "=INDEX(Production!$E:$E, SMALL(IF(Inventory!$M$3=Production!$H:$H, ROW(Production!$H:$H)-ROW($A$1)+1), ROW(1:1)))"
Range("AL5:AL554").FillDown
Worksheets("Inventory").Range("AO5").FormulaArray = "=INDEX($AL$5:$AL$554, MATCH(0, COUNTIF($AO$4:AO4,$AL$5:$AL$554), 0))"
Worksheets("Inventory").Range("AO6").FormulaArray = "=INDEX($AL$5:$AL$554, MATCH(0, COUNTIF($AO$4:AO5,$AL$5:$AL$554), 0))"
Worksheets("Inventory").Range("AO7").FormulaArray = "=INDEX($AL$5:$AL$554, MATCH(0, COUNTIF($AO$4:AO6,$AL$5:$AL$554), 0))"
Worksheets("Invnetory").Range("AO8").FormulaArray = "=INDEX($AL$5:$AL$554, MATCH(0, COUNTIF($AO$4:AO7,$AL$5:$AL$554), 0))"
Worksheets("Inventory").Range("AO9").FormulaArray = "=INDEX($AL$5:$AL$554, MATCH(0, COUNTIF($AO$4:AO8,$AL$5:$AL$554), 0))"
Call SpeedDown
End Sub

Sub BottomDaysoftheWeek()
'This code runs with TopDaysoftheWeek. Thile that code will pull up the date of each day, this code will convert that into the name of the day.
Call SpeedUp
Worksheets("Inventory").Range("C9").formula = "=TEXT(C8,""ddd"")"
Range("C9:W9").FillRight
Call SpeedDown
End Sub

Sub SalesHistoryByMonth()
'This code finds the months for Sales History. It will find the month ahead of the current month all the way until a year prior.
Call SpeedUp
Worksheets("Inventory").Range("L51").formula = "=EOMONTH(TODAY(),0)+1"
Worksheets("Inventory").Range("L53").formula = "=EOMONTH(TODAY(),-1)+1"
Worksheets("Inventory").Range("L55").formula = "=EOMONTH(TODAY(),-2)+1"
Worksheets("Inventory").Range("L57").formula = "=EOMONTH(TODAY(),-3)+1"
Worksheets("Inventory").Range("L59").formula = "=EOMONTH(TODAY(),-4)+1"
Worksheets("Inventory").Range("L61").formula = "=EOMONTH(TODAY(),-5)+1"
Worksheets("Inventory").Range("L63").formula = "=EOMONTH(TODAY(),-6)+1"
Worksheets("Inventory").Range("L65").formula = "=EOMONTH(TODAY(),-7)+1"
Worksheets("Inventory").Range("L67").formula = "=EOMONTH(TODAY(),-8)+1"
Worksheets("Inventory").Range("L69").formula = "=EOMONTH(TODAY(),-9)+1"
Worksheets("Inventory").Range("L71").formula = "=EOMONTH(TODAY(),-10)+1"
Worksheets("Inventory").Range("L73").formula = "=EOMONTH(TODAY(),-11)+1"
Worksheets("Inventory").Range("L75").formula = "=EOMONTH(TODAY(),-12)+1"
Worksheets("Inventory").Range("L77").formula = "=EOMONTH(TODAY(),-13)+1"
Call SpeedDown
End Sub

Sub SalesHistoryWeeks()
'This code finds the weeks of sales history. The first formula takes the first date of the inventory section and adds 6 weeks to it. The following formula just decreases the week by a week until the end of the table.
Call SpeedUp
Worksheets("Inventory").Range("B51").formula = "=(C8+42)-WEEKDAY(C8,3)"
Worksheets("Inventory").Range("B52").formula = "=B51-7"
Range("B52:B108").FillDown
Call SpeedDown
End Sub

Sub SalesHistoryMonthlyCalculations()
'This code calculates the sales history by month. It works by adding together all of the weekly sales history to the left of these formulas based on if the weeks are within the corresponding month. This formula is a little iffy, where it works on a beginning of the week basis (i.e. 5/28-6/4 counts as May, not May and June). Aside form that, works really well. May need to rework this formula.
Call SpeedUp
Worksheets("Inventory").Range("L52").formula = "=SUMIFS(K51:K108,B51:B108,"">=""&L51,B51:B108,""<=""&EOMONTH(L51,0))"
Worksheets("Inventory").Range("L54").formula = "=SUMIFS(K51:K108,B51:B108,"">=""&L53,B51:B108,""<=""&EOMONTH(L53,0))"
Worksheets("Inventory").Range("L56").formula = "=SUMIFS(K51:K108,B51:B108,"">=""&L55,B51:B108,""<=""&EOMONTH(L55,0))"
Worksheets("Inventory").Range("L58").formula = "=SUMIFS(K51:K108,B51:B108,"">=""&L57,B51:B108,""<=""&EOMONTH(L57,0))"
Worksheets("Inventory").Range("L60").formula = "=SUMIFS(K51:K108,B51:B108,"">=""&L59,B51:B108,""<=""&EOMONTH(L59,0))"
Worksheets("Inventory").Range("L62").formula = "=SUMIFS(K51:K108,B51:B108,"">=""&L61,B51:B108,""<=""&EOMONTH(L61,0))"
Worksheets("Inventory").Range("L64").formula = "=SUMIFS(K51:K108,B51:B108,"">=""&L63,B51:B108,""<=""&EOMONTH(L63,0))"
Worksheets("Inventory").Range("L66").formula = "=SUMIFS(K51:K108,B51:B108,"">=""&L65,B51:B108,""<=""&EOMONTH(L65,0))"
Worksheets("Inventory").Range("L68").formula = "=SUMIFS(K51:K108,B51:B108,"">=""&L67,B51:B108,""<=""&EOMONTH(L67,0))"
Worksheets("Inventory").Range("L70").formula = "=SUMIFS(K51:K108,B51:B108,"">=""&L69,B51:B108,""<=""&EOMONTH(L69,0))"
Worksheets("Inventory").Range("L72").formula = "=SUMIFS(K51:K108,B51:B108,"">=""&L71,B51:B108,""<=""&EOMONTH(L71,0))"
Worksheets("Inventory").Range("L74").formula = "=SUMIFS(K51:K108,B51:B108,"">=""&L73,B51:B108,""<=""&EOMONTH(L73,0))"
Worksheets("Inventory").Range("L76").formula = "=SUMIFS(K51:K108,B51:B108,"">=""&L75,B51:B108,""<=""&EOMONTH(L75,0))"
Worksheets("Inventory").Range("L78").formula = "=SUMIFS(K51:K108,B51:B108,"">=""&L77,B51:B108,""<=""&EOMONTH(L77,0))"
Call SpeedDown
End Sub

Sub ProductionHistoryInfo()
'This code is all of the formulas regarding Production History. Finds Cases, Doughs, and Line product was run on. Also has the yield formulas (cases/doughs).
Call SpeedUp
Worksheets("Inventory").Range("Q52").FormulaArray = "=IF(OR(($O52)="""",$O52=DATE(1900,1,0)),"""",INDEX(Production!$R:$R,MATCH(CONCATENATE(Inventory!$O52,Inventory!$M$3),Production!$B:$B,0)))"
Range("Q52:Q104").FillDown
Worksheets("Inventory").Range("R52").formula = "0"
Range("R52:R104").FillDown
Worksheets("Inventory").Range("S52").FormulaArray = "=IF(OR(($O52)="""",$O52=DATE(1900,1,0)),"""",INDEX(Production!$E:$E,MATCH(CONCATENATE(Inventory!$O52,Inventory!$M$3),Production!$B:$B,0)))"
Range("S52:S104").FillDown
Worksheets("Inventory").Range("T52").formula = "=IF(ISERR(Q52/R52),"""",Q52/R52)"
Range("T52:T104").FillDown
Call SpeedDown
End Sub

Sub ProductionHistoryDates()
'This code finds the dates that a given product was run on. Uses an array function to look up dates of production based on matching SKU numbers. Most volatile function in this section. Can only lookup/match up to 20,000 values, which could be problematic. May need some editing done based on how actual tables are set up. Goes from oldest to newest, which may also be a problem.
Call SpeedUp
Worksheets("Inventory").Range("O52").FormulaArray = "=IFERROR(OFFSET(Production!$H$1:$H$100000,SMALL(IF(Production!$H$1:$H$100000=Inventory!$M$3,ROW(Production!$H$1:$H$100000)-ROW(INDEX(Production!$H$1:$H$100000,1,1))),ROW()-51),COLUMN()-9),"""")"
Range("O52:O104").FillDown
Call SpeedDown
End Sub

Sub BorderFixer()
'This code was created after I discovered that some of these vba formulas will break the borders I made after running. Its only purpose is to fill in those broken borders and fix them to look like how they looked before.

With Worksheets("Inventory").Range("W10").Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 2
End With

  With Worksheets("Inventory").Range("W7:W43").Borders(xlEdgeRight)
 .LineStyle = xlContinuous
 .Weight = xlThin
 .ColorIndex = 2
 End With

 With Worksheets("Inventory").Range("B108:K108").Borders(xlEdgeBottom)
 .LineStyle = xlContinuous
 .Weight = xlThin
 .ColorIndex = 2
 End With

 With Worksheets("Inventory").Range("K108").Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 2
End With
End Sub

Sub ResetInventory()
'This code is made to run only if the user manages to unlock all of the cells on this sheet and start deleting the formulas I added. Once clicked, this code will: deactivate all of Excel's functions, run all of the prior codes related to inventory, run the border fixer to repair broken borders, and reactivate Excel's functions. While it runs fast and effectively, has a chance to break the worksheet. If so, click fix frozen cells. Will return error if the user tries to run this while sheets are protected.
 Call SpeedUp
 Call InventoryInfo
 Call TopDaysoftheWeek
 Call BottomDaysoftheWeek
 Call SumIfSales
 Call SumIfMovement
 Call SumIfProduction
 Call FillInventoryPerLocation
 Call FillInventoryAcross
 Call BorderFixer
 Call CPD
 Call SpeedDown
 End Sub

Sub ResetSalesHistory()
'This code is made to run only if the user manages to unlock all of the cells on this sheet and start deleting the formulas I added. Once clicked, this code will: deactivate all of Excel's functions, run all of the prior codes related to sales history, run the border fixer to repair broken borders, and reactivate Excel's functions. While it runs fast and effectively, has a chance to break the worksheet. If so, click fix frozen cells. Will return error if the user tries to run this while sheets are protected.
Call SpeedUp
Call SalesHistoryWeeks
Call DailySalesHistory
Call WeeklySalesHistory
Call SalesHistoryByMonth
Call SalesHistoryMonthlyCalculations
Call BorderFixer
Call SpeedDown
End Sub

Sub ResetProductionHistory()
'This code is made to run only if the user manages to unlock all of the cells on this sheet and start deleting the formulas I added. Once clicked, this code will: deactivate all of Excel's functions, run all of the prior codes related to production history, run the border fixer to repair broken borders, and reactivate Excel's functions. While it runs fast and effectively, has a chance to break the worksheet. If so, click fix frozen cells. Will return error if the user tries to run this while sheets are protected.
Call SpeedUp
Call ProductionHistoryDates
Call ProductionHistoryInfo
Call BorderFixer
Call SpeedDown
End Sub

Sub InventoryPrintPreview()
'This code is made to show the users a print preview of what the worksheet will look like. I already made the print areas for the three tables on this sheet. User can change margins or format.
Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",True)"
Application.DisplayFormulaBar = True
ActiveWindow.DisplayWorkbookTabs = True
Worksheets("Inventory").PrintPreview
Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",False)"
Application.DisplayFormulaBar = False
ActiveWindow.DisplayWorkbookTabs = False
End Sub

2 个答案:

答案 0 :(得分:0)

我在您的广泛代码中没有注意到这一点,但您是否尝试过关闭某些Excel服务。

放置代码开头:

With Excel.Application
   .ScreenUpdating = False
   .EnableEvents = False
   .Calculation = Excel.xlCalculationManual
   .DisplayStatusBar = False
End With

放在代码末尾:

With Excel.Application
    .DisplayStatusBar = True
    .Calculation = Excel.xlCalculationAutomatic
    .EnableEvents = True
    .ScreenUpdating = True
End With

答案 1 :(得分:0)

你似乎有很多这样的行:

.Range("D15:W15").formula = strformula(7)
.Range("D15:W15").FillRight

当您已将公式放置到这些单元格时,FillRight不会执行任何操作,因此可以将其删除。可能比速度更有助于线的数量。 : - )