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.