Excel VBA animation runs slowly after editing worksheet











up vote
2
down vote

favorite












My VBA macro displays an animation based on a row-by-row time step simulation in the worksheet. The user enters the desired inputs and the worksheet calculates the evolution of the simulation in a 1000 row table. The user then runs the macro to display the results in an animation. The macro loops through the rows in the simulation. For each row, the macro extracts four values and applies them as positions to four shapes in the worksheet.



The macro runs fast when I open the workbook and run the macro without doing else first. If I do an edit to the worksheet that causes it to recalculate the simulation, then the macro runs about 1/3 as fast.



The Task Manager shows some interesting things about the memory usage. When the macro is run without an edit, the usage jumps 66,000 K every run. When the macro is run after an edit, the usage jumps only 2,000 K every run. I've checked the values of ScreenUpdating, EnableEvents, Calculation and DisplayPageBreaks, and they don't change if the worksheet is edited.



Is there something I can do to prevent this other than saving and reopening the workbook after I make an edit?



Sub playfast()
Dim arrRangeValues() As Variant
Dim j As Long
Dim vpos As Double
Dim hpos As Double
Dim calcflag As Boolean
Dim chartflag As Boolean
Dim eventflag As Boolean

If iVal = 0 Then iVal = 1
If iVal = 1000 Then iVal = 1
If iVal = 1001 Then iVal = 1
isCancelled = False
calcflag = ActiveSheet.Cells(8, 12).Value
chartflag = ActiveSheet.Cells(9, 12).Value
eventflag = ActiveSheet.Cells(10, 12).Value

ActiveSheet.Cells(7, 15).Value = "PLAYING"
vpos = ActiveSheet.Cells(6, 12).Value
hpos = ActiveSheet.Cells(7, 12).Value

arrRangeValues = Range("a41:q1041").Value

For j = iVal To 1001 Step ActiveSheet.Cells(24, 12).Value
iVal = j
show hpos, vpos, vpos - arrRangeValues(j, 4) * 2, vpos - arrRangeValues(j, 17) * 2, vpos - arrRangeValues(j, 5) * 2
ActiveSheet.Cells(17, 10).Value = arrRangeValues(j, 1) 'Write time into worksheet
If eventflag Then DoEvents 'Yield to operating system.
If calcflag Then ActiveSheet.Calculate 'May be necessary for animation to move after worksheet is changed

If chartflag Then
ActiveSheet.ChartObjects("uprchart").Activate
ActiveSheet.ChartObjects("uprchart").Chart.Refresh
ActiveSheet.ChartObjects("lwrchart").Activate
ActiveSheet.ChartObjects("lwrchart").Chart.Refresh
End If

If isCancelled Then GoTo EarlyExit

Next j
EarlyExit:
isCancelled = False
ActiveSheet.Cells(7, 15).Value = "STOPPED"

