Open worksheet and create form button if it doesn't exist

Byron

New Member
Joined
Oct 23, 2002
Messages
28
Upon opening a Excel Workbook I need to display a form button that will run a macro if the form button does not exist.

I know how to assign the macro to the form button. But how do I make sure it exist and is visible upon opening the workbook?
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Re: Open worksheet and create form button if it doesn't exis

Hi there,

First things first, I need to give credit to Nate Oliver for writing 90% of this code (and teaching me how it all works.) I've modified it to fit what you need...

Code:
Sub Add_Command_Buttons()
'Macro created 07/06/03 by Nate Oliver, modified 07/14/03 by Ken Puls
'Macro purpose: To create acommand buttons on the worksheet if necessary

'SECTION 1
'Declare required variables and working environment
    Dim cl As Range, Ctrl As OLEObject
    Application.ScreenUpdating = False

'SECTION 2
'Change name of button, if it exists.  If not, error will occur and send to errortrap
    On Error GoTo Createbutton
        Set Ctrl = ActiveSheet.OLEObjects("CommandButton1")
    On Error GoTo 0
        Application.ScreenUpdating = True
    Exit Sub

'SECTION 3
'Create button if necessary (this code will not fire if any button exists)
Createbutton:
    Set cl = Range("A1")
    Set Ctrl = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", _
        Left:=cl.Left + 1, Top:=cl.Top + 1, Width:=cl.Width * 2, Height:=cl.Height * 2)
    With Ctrl
        .Name = "CommandButton1" 'you can call it anything you like here
        .Placement = xlMove
        .PrintObject = False
        With .Object   'sets what is displayed on the button
            .Caption = "Click me!"
            .Enabled = True
            .Font.Name = "Times New Roman"
            .Font.Size = 10
            .Font.Bold = True
            .TakeFocusOnClick = False
            .WordWrap = True
        End With
    End With

Application.ScreenUpdating = True
End Sub

You would want to include this code in the Workbook_Open event.

FYI, this only sets up the button, it does not add the code. That is something I haven't had time to try yet, but I did find a MKBA on how to do this: http://support.microsoft.com/default.aspx?scid=kb;en-us;194611

HTH,
 
Upvote 0
Re: Open worksheet and create form button if it doesn't exis

Hmmm, not sure why you wouldn't know or not whether a button was there, but not nice proc. Ken. :) Note this ActiveX versus a form button, better in my estimation. Also note, one enters the twilight zone regarding ActiveX controls and VBA, there is some fairly odd business resulting from this, and it hasn't been fixed in XP. Never-the-less...
FYI, this only sets up the button, it does not add the code. That is something I haven't had time to try yet, but I did find a MKBA on how to do this: http://support.microsoft.com/default.aspx?scid=kb;en-us;194611
Let's add to it so that it adds a worksheet event, tieing a procedure to the button:

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> Add_Command_Buttons()
<SPAN style="color:#007F00">'Macro created 07/06/03 by Nate Oliver, modified 07/14/03 by Ken Puls _
    Modified 7/19/03 by Nate Oliver :D</SPAN>

<SPAN style="color:#007F00">'Macro purpose: To create acommand buttons on the worksheet if necessary</SPAN>

<SPAN style="color:#007F00">'SECTION 1</SPAN>
<SPAN style="color:#007F00">'Declare required variables and working environment</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> cl <SPAN style="color:#00007F">As</SPAN> Range, Ctrl <SPAN style="color:#00007F">As</SPAN> OLEObject
    Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN>

<SPAN style="color:#007F00">'SECTION 2</SPAN>
<SPAN style="color:#007F00">'Change name of button, if it exists.  If not, error will occur and send to errortrap</SPAN>
    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> Createbutton
        <SPAN style="color:#00007F">Set</SPAN> Ctrl = ActiveSheet.OLEObjects("CommandButton1")
    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> 0
        Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN>
    <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

