Trigger CommandButton1 using code. Can I??? Help PLEASE!

Mister H

Well-known Member
Joined
Mar 6, 2002
Messages
1,507
Hi All:

Can a Command Button be triggered using code? I know how to trigger other macros ( Application.Run "MacroNameHere" ) but I am not sure if the same thibng can be done for Command Buttons. Can it?

I want my macro to trigger CommandButton1...

Any assistance would be appreciated. I did a quick search but I must not be using the correct key words.

THANKS,
Mark :confused:
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
I am sure that there are other ways to do this that are more efficient, but how about having your command button call a macro, the same one that you want to call through code?
 
Upvote 0
Can't get that to work for me

Hi Seti:

THANKS for the reply. I can't get your solution to work. I think the reason is the fact that this button actually changes name after it is clicked. I got this code from a buddy and it eliminates the need for two buttons. Anyway, I just tried putting it as a macro but I get an error "Object Missing". Here is the code in case anyone has a use for it.

Does anyone know how I can get this to fucntion for me? THANKS.

Code:
Private Sub CommandButton1_Click()
'''''''''''''''''''''''''''''''''''''

' Hide_Zero_Rows Macro


Dim tmprow As Integer
Dim tmpsel As String
Dim TFAction As Boolean

ActiveSheet.Unprotect "unlock"
Application.ScreenUpdating = False


TFAction = True
If CommandButton1.Caption = "HIDE Blank Rows" Then
    TFAction = True
    CommandButton1.Caption = "SHOW Blank Rows"
Else
    TFAction = False
    CommandButton1.Caption = "HIDE Blank Rows"
End If

tmprow = 16
Do While Cells(tmprow, 1) <> "Bottom"
    tmprow = tmprow + 1
Loop


tmprow = tmprow - 1

Do
   
   If (Cells(tmprow, 1) = "" Or 0 And Cells(tmprow, 8) = "" Or 0) Then '1 is Column A and 14 is Column N
        tmpsel = tmprow & ":" & tmprow
        Range(tmpsel).EntireRow.Hidden = TFAction
 
    End If
    tmprow = tmprow - 1
Loop Until tmprow = 16

Application.ScreenUpdating = True
ActiveSheet.Protect "unlock"
End Sub

Take Care,
Mark :p
 
Upvote 0
Hi All:

Nobody had an answer for me which is a little unusual for this Board :)

I went and did a little Googling and then wound up getting this answer from Chip Pearson (THANKS Chip):

Change the declaration of the Click procedure from "Private" to "Friend" and then call the Click function as a normal procedure.

Code:
Sheet1.CommandButton1_Click	' if the button is on the worksheet

OR

Code:
UserForm1.CommandButton1_Click ' if the button is on a userform

Again THANKS to Chip for this info...
[url]www.cpearson.com
[/url]

Have a GREAT day ALL. :biggrin: :) :confused: :p
 
Upvote 0
Mark

Glad you found a solution.:)

I think part of the reason you didn't get many replies is because it doesn't really seem logical, to me anyway, to use code to call an event.

Not saying it can't be done though, and you seemed to have it sorted.

By the way as far as I can see the button isn't actually changing name in that code, it's changing it's caption.
 
Upvote 0
Sorry I missed reading this post.
It is a bit unusual as Norie says, but I did this exact call in code the other day. Glad you found solution. :wink: TT
 
Upvote 0

Forum statistics

Threads
1,214,424
Messages
6,119,407
Members
448,894
Latest member
spenstar

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