End Sub`
Sub show(leftedge As Double, groundtop As Double, bodytop As Double, wheelstop As Double, passengerstop As Double)

ActiveSheet.Shapes("ground").Left = leftedge
ActiveSheet.Shapes("ground").Top = groundtop

ActiveSheet.Shapes("body").Left = leftedge
ActiveSheet.Shapes("body").Top = bodytop

ActiveSheet.Shapes("wheels").Left = leftedge
ActiveSheet.Shapes("wheels").Top = wheelstop

ActiveSheet.Shapes("passengers").Left = leftedge
ActiveSheet.Shapes("passengers").Top = passengerstop

End Sub









share|improve this question









New contributor




Paul N is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • A few questions: Where is iVal coming from? What are the type of calculations performed in the worksheet (through the formulas), especially those that are necessary each time your code loops? You're bringing your 1000 row range into an array prior to the loop, but if the worksheet recalculates values in that area, the updated values are not re-copied into the array (so the calculations don't matter). Do you have sample data that we can use to replicate the behavior you're seeing?
    – PeterT
    yesterday










  • iVal is used to save the current place in the loop if the user wants to stop the animation. The loop stops via EarlyExit if the user sets isCancelled to True while the loop is running. When the user runs playfast() again, the loop resumes where it stopped.
    – Paul N
    yesterday










  • The range actually does get re-copied if the user runs playfast() again, and the loop starts with j set to the value stored in iVal. Ordinarily the user will reset iVal via another macro called reset() before playing the animation again if he edits the worksheet. I can provide sample data if that would be helpful.
    – Paul N
    yesterday










  • Can you provide a download link?
    – TinMan
    21 hours ago















up vote
2
down vote

favorite












My VBA macro displays an animation based on a row-by-row time step simulation in the worksheet. The user enters the desired inputs and the worksheet calculates the evolution of the simulation in a 1000 row table. The user then runs the macro to display the results in an animation. The macro loops through the rows in the simulation. For each row, the macro extracts four values and applies them as positions to four shapes in the worksheet.



The macro runs fast when I open the workbook and run the macro without doing else first. If I do an edit to the worksheet that causes it to recalculate the simulation, then the macro runs about 1/3 as fast.



The Task Manager shows some interesting things about the memory usage. When the macro is run without an edit, the usage jumps 66,000 K every run. When the macro is run after an edit, the usage jumps only 2,000 K every run. I've checked the values of ScreenUpdating, EnableEvents, Calculation and DisplayPageBreaks, and they don't change if the worksheet is edited.



Is there something I can do to prevent this other than saving and reopening the workbook after I make an edit?



Sub playfast()
Dim arrRangeValues() As Variant
Dim j As Long
Dim vpos As Double
Dim hpos As Double
Dim calcflag As Boolean
Dim chartflag As Boolean
Dim eventflag As Boolean

If iVal = 0 Then iVal = 1
If iVal = 1000 Then iVal = 1
If iVal = 1001 Then iVal = 1
isCancelled = False
calcflag = ActiveSheet.Cells(8, 12).Value
chartflag = ActiveSheet.Cells(9, 12).Value
eventflag = ActiveSheet.Cells(10, 12).Value

ActiveSheet.Cells(7, 15).Value = "PLAYING"
vpos = ActiveSheet.Cells(6, 12).Value
hpos = ActiveSheet.Cells(7, 12).Value

arrRangeValues = Range("a41:q1041").Value

For j = iVal To 1001 Step ActiveSheet.Cells(24, 12).Value
iVal = j
show hpos, vpos, vpos - arrRangeValues(j, 4) * 2, vpos - arrRangeValues(j, 17) * 2, vpos - arrRangeValues(j, 5) * 2
ActiveSheet.Cells(17, 10).Value = arrRangeValues(j, 1) 'Write time into worksheet
If eventflag Then DoEvents 'Yield to operating system.
If calcflag Then ActiveSheet.Calculate 'May be necessary for animation to move after worksheet is changed

If chartflag Then
ActiveSheet.ChartObjects("uprchart").Activate
ActiveSheet.ChartObjects("uprchart").Chart.Refresh
ActiveSheet.ChartObjects("lwrchart").Activate
ActiveSheet.ChartObjects("lwrchart").Chart.Refresh
End If

If isCancelled Then GoTo EarlyExit

Next j
EarlyExit:
isCancelled = False
ActiveSheet.Cells(7, 15).Value = "STOPPED"

End Sub`
Sub show(leftedge As Double, groundtop As Double, bodytop As Double, wheelstop As Double, passengerstop As Double)

ActiveSheet.Shapes("ground").Left = leftedge
ActiveSheet.Shapes("ground").Top = groundtop

ActiveSheet.Shapes("body").Left = leftedge
ActiveSheet.Shapes("body").Top = bodytop

ActiveSheet.Shapes("wheels").Left = leftedge
ActiveSheet.Shapes("wheels").Top = wheelstop

ActiveSheet.Shapes("passengers").Left = leftedge
ActiveSheet.Shapes("passengers").Top = passengerstop

End Sub









share|improve this question









New contributor




