If statement on Command Button Click

Pacman52

Active Member
Joined
Jan 29, 2009
Messages
319
Office Version
  1. 365
Platform
  1. Windows
Hi all,
Is it possible to have an If statement to check if a command button has been clicked? I've tried the line below but it doesnt seem to work.

VBA Code:
If cmbBut1 = True Then

Many thanks for any responses. Paul
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
I think you need to trap it

try this

VBA Code:
Private ButtonClicked As Boolean

Private Sub CommandButton1_Click()
    ButtonClicked = True
   
    DoWork
End Sub


Private Sub DoWork()
   ' do something
   
    ' reset whenever
    ButtonClicked = False
End Sub

Private Sub OtherStuff()
    If ButtonClicked Then
   
    Else
   
   
    End If
End Sub
 
Upvote 0
Solution
Hi thanks so much for the quick reply I'll give that a go later tonight when back home. One question though what is the purpose of the Sub 'DoWork' ? just so I understand what its meant for.

Thanks again Paul
 
Upvote 0
So that you can see how to interact with the variable that holds the setting. Your request is a bit vague to me. What should happen when clicked 2nd, 3rd, etc. time? Why does this even matter when there is an event that will run when clicked, which probably makes something happen. Would you not then know it was clicked because that something happened? That 'something' could probably tell you it was clicked by changing the button caption in that event.

Not that there's anything wrong with the suggestion, but another method would be to change the Tag property of the button. With that you could even have a count of clicks.
 
Upvote 0
I think you need to trap it

try this

VBA Code:
Private ButtonClicked As Boolean

Private Sub CommandButton1_Click()
    ButtonClicked = True
  
    DoWork
End Sub


Private Sub DoWork()
   ' do something
  
    ' reset whenever
    ButtonClicked = False
End Sub

Private Sub OtherStuff()
    If ButtonClicked Then
  
    Else
  
  
    End If
End Sub
Just to let you know that did the trick thanks again for the reply and help.

Paul
 
Upvote 0
So that you can see how to interact with the variable that holds the setting. Your request is a bit vague to me. What should happen when clicked 2nd, 3rd, etc. time? Why does this even matter when there is an event that will run when clicked, which probably makes something happen. Would you not then know it was clicked because that something happened? That 'something' could probably tell you it was clicked by changing the button caption in that event.

Not that there's anything wrong with the suggestion, but another method would be to change the Tag property of the button. With that you could even have a count of clicks.
Thanks for the reply and explanation - I was trying to sort out a userform 'cancel' button to run a yes / no msgbox and to run a 'Stop' on all the other msgboxes if a user opted for Yes. A bit more complex than that but it worked for what I needed so all is ok.

Paul
 
Upvote 0

Forum statistics

Threads
1,215,149
Messages
6,123,311
Members
449,095
Latest member
Chestertim

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