4

i need to click a button(FormControl) on an excel sheet and run the macro assigned to it, through VBA code.

I tried what was suggested here https://stackoverflow.com/questions/130166/clicking-command-button-from-other-workbook but it didn't work for me.

Is there any other way to do this ??

1
  • why not? What didn't work? Any errors? Commented Sep 27, 2012 at 19:47

4 Answers 4

8

Fairly easy method for doing this, even if it's not really documented.

CommandButton1.Value = True
Sign up to request clarification or add additional context in comments.

1 Comment

This allowed me to click a button on a Userform where the button's event code was Private.
3

Did you actually try? Because you want exaclty what is suggested there.

Application.Run Activesheet.Shapes(1).OnAction

Comments

0

If in a worksheet. I used this in the past and it worked!

Sheet1.CommandButton1_Click

else

Sheets("Sheet Name").CommandButton1_Click

1 Comment

Nice, it worked. But in my tests it only works if you remove the Private from the _Click Sub.
0

In my case, I needed to add a button, say on Sheet1, to call the code of CommandButton1 on, say, Sheet2. First I had to delete the 'Private' statement that was in front of CommandButton1_Click(). Then I had to call Worksheets("Sheet2").Activate before calling Worksheets("Sheet2").CommandButton1_Click. Then it worked. You can then add Worksheets("Sheet1").Activate to get you back to your original sheet. So:

Private Sub CommandButton1_Click()
Worksheets("Sheet2").Activate
Worksheets("Sheet2").CommandButton1_Click
Worksheets("Sheet1").Activate
End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.