Paul N is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • A few questions: Where is iVal coming from? What are the type of calculations performed in the worksheet (through the formulas), especially those that are necessary each time your code loops? You're bringing your 1000 row range into an array prior to the loop, but if the worksheet recalculates values in that area, the updated values are not re-copied into the array (so the calculations don't matter). Do you have sample data that we can use to replicate the behavior you're seeing?
    – PeterT
    yesterday










  • iVal is used to save the current place in the loop if the user wants to stop the animation. The loop stops via EarlyExit if the user sets isCancelled to True while the loop is running. When the user runs playfast() again, the loop resumes where it stopped.
    – Paul N
    yesterday










  • The range actually does get re-copied if the user runs playfast() again, and the loop starts with j set to the value stored in iVal. Ordinarily the user will reset iVal via another macro called reset() before playing the animation again if he edits the worksheet. I can provide sample data if that would be helpful.
    – Paul N
    yesterday










  • Can you provide a download link?
    – TinMan
    21 hours ago













up vote
2
down vote

favorite









up vote
2
down vote

favorite











My VBA macro displays an animation based on a row-by-row time step simulation in the worksheet. The user enters the desired inputs and the worksheet calculates the evolution of the simulation in a 1000 row table. The user then runs the macro to display the results in an animation. The macro loops through the rows in the simulation. For each row, the macro extracts four values and applies them as positions to four shapes in the worksheet.



The macro runs fast when I open the workbook and run the macro without doing else first. If I do an edit to the worksheet that causes it to recalculate the simulation, then the macro runs about 1/3 as fast.



The Task Manager shows some interesting things about the memory usage. When the macro is run without an edit, the usage jumps 66,000 K every run. When the macro is run after an edit, the usage jumps only 2,000 K every run. I've checked the values of ScreenUpdating, EnableEvents, Calculation and DisplayPageBreaks, and they don't change if the worksheet is edited.



Is there something I can do to prevent this other than saving and reopening the workbook after I make an edit?



Sub playfast()
Dim arrRangeValues() As Variant
Dim j As Long
Dim vpos As Double
Dim hpos As Double
Dim calcflag As Boolean
Dim chartflag As Boolean
Dim eventflag As Boolean

If iVal = 0 Then iVal = 1
If iVal = 1000 Then iVal = 1
If iVal = 1001 Then iVal = 1
isCancelled = False
calcflag = ActiveSheet.Cells(8, 12).Value
chartflag = ActiveSheet.Cells(9, 12).Value
eventflag = ActiveSheet.Cells(10, 12).Value

ActiveSheet.Cells(7, 15).Value = "PLAYING"
vpos = ActiveSheet.Cells(6, 12).Value
hpos = ActiveSheet.Cells(7, 12).Value

arrRangeValues = Range("a41:q1041").Value

For j = iVal To 1001 Step ActiveSheet.Cells(24, 12).Value
iVal = j
show hpos, vpos, vpos - arrRangeValues(j, 4) * 2, vpos - arrRangeValues(j, 17) * 2, vpos - arrRangeValues(j, 5) * 2
ActiveSheet.Cells(17, 10).Value = arrRangeValues(j, 1) 'Write time into worksheet
If eventflag Then DoEvents 'Yield to operating system.
If calcflag Then ActiveSheet.Calculate 'May be necessary for animation to move after worksheet is changed

If chartflag Then
ActiveSheet.ChartObjects("uprchart").Activate
ActiveSheet.ChartObjects("uprchart").Chart.Refresh
ActiveSheet.ChartObjects("lwrchart").Activate
ActiveSheet.ChartObjects("lwrchart").Chart.Refresh
End If

If isCancelled Then GoTo EarlyExit

Next j
EarlyExit:
isCancelled = False
ActiveSheet.Cells(7, 15).Value = "STOPPED"

