I recorded a macro to better understand my process. This macro is shown below in the first part. The second part is my attempt to make this thing faster.
Current Sequence of Events
- Insert asset name in cell D3, in a tab titled "Live" (cell D3 is a dropdown list with 50+ assets - more on this in next section titled "Steps We Can Improve")
- Calculate new asset by using Application.CalculateFull
- Wait until the "Live" tab is done calculating to move to next line of code
- Wait 15 seconds (more on this in next section)
- Copy the newly iterated data in the "Live" tab
- Create a new worksheet titled with the asset's name
- Paste the data from the "Live" tab as values into the newly created worksheet
- Repeat steps 1-7 until all assets have been calculated
Steps to Improve
- instead of inserting an asset name in cell D3, which requires a lot of code, I would like to loop through the list found within cell D3 (a data validation list which sources asset names from a master list on another worksheet).
- I added time to the code because I thought the model was not fully iterating for certain assets. This was wrong - it never iterated correctly with the addition of time. So, it is likely a formula error? The formula is listed below, and it is showing a #VALUE error for roughly 20 assets - but only when I run the macro. Outside of the macro, this formula populates correctly.
Sub SlowMACROv3()
' Turns off screen updating and auto calcs which helps to speed macro
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
' Turns off screen updating and auto calcs which helps to speed macro
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Sheets("Live").Select
Range("D3").Select
ActiveCell.FormulaR1C1 = "309"
Application.Calculate
If Not Application.CalculationState = xlDone Then DoEvents
Application.Wait (Now + TimeValue("0:00:15"))
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Sheets("Live").Select
Sheets("Live").Copy Before:=Sheets(1)
Range("D3").Select
ActiveCell.FormulaR1C1 = "310"
Application.Calculate
If Not Application.CalculationState = xlDone Then DoEvents
Application.Wait (Now + TimeValue("0:00:15"))
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
' Turns on screen updating and auto calcs
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
My second attempt works, but the code is just as long. Also a formula throws an error for some assets and can be found at the very bottom.
Sub SlowishMACROv1()
' Turns off screen updating and auto calcs which helps to speed macro
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
' Cycle all assets and create new worksheets as values macro
Sheets("Live").Copy Before:=Sheets(1)
Range("D3").FormulaR1C1 = "309"
Application.Calculate
If Not Application.CalculationState = xlDone Then DoEvents
Range("A1:XFD1000").Value = Range("A1:XFD1000").Value
Sheets("Live").Copy Before:=Sheets(1)
Range("D3").FormulaR1C1 = "310"
Application.Calculate
If Not Application.CalculationState = xlDone Then DoEvents
Range("A1:XFD1000").Value = Range("A1:XFD1000").Value
' Turns on screen updating and auto calcs
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
Formula: =+IF(INDEX('Reversion Dashboard'!$I:$I,MATCH($D$3,'Reversion Dashboard'!$C:$C,0))=1,I331*(1+(RATE($E$325,,$I331,-('Reversion Schedule'!$K$19)))),"")
This formula works in the "Live" tab except for assets that trigger the =1 portion within the IF True section, whenever the macro is run. Why does this formula work every time we switch to a different asset, but not when I run the macro?