Macro assigned to a command button

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,922
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have the following code attached to a command button, which is found inside a worksheet (rather than in a module):
Code:
Private Sub ToggleCalc_Click()

With Application
    If .Calculation = xlCalculationAutomatic Then
        .Calculation = xlCalculationManual
        With ToggleCalc
            .BackColor = RGB(240, 128, 128)
            .Caption = "Calc is: Manual"
        End With
    Else
        .Calculation = xlCalculationAutomatic
        With ToggleCalc
            .BackColor = RGB(176, 224, 230)
            .Caption = "Calc is: Automatic"
        End With
    End If
End With

End Sub
I also have existing macros within modules in this same workbook. If I want to call the above from an existing macro how can I do this? Simply typing:
Code:
ToggleCalc_Click
or
Code:
Run ToggleCalc_Click
Doesn't work, error message is: Compile error: Variable not defined; I have an understanding of what this means but I don't know how to resolve this issue as I've not come across it before.

Thanks in advance for any help,
Jack
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
You would need to make it public and use:
Code:
Sheet1.ToggleCalc_Click
for instance, or use Run but include the sheet code name as above.
As a rule, I would say that if you need to run the code from other locations, it doesn't belong in the worksheet code module - it should be a normal routine that you call from each required routine.
 
Upvote 0
Thank you Rory, I'll try out your suggestions and see if I can move it out of the sheet code and into a module
 
Upvote 0

Forum statistics

Threads
1,224,559
Messages
6,179,517
Members
452,921
Latest member
BBQKING

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top