End Sub`
Sub show(leftedge As Double, groundtop As Double, bodytop As Double, wheelstop As Double, passengerstop As Double)

ActiveSheet.Shapes("ground").Left = leftedge
ActiveSheet.Shapes("ground").Top = groundtop

ActiveSheet.Shapes("body").Left = leftedge
ActiveSheet.Shapes("body").Top = bodytop

ActiveSheet.Shapes("wheels").Left = leftedge
ActiveSheet.Shapes("wheels").Top = wheelstop

ActiveSheet.Shapes("passengers").Left = leftedge
ActiveSheet.Shapes("passengers").Top = passengerstop

End Sub









share|improve this question









New contributor




Paul N is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











My VBA macro displays an animation based on a row-by-row time step simulation in the worksheet. The user enters the desired inputs and the worksheet calculates the evolution of the simulation in a 1000 row table. The user then runs the macro to display the results in an animation. The macro loops through the rows in the simulation. For each row, the macro extracts four values and applies them as positions to four shapes in the worksheet.



The macro runs fast when I open the workbook and run the macro without doing else first. If I do an edit to the worksheet that causes it to recalculate the simulation, then the macro runs about 1/3 as fast.



The Task Manager shows some interesting things about the memory usage. When the macro is run without an edit, the usage jumps 66,000 K every run. When the macro is run after an edit, the usage jumps only 2,000 K every run. I've checked the values of ScreenUpdating, EnableEvents, Calculation and DisplayPageBreaks, and they don't change if the worksheet is edited.



Is there something I can do to prevent this other than saving and reopening the workbook after I make an edit?



Sub playfast()
Dim arrRangeValues() As Variant
Dim j As Long
Dim vpos As Double
Dim hpos As Double
Dim calcflag As Boolean
Dim chartflag As Boolean
Dim eventflag As Boolean

If iVal = 0 Then iVal = 1
If iVal = 1000 Then iVal = 1
If iVal = 1001 Then iVal = 1
isCancelled = False
calcflag = ActiveSheet.Cells(8, 12).Value
chartflag = ActiveSheet.Cells(9, 12).Value
eventflag = ActiveSheet.Cells(10, 12).Value

ActiveSheet.Cells(7, 15).Value = "PLAYING"
vpos = ActiveSheet.Cells(6, 12).Value
hpos = ActiveSheet.Cells(7, 12).Value

arrRangeValues = Range("a41:q1041").Value

For j = iVal To 1001 Step ActiveSheet.Cells(24, 12).Value
iVal = j
show hpos, vpos, vpos - arrRangeValues(j, 4) * 2, vpos - arrRangeValues(j, 17) * 2, vpos - arrRangeValues(j, 5) * 2
ActiveSheet.Cells(17, 10).Value = arrRangeValues(j, 1) 'Write time into worksheet
If eventflag Then DoEvents 'Yield to operating system.
If calcflag Then ActiveSheet.Calculate 'May be necessary for animation to move after worksheet is changed

If chartflag Then
ActiveSheet.ChartObjects("uprchart").Activate
ActiveSheet.ChartObjects("uprchart").Chart.Refresh
ActiveSheet.ChartObjects("lwrchart").Activate
ActiveSheet.ChartObjects("lwrchart").Chart.Refresh
End If

If isCancelled Then GoTo EarlyExit

Next j
EarlyExit:
isCancelled = False
ActiveSheet.Cells(7, 15).Value = "STOPPED"

End Sub`
Sub show(leftedge As Double, groundtop As Double, bodytop As Double, wheelstop As Double, passengerstop As Double)

ActiveSheet.Shapes("ground").Left = leftedge
ActiveSheet.Shapes("ground").Top = groundtop

ActiveSheet.Shapes("body").Left = leftedge
ActiveSheet.Shapes("body").Top = bodytop

ActiveSheet.Shapes("wheels").Left = leftedge
ActiveSheet.Shapes("wheels").Top = wheelstop

ActiveSheet.Shapes("passengers").Left = leftedge
ActiveSheet.Shapes("passengers").Top = passengerstop

End Sub






performance beginner vba animation simulation






share|improve this question









New contributor




Paul N is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Paul N is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 2 days ago









200_success

127k15148412




127k15148412






New contributor




Paul N is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 days ago









Paul N

112




112




New contributor




Paul N is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Paul N is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Paul N is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • A few questions: Where is iVal coming from? What are the type of calculations performed in the worksheet (through the formulas), especially those that are necessary each time your code loops? You're bringing your 1000 row range into an array prior to the loop, but if the worksheet recalculates values in that area, the updated values are not re-copied into the array (so the calculations don't matter). Do you have sample data that we can use to replicate the behavior you're seeing?
    – PeterT
    yesterday










  • iVal is used to save the current place in the loop if the user wants to stop the animation. The loop stops via EarlyExit if the user sets isCancelled to True while the loop is running. When the user runs playfast() again, the loop resumes where it stopped.
    – Paul N
    yesterday










  • The range actually does get re-copied if the user runs playfast() again, and the loop starts with j set to the value stored in iVal. Ordinarily the user will reset iVal via another macro called reset() before playing the animation again if he edits the worksheet. I can provide sample data if that would be helpful.
    – Paul N
    yesterday










  • Can you provide a download link?
    – TinMan
    21 hours ago


















  • A few questions: Where is iVal coming from? What are the type of calculations performed in the worksheet (through the formulas), especially those that are necessary each time your code loops? You're bringing your 1000 row range into an array prior to the loop, but if the worksheet recalculates values in that area, the updated values are not re-copied into the array (so the calculations don't matter). Do you have sample data that we can use to replicate the behavior you're seeing?
    – PeterT
    yesterday










  • iVal is used to save the current place in the loop if the user wants to stop the animation. The loop stops via EarlyExit if the user sets isCancelled to True while the loop is running. When the user runs playfast() again, the loop resumes where it stopped.
    – Paul N
    yesterday










  • The range actually does get re-copied if the user runs playfast() again, and the loop starts with j set to the value stored in iVal. Ordinarily the user will reset iVal via another macro called reset() before playing the animation again if he edits the worksheet. I can provide sample data if that would be helpful.
    – Paul N
    yesterday










  • Can you provide a download link?
    – TinMan
    21 hours ago
















A few questions: Where is iVal coming from? What are the type of calculations performed in the worksheet (through the formulas), especially those that are necessary each time your code loops? You're bringing your 1000 row range into an array prior to the loop, but if the worksheet recalculates values in that area, the updated values are not re-copied into the array (so the calculations don't matter). Do you have sample data that we can use to replicate the behavior you're seeing?
– PeterT
yesterday




A few questions: Where is iVal coming from? What are the type of calculations performed in the worksheet (through the formulas), especially those that are necessary each time your code loops? You're bringing your 1000 row range into an array prior to the loop, but if the worksheet recalculates values in that area, the updated values are not re-copied into the array (so the calculations don't matter). Do you have sample data that we can use to replicate the behavior you're seeing?
– PeterT
yesterday












iVal is used to save the current place in the loop if the user wants to stop the animation. The loop stops via EarlyExit if the user sets isCancelled to True while the loop is running. When the user runs playfast() again, the loop resumes where it stopped.
– Paul N
yesterday




iVal is used to save the current place in the loop if the user wants to stop the animation. The loop stops via EarlyExit if the user sets isCancelled to True while the loop is running. When the user runs playfast() again, the loop resumes where it stopped.
– Paul N
yesterday












The range actually does get re-copied if the user runs playfast() again, and the loop starts with j set to the value stored in iVal. Ordinarily the user will reset iVal via another macro called reset() before playing the animation again if he edits the worksheet. I can provide sample data if that would be helpful.
– Paul N
yesterday




The range actually does get re-copied if the user runs playfast() again, and the loop starts with j set to the value stored in iVal. Ordinarily the user will reset iVal via another macro called reset() before playing the animation again if he edits the worksheet. I can provide sample data if that would be helpful.
– Paul N
yesterday












Can you provide a download link?
– TinMan
21 hours ago




Can you provide a download link?
– TinMan
21 hours ago















active

oldest

votes











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Paul N is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208711%2fexcel-vba-animation-runs-slowly-after-editing-worksheet%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes








Paul N is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















Paul N is a new contributor. Be nice, and check out our Code of Conduct.













Paul N is a new contributor. Be nice, and check out our Code of Conduct.












Paul N is a new contributor. Be nice, and check out our Code of Conduct.
















Thanks for contributing an answer to Code Review Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208711%2fexcel-vba-animation-runs-slowly-after-editing-worksheet%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Morgemoulin

Scott Moir

Souastre