<SPAN style="color:#007F00">'SECTION 3</SPAN>
<SPAN style="color:#007F00">'Create button if necessary (this code will not fire if any button exists)</SPAN>
Createbutton:
    <SPAN style="color:#00007F">Set</SPAN> cl = Range("A1")
    <SPAN style="color:#00007F">Set</SPAN> Ctrl = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", _
        Left:=cl.Left + 1, Top:=cl.Top + 1, Width:=cl.Width * 2, Height:=cl.Height * 2)
    <SPAN style="color:#00007F">With</SPAN> Ctrl
        .Name = "CommandButton1" <SPAN style="color:#007F00">'you can call it anything you like here</SPAN>
        .Placement = xlMove
        .PrintObject = <SPAN style="color:#00007F">False</SPAN>
        <SPAN style="color:#00007F">With</SPAN> .Object   <SPAN style="color:#007F00">'sets what is displayed on the button</SPAN>
            .Caption = "Click me!"
            .Enabled = <SPAN style="color:#00007F">True</SPAN>
            .Font.Name = "Times New Roman"
            .Font.Size = 10
            .Font.Bold = <SPAN style="color:#00007F">True</SPAN>
            .TakeFocusOnClick = <SPAN style="color:#00007F">False</SPAN>
            .WordWrap = <SPAN style="color:#00007F">True</SPAN>
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
    
<SPAN style="color:#007F00">'SECTION 4</SPAN>
<SPAN style="color:#007F00">'Tie a procedure to the ActiveX Control, Based on Chip Pearson's Example</SPAN>

    <SPAN style="color:#00007F">With</SPAN> ThisWorkbook.VBProject.VBComponents(ActiveSheet.CodeName).CodeModule
        LineNum = .CountOfLines + 1
        .InsertLines LineNum, _
            "Private Sub CommandButton1_Click()" & vbLf & _
            "     Msgbox ""Here is the new procedure"" " & vbLf & _
            "End Sub"
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>

Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN>
End <SPAN style="color:#00007F">Sub</SPAN></FONT>

Obviously modify the string to suit your needs. You might want to add a worksheet object to define the sheet you're working with, this code will fire on the activesheet, by design.
 
Upvote 0
Re: Open worksheet and create form button if it doesn't exis

Hi Nate,

I just tried to use your modified code of my modified code of your unmodified code... :eek:

After dim'ng LineNum as an integer, (it didn't like it without,) I ran the code, but received an error that kind of threw me off. Can you tell me how to deal with this one?

Run Time Error '1004'
Programmatic access to Visual Basic Project is not trusted.

(Maybe my computer is just smarter than I thought and knows I'm about to do something dangerous!) :LOL:

Thanks,
 
Upvote 0
Re: Open worksheet and create form button if it doesn't exis

Ah yes, I forgot to explicitly state that variable, how silly of me, sounds like you have option explicit going in your workbook.

In any case, I imagine you're using XP. Go into Tools->Macro->Security. There should be something there about accessing the VBE. I don't have XP in front of me at the moment and am at a bit of loss to recall what it exactly looks like, I think it's obvious though.
 
Upvote 0
Re: Open worksheet and create form button if it doesn't exis

Thank you!

Not only does that let me run the afformentioned code, but it also allows me to run the VB HTML maker and Colo's HTMLMaker runs without telling me to manually set some VBIDE thing!

You've cleared up 3 problems in one shot! :pray:

For the record, I do use option explicit in all my workbooks (that way I get closer to understanding what I'm trying to accomplish!) :biggrin:

Also, the VBE thing is obvious when you know where to look. It's on the "Trusted Sources" tab of the "Macro Security" settings.

Thanks again!
 
Upvote 0
Re: Open worksheet and create form button if it doesn't exis

kpuls said:
Thank you!

You've cleared up 3 problems in one shot! :pray:

For the record, I do use option explicit in all my workbooks (that way I get closer to understanding what I'm trying to accomplish!) :biggrin:

You're welcome! Phew, I remembered it being apparent once you were in the correct dialog.

Re: O.E., good habbit. I do it on a case by case basis, but I should all the time, bad habbits die hard. :biggrin:

Have a good one eh.
 
Upvote 0
Re: Open worksheet and create form button if it doesn't exis

In Excel 2007 the road to the setting is:

Excel Options >> Trust Center >> Trust Center Settings >> Macro Settings >> Check "Trust access to the VBA Project Object Model"
 
Upvote 0

Forum statistics

Threads
1,215,464
Messages
6,124,969
Members
449,200
Latest member
Jamil ahmed

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