Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Add a Button with code

Is there any way I can add a command button using code
when a new worksheet is added to a workbook, the button is
called formatsheet and it calls formatsheet(), its
position will be left 1.5, top 14.25 Height 24.5 its
caption is ReFormat Sheet and it is not to be printed

Thanks in Advance
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Add a Button with code

Here is a general code snippet that shows you how to do it, it adds a button
and some click event code for it.

'-----------------------------------------------------------------
Sub CreateControlButton()
'-----------------------------------------------------------------
Dim oWs As Worksheet
Dim oOLE As OLEObject

Set oWs = ActiveSheet

Set oOLE =
ActiveSheet.OLEObjects.Add(ClassType:="Forms.Comma ndButton.1", _
Left:=200, Top:=100, Width:=80, Height:=32)

'To set with a cell
'With Range("H2")
' Set oOLE =
ActiveSheet.OLEObjects.Add(ClassType:="Forms.Comma ndButton.1", _
' Left:=.Left, Top:=.Top, Width:=.Width,
Height:=.Height)
'End With

With oOLE
.Object.Caption = "Run myMacro"
.Name = "myMacro"
End With

With ThisWorkbook.VBProject.VBComponents(oWs.CodeName). CodeModule
.InsertLines .CreateEventProc("Click", oOLE.Name) + 1, _
vbTab & "If Range(""A1"").Value 0 Then " & vbCrLf & _
vbTab & vbTab & "Msgbox ""Hi""" & vbCrLf & _
vbTab & "End If"

End With

End Sub
'-----------------------------------------------------------------
Sub CreateCombobox()
'-----------------------------------------------------------------
Dim oWs As Worksheet
Dim oOLE As OLEObject

Set oWs = ActiveSheet

Set oOLE = ActiveSheet.OLEObjects.Add(ClassType:="Forms.Combo box.1", _
Left:=200, Top:=100, Width:=80, Height:=32)


oOLE.ListFillRange = "A1:A10"

End Sub

'-----------------------------------------------------------------


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Alan Smykowski" wrote in message
...
Is there any way I can add a command button using code
when a new worksheet is added to a workbook, the button is
called formatsheet and it calls formatsheet(), its
position will be left 1.5, top 14.25 Height 24.5 its
caption is ReFormat Sheet and it is not to be printed

Thanks in Advance



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Add a Button with code

Ok it will add the button but then I get the msgbox as
follows

Runtime error '1004'

Programmatic access to Visual Basic Project is not Trusted
-----Original Message-----
Here is a general code snippet that shows you how to do

it, it adds a button
and some click event code for it.

'---------------------------------------------------------

--------
Sub CreateControlButton()
'---------------------------------------------------------

--------
Dim oWs As Worksheet
Dim oOLE As OLEObject

Set oWs = ActiveSheet

Set oOLE =
ActiveSheet.OLEObjects.Add

(ClassType:="Forms.CommandButton.1", _
Left:=200, Top:=100, Width:=80,

Height:=32)

'To set with a cell
'With Range("H2")
' Set oOLE =
ActiveSheet.OLEObjects.Add

(ClassType:="Forms.CommandButton.1", _
' Left:=.Left, Top:=.Top,

Width:=.Width,
Height:=.Height)
'End With

With oOLE
.Object.Caption = "Run myMacro"
.Name = "myMacro"
End With

With ThisWorkbook.VBProject.VBComponents

(oWs.CodeName).CodeModule
.InsertLines .CreateEventProc("Click", oOLE.Name)

+ 1, _
vbTab & "If Range(""A1"").Value 0 Then " &

vbCrLf & _
vbTab & vbTab & "Msgbox ""Hi""" & vbCrLf & _
vbTab & "End If"

End With

End Sub
'---------------------------------------------------------

--------
Sub CreateCombobox()
'---------------------------------------------------------

--------
Dim oWs As Worksheet
Dim oOLE As OLEObject

Set oWs = ActiveSheet

Set oOLE = ActiveSheet.OLEObjects.Add

(ClassType:="Forms.Combobox.1", _
Left:=200, Top:=100, Width:=80,

Height:=32)


oOLE.ListFillRange = "A1:A10"

End Sub

'---------------------------------------------------------

--------


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Alan Smykowski"

wrote in message
...
Is there any way I can add a command button using code
when a new worksheet is added to a workbook, the button

is
called formatsheet and it calls formatsheet(), its
position will be left 1.5, top 14.25 Height 24.5 its
caption is ReFormat Sheet and it is not to be printed

Thanks in Advance



.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Add a Button with code

tools=Macro=Security, go to the trusted publishes and in the lower left
corner click
Trust Acces to Visual Basic Project

--
Regards,
Tom Ogilvy


"Alan Smykowski" wrote in message
...
Ok it will add the button but then I get the msgbox as
follows

Runtime error '1004'

Programmatic access to Visual Basic Project is not Trusted
-----Original Message-----
Here is a general code snippet that shows you how to do

it, it adds a button
and some click event code for it.

'---------------------------------------------------------

--------
Sub CreateControlButton()
'---------------------------------------------------------

