Enable/Disable Macro Button

Kevin0427

Board Regular
Joined
Mar 31, 2016
Messages
69
Hey there. I have a Workbook with several Sheets. One Sheet (Data Entry Form) has a button on it that runs a macro to save the data entered to a separate sheet (Real Data).

I would like for that button to be disabled at all times unless the data the user entered meets certain criteria. I have a cell (ValidData) on a third sheet (Calculations) that determines that. If ValidData = 0 the button should be disabled and if ValidData= 1 the button should be enabled. This should update anytime the workbook is changed so that it is always correct. And the macro won't run if the data the user entered is wrong.

I am very new at VBA so go easy on me !! LOL
 
He is a simple example:
Code:
Sub Data_Check()
'Modified  8/30/2018  2:30:18 PM  EDT
If Range("DataValid") = 1 Then
MsgBox "Hello"
Else
MsgBox "DataValid does not equal 1  I will stop script": Exit Sub
End If
End Sub
 
Upvote 0

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
The following assumes: (a) the button is an ActiveX button and not a forms button and (b) your named range "ValidData" is scoped to the workbook level and not scoped to the Calculations worksheet.

Code:
Private Sub Worksheet_Calculate()
    Me.CommandButton1.Enabled = Me.Range("ValidData")
End Sub
 
Last edited:
Upvote 0
Like this ?

If Range("DataValid") = 1 Then

Existing Marco Here

Else
Msgbox "Invalid Data. Please check your work and try again."
 
Upvote 0
And of course I need an If End If before the End Sub.

Does DataValid need to be globally defined as Greg indicated? If so, how is that done.
 
Last edited:
Upvote 0
I never like to comment on what other posters may suggest.
I see nothing in his post here about
globally defined


 
Upvote 0
My bad. "scoped to theWorkbook" presumably because the button and the ValidData cell are on different sheets. Does that matter ?

Your code works perfectly without the scope change unless I defined it like that by mistake when I did it. Thanks a Million !!!!
 
Upvote 0
Glad I was able to help you.
Come back here to Mr. Excel next time you need additional assistance.

There are always 10 different ways to do everything in Excel:
Which way is the best or fastest I'm sure is always up for discussion.

Like you could write a script.

And then in the button only enter: Call Sally

If your original script was name Sally

This way you could easily assign the same script to several buttons. Without having to put same script in each button.

And you could just enter: Sally

See how there are always different ways to do things

Nice talking with you. Take care
 
Upvote 0
My bad. "scoped to theWorkbook" presumably because the button and the ValidData cell are on different sheets. Does that matter ?


It only matters if you use the code I posted. Note that the code is tied to the _Calculate event handler for the Worksheet that has the button and that the call to the .Range method is prefixed by the Me. object. Even though, technically it shouldn't let you get away with doing this when the "parent" of the name is the workbook, Excel will let you slide this reference in.


Rich (BB code):
Private Sub Worksheet_Calculate()
    Me.CommandButton1.Enabled = Me.Range("ValidData")
End Sub


As My Answer said, there are almost always several ways of skinning Excel cats. Pick whichever one you feel is the best fit and learn from the rest if you have the time and inclination. You had asked to actually disable the button - my code actually does disable the button.


However, there are many, many cases where — instead of toggling the .enabled property of a control — I've just had the procudure the control was calling send a message and exit if some validity check failed. This is especially true when I've been coding buttons that are called from the ribbon. Trying to get into all the happy jack surrounding coding the enabled callbacks and slipping those attributes into the Ribbon's xml is tiresome and tedious. Not to mention trying to figure out which application events to trap to invalidate the control to force Excel to raise the event to call the .enabled callback. Usually just easier to error message and exit.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,218
Messages
6,123,676
Members
449,116
Latest member
HypnoFant

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