CommandButton question

Abeladoni

New Member
Joined
Nov 8, 2004
Messages
6
Greetings all,

Hopefully someone can help me with a small problem I've been having. I've got a workbook that has a template sheet in it. When activated the workbook pulls data from a database, then copies the template sheet and places the data into the copied template sheet and formats it. The new sheet that is created is renamed based upon the data pulled from the database. For example the template sheet is copied, the data is entered and formatted, then the sheet would be renamed 5R04.

Now the problem that I'm having is a button that's on the template sheet. For testing purposes the only thing the button does now is to pop up a message box. Now when the new sheet is created, I renamed the button as well. Also based up on the data pulled from the database. So in this example the button would be renamed CommandButton5R04 (the original button name is just CommandButton1).

Now the problem that i'm having is to actually get the command button to DO anything. Initially I wanted it to call a function that would modify the data on the new sheet (once again based upon the data pulled from the db). However it seems to just ignore the call. I've even tried creating a function named 5R04_Click(), and it ignores that as well. Okay, it's early and my brain isn't functioning at full capacity. But what am I missing here? Any help would be appreciated. Below are some of the code snippets relevant to the problem:

Dim cmdButton As OLEObject
Set cmdButton = ActiveSheet.OLEObjects("commandbutton1")
With cmdButton
.Name = "5R04"
End With

Private Sub 5R04_Click()
MsgBox ("Succesful Test")
End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

Juan Pablo González

MrExcel MVP
Joined
Feb 8, 2002
Messages
11,959
Instead, add a button from the Forms toolbar, and use the .OnAction property to assign a macro to it. It's much easier than adding code through code...
 

R_Stephens

Board Regular
Joined
Dec 23, 2004
Messages
143
Hi there,

One reason may be that VBA does not like sub names beginning with numbers, could you possibly try the code putting an A or something in front?

Hope this helps

Richard
 

Forum statistics

Threads
1,147,510
Messages
5,741,582
Members
423,669
Latest member
necat02

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
Top