View Full Version : [SOLVED:] Control the command button
minghong
07-31-2005, 02:39 PM
Hi! All, Does anybody know how to use some VBA code in macro to control the command button on the worksheet? That means the code is used the same way as people click the command button. Thanks a lot! :think:
Norie
07-31-2005, 02:52 PM
What do you mean?
Do you want to run the code associated with a command button from some other code?
minghong
07-31-2005, 04:21 PM
Yes, norie. You can say it that way.
Justinlabenne
07-31-2005, 04:38 PM
I have no idea what your after here, but if your looking to add code to a commandbutton on a worksheet that came from the Control Toolbox toolbar:
Private Sub CommandButton1_Click()
MsgBox "Messagebox!"
End Sub
If you have any code your using or even a sample workbook, attach it or post the code. There are quite a few different ways to do what you asked if I understand correctly, so some more insight would help out.
minghong
07-31-2005, 07:24 PM
Thanks! Justin. My problem is I have already generated a commandbutton which has some code in it. And I want to write a new code which can work to click the commandbutton and run the code in it at the same time. thanks!
Ivan F Moala
07-31-2005, 08:20 PM
When do you want to run this code which is the same as the commandbutton code ?
geekgirlau
08-01-2005, 12:26 AM
The simplest option is to cut and paste the code from the command button to a new sub, then have the command button and your new macro call the same sub. For example:
Private Sub CommandButton1_Click()
MyNewSub
End Sub
The simplest option is to cut and paste the code from the command button to a new sub, then have the command button and your new macro call the same sub. For example:
Private Sub CommandButton1_Click()
MyNewSub
End Sub
This is the approach that I use often; even when the command button is the only method by which the procedure is called. I think it makes for more useful, modular and easier-to-understand code.
minghong
08-01-2005, 08:26 AM
The simplest option is to cut and paste the code from the command button to a new sub, then have the command button and your new macro call the same sub. For example:
Private Sub CommandButton1_Click()
MyNewSub
End Sub
Thanks! Geekgirlau. I already use the same code like your example, but when I want to run the code (" MyNewSub" in your example) in the commandbutton, I need to click the button on the workshee. And my problem is I want to write another code which can make the process "click CommandButton1 to run MyNewSub" automatically without clicking any button.
Thanks! Geekgirlau. I already use the same code like your example, but when I want to run the code (" MyNewSub" in your example) in the commandbutton, I need to click the button on the workshee. And my problem is I want to write another code which can make the process "click CommandButton1 to run MyNewSub" automatically without clicking any button.
If I understand things ...
1. you have some code that runs when, say, CmdBtn1, is clicked.
2. you wish to have the same code run when some other stuff occurs
As suggested earlier, you need to:
1. take the code presently in the sub CmdBtn1_Click and move it to a new procedure, say, MyNewSub
2. the code in CmdBtn1_Click should just call MyNewSub and exit, for example
Sub CmdBtn1_Click
Call MyNewSub
End Sub
3. where ever else you want to "do whatever CmdBtn1_Click originally did", just call the sub MyNewSub, for example:
Sub SomeOtherSub
.
.
.
Call MyNewSub
.
.
.
End Sub
minghong
08-01-2005, 02:27 PM
Thanks a lot! MWE, Your explanation is crystal. Thank you!
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.