--------
Dim oWs As Worksheet
Dim oOLE As OLEObject

Set oWs = ActiveSheet

Set oOLE =
ActiveSheet.OLEObjects.Add

(ClassType:="Forms.CommandButton.1", _
Left:=200, Top:=100, Width:=80,

Height:=32)

'To set with a cell
'With Range("H2")
' Set oOLE =
ActiveSheet.OLEObjects.Add

(ClassType:="Forms.CommandButton.1", _
' Left:=.Left, Top:=.Top,

Width:=.Width,
Height:=.Height)
'End With

With oOLE
.Object.Caption = "Run myMacro"
.Name = "myMacro"
End With

With ThisWorkbook.VBProject.VBComponents

(oWs.CodeName).CodeModule
.InsertLines .CreateEventProc("Click", oOLE.Name)

+ 1, _
vbTab & "If Range(""A1"").Value 0 Then " &

vbCrLf & _
vbTab & vbTab & "Msgbox ""Hi""" & vbCrLf & _
vbTab & "End If"

End With

End Sub
'---------------------------------------------------------

--------
Sub CreateCombobox()
'---------------------------------------------------------

--------
Dim oWs As Worksheet
Dim oOLE As OLEObject

Set oWs = ActiveSheet

Set oOLE = ActiveSheet.OLEObjects.Add

(ClassType:="Forms.Combobox.1", _
Left:=200, Top:=100, Width:=80,

Height:=32)


oOLE.ListFillRange = "A1:A10"

End Sub

'---------------------------------------------------------

--------


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Alan Smykowski"

wrote in message
...
Is there any way I can add a command button using code
when a new worksheet is added to a workbook, the button

is
called formatsheet and it calls formatsheet(), its
position will be left 1.5, top 14.25 Height 24.5 its
caption is ReFormat Sheet and it is not to be printed

Thanks in Advance



.



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,758
Default Add a Button with code

And just to add to Tom's response...

This is a setting that doesn't travel with the workbook--it's a user setting.
If other users are going to run this code, they'll have to change this setting,
too.



Tom Ogilvy wrote:

tools=Macro=Security, go to the trusted publishes and in the lower left
corner click
Trust Acces to Visual Basic Project

--
Regards,
Tom Ogilvy

"Alan Smykowski" wrote in message
...
Ok it will add the button but then I get the msgbox as
follows

Runtime error '1004'

Programmatic access to Visual Basic Project is not Trusted
-----Original Message-----
Here is a general code snippet that shows you how to do

it, it adds a button
and some click event code for it.

'---------------------------------------------------------

--------
Sub CreateControlButton()
'---------------------------------------------------------

--------
Dim oWs As Worksheet
Dim oOLE As OLEObject

Set oWs = ActiveSheet

Set oOLE =
ActiveSheet.OLEObjects.Add

(ClassType:="Forms.CommandButton.1", _
Left:=200, Top:=100, Width:=80,

Height:=32)

'To set with a cell
'With Range("H2")
' Set oOLE =
ActiveSheet.OLEObjects.Add

(ClassType:="Forms.CommandButton.1", _
' Left:=.Left, Top:=.Top,

Width:=.Width,
Height:=.Height)
'End With

With oOLE
.Object.Caption = "Run myMacro"
.Name = "myMacro"
End With

With ThisWorkbook.VBProject.VBComponents

(oWs.CodeName).CodeModule
.InsertLines .CreateEventProc("Click", oOLE.Name)

+ 1, _
vbTab & "If Range(""A1"").Value 0 Then " &

vbCrLf & _
vbTab & vbTab & "Msgbox ""Hi""" & vbCrLf & _
vbTab & "End If"

End With

End Sub
'---------------------------------------------------------

--------
Sub CreateCombobox()
'---------------------------------------------------------

--------
Dim oWs As Worksheet
Dim oOLE As OLEObject

Set oWs = ActiveSheet

Set oOLE = ActiveSheet.OLEObjects.Add

(ClassType:="Forms.Combobox.1", _
Left:=200, Top:=100, Width:=80,

Height:=32)


oOLE.ListFillRange = "A1:A10"

End Sub

'---------------------------------------------------------

--------


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Alan Smykowski"

wrote in message
...
Is there any way I can add a command button using code
when a new worksheet is added to a workbook, the button

is
called formatsheet and it calls formatsheet(), its
position will be left 1.5, top 14.25 Height 24.5 its
caption is ReFormat Sheet and it is not to be printed

Thanks in Advance


.


--

Dave Peterson


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
command button code dummy Excel Discussion (Misc queries) 2 December 1st 09 02:57 PM
configure add button & code [email protected] uk Excel Discussion (Misc queries) 7 November 24th 08 02:34 PM
VBA code behind command button [email protected] Excel Worksheet Functions 1 March 22nd 06 08:13 PM
Button, but no code Aaron Fude Excel Programming 3 May 16th 04 05:32 PM
Button, but now code Aaron Fude Excel Programming 2 May 16th 04 10:35 AM


All times are GMT +1. The time now is 04:00 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"