• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Undo a macro with check box

danrosey

New Member
I know it's possible, just not sure how to edit my Macro code...


I've got a Macro recorded which adds data labels and formats them for a chart when "Checked." How do I edit this so that when I "Uncheck," it reverses the code? I imagine I can do a simple "If True, then X, if False then X" statement but I haven't had luck getting it just right.


Any thoughts welcome!


Current macro below:


Sub Toggle_Volume_Labels()


'

' Toggle_Volume_Labels Macro

'


'

ActiveSheet.ChartObjects("Chart 11").Activate

ActiveChart.SeriesCollection(1).Select

ActiveChart.SeriesCollection(1).ApplyDataLabels

ActiveChart.SeriesCollection(1).DataLabels.Select

Selection.NumberFormat = "[$-409]mmmmm;@"

Selection.NumberFormat = _

"[>=1000000] #,##0.0,,""M"";[>0] #,##0.0,""K"";General"

ActiveChart.SeriesCollection(1).Select

ActiveChart.SeriesCollection(1).DataLabels.Select

Selection.Position = xlLabelPositionAbove

Selection.Format.TextFrame2.TextRange.Font.Italic = msoTrue

Range("H1").Select

End Sub
 
Hi, danrosey!

Ok, undo, ... but undo to which previous state? what was before you run the macro?

Regards!
 
SirJB7 - correct, to previous state! The graph loads without labels, the check box will toggle those lables on and off.
 
Hi, danrosey!


Change your code to this:


-----

[pre]
Code:
Option Explicit

Sub Toggle_Volume_Labels()

'
' Toggle_Volume_Labels Macro
'

'

ActiveSheet.ChartObjects("Chart 11").Activate

With ActiveChart.SeriesCollection(1)
.Select
If .HasDataLabels Then
' has, then remove
.HasDataLabels = False
Else
' hasn't, then add
.ApplyDataLabels
.DataLabels.Select
Selection.NumberFormat = "[$-409]mmmmm;@"
Selection.NumberFormat = _
"[>=1000000] #,##0.0,,""M"";[>0] #,##0.0,""K"";General"
.Select
.DataLabels.Select
Selection.Position = xlLabelPositionAbove
Selection.Format.TextFrame2.TextRange.Font.Italic = msoTrue
End If
End With

Range("H1").Select
End Sub
[/pre]
-----


Regards!
 
Hi, danrosey!

It's an statement used at module level to force explicit declaration of all variables in that module. Some people consider it one of the best practices as it helps to avoid ambiguous variable names or proper types.

BTW, if you're planning to use VBA code, either recorded, edited or downloaded/provided it's strongly recommended to start from the basics of VBA programming, a built in help is available pressing F1 from the VBA editor: once there it's matter of reading and further reading.

Regards!
 
Back
Top