[SIZE=3].OnAction = [COLOR=red]"[/COLOR]'mymacro 99'[COLOR=red]"[/COLOR][/SIZE]
[SIZE=3].OnAction = [COLOR=red]"[/COLOR]'mymacro [COLOR=magenta]""[/COLOR]fred[COLOR=magenta]""[/COLOR]'[COLOR=red]"[/COLOR][/SIZE]
With ThisWorkbook.Sheets("Sheet1")
.OLEObjects.Add ClassType:="Forms.CommandButton.1", _
Height:=32.25, _
Left:=434.25, _
Top:=34.5, _
Width:=102.75
With b2.OLEObjects("CommandButton1")
.Name = "RecreateSheet"
.Object.Caption = "Recreate Sheet"
.Object.ForeColor = &HC0&
.OnAction = "[B][COLOR=red]'[/COLOR][/B]Update[B][COLOR=red]'[/COLOR][/B]"
End With
End With
b6 = "Private Sub Update_Click" & Chr(10) & Chr(10) & _
"Call Code.Update" & Chr(10) & Chr(10) & _
"End Sub" & Chr(10) & Chr(10) & _
"Private Sub Format_Click()" & Chr(10) & Chr(10) & _
"Do" & Chr(10) & Chr(10) & _
"i = i + 1" & Chr(10) & Chr(10) & _
"With ActiveSheet.Range(""M"" & i)" & Chr(10) & Chr(10) & _
" .Value = Range(""A"" & i) & "" - "" & Range(""B"" & i)" & Chr(10) & Chr(10) & _
"End With" & Chr(10) & Chr(10) & _
"Loop Until IsEmpty(Range(""A1"").Offset(i, 0))" & Chr(10) & Chr(10) & _
"End Sub" & Chr(10) & Chr(10) & _
"Private Sub Recreate_Click" & Chr(10) & Chr(10) & _
"Call Code.Query" & Chr(10) & Chr(10) & _
"End Sub"
'Inserts procedure
ThisWorkbook.VBProject.VBComponents(Sheets("Sheet1").CodeName).CodeModule.insertlines 1, b6
Option Explicit
Private WithEvents oCmb As CommandButton
Private Sub oCmb_Click()
MsgBox "Hello from " & "'" & oCmb.Caption & "'"
End Sub
Public Property Set GetCommandButton(ByVal Cmb As CommandButton)
Set oCmb = Cmb
End Property
Private oCmbInstance As CmbClass
Sub AddButton()
Dim Cmb As OLEObject
Set Cmb = ThisWorkbook.ActiveSheet.OLEObjects.Add _
(ClassType:="Forms.CommandButton.1", _
Height:=32, _
Width:=200, _
Left:=292, _
Top:=34, _
Width:=102)
Names.Add "ButtonID", Cmb.ShapeRange.ID
Cmb.Name = "MyCommandButton" & Cmb.ShapeRange.ID
Application.OnTime Now + TimeSerial(0, 0, 1), "HookButton"
End Sub
Private Sub HookButton()
Set oCmbInstance = New CmbClass
Set oCmbInstance.GetCommandButton = _
ActiveSheet.OLEObjects("MyCommandButton" & [ButtonID]).Object
Names("ButtonID").Delete
End Sub
After I read Juan's post in the other thread that was what I tried (that's the suggestion I was talking about in post #4). The same error message occurs.Code:With ThisWorkbook.Sheets("Sheet1") .OLEObjects.Add ClassType:="Forms.CommandButton.1", _ Height:=32.25, _ Left:=434.25, _ Top:=34.5, _ Width:=102.75 With b2.OLEObjects("CommandButton1") .Name = "RecreateSheet" .Object.Caption = "Recreate Sheet" .Object.ForeColor = &HC0& .OnAction = "[B][COLOR=red]'[/COLOR][/B]Update[B][COLOR=red]'[/COLOR][/B]" End With End With
Right now I'm reduced to inserting the code I want to use into the worksheet object module after I add it:
It's not pretty, but it works.Code:b6 = "Private Sub Update_Click" & Chr(10) & Chr(10) & _ "Call Code.Update" & Chr(10) & Chr(10) & _ "End Sub" & Chr(10) & Chr(10) & _ "Private Sub Format_Click()" & Chr(10) & Chr(10) & _ "Do" & Chr(10) & Chr(10) & _ "i = i + 1" & Chr(10) & Chr(10) & _ "With ActiveSheet.Range(""M"" & i)" & Chr(10) & Chr(10) & _ " .Value = Range(""A"" & i) & "" - "" & Range(""B"" & i)" & Chr(10) & Chr(10) & _ "End With" & Chr(10) & Chr(10) & _ "Loop Until IsEmpty(Range(""A1"").Offset(i, 0))" & Chr(10) & Chr(10) & _ "End Sub" & Chr(10) & Chr(10) & _ "Private Sub Recreate_Click" & Chr(10) & Chr(10) & _ "Call Code.Query" & Chr(10) & Chr(10) & _ "End Sub" 'Inserts procedure ThisWorkbook.VBProject.VBComponents(Sheets("Sheet1").CodeName).CodeModule.insertlines 1, b6
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Call AddDropDown
Cancel = True
End Sub
Sub AddDropDown()
Dim dd As DropDown
With ActiveCell
Set dd = Sheet1.DropDowns.Add(.Left, .Top, .Width, .Height)
End With
With dd
.Name = "dd1"
.OnAction = "SomeSub" 'Assign action when item is selected.
.AddItem "Item1"
.AddItem "Item2"
.AddItem "Item3"
End With
End Sub
Private Sub SomeSub()
With Sheet1.DropDowns(Application.Caller)
.TopLeftCell = .List(.ListIndex) 'Add selected value to cell beneath DropDown.
.Delete 'Delete DropDown.
End With
End Sub
Option Explicit
Private WithEvents oCmb As CommandButton
Private Sub oCmb_Click()
MsgBox "Hello from " & "'" & oCmb.Caption & "'"
End Sub
Public Property Set GetCommandButton(ByVal Cmb As CommandButton)
Set oCmb = Cmb
End Property
Private oCmbInstance As CmbClass
Sub AddButton()
Dim Cmb As OLEObject
Set Cmb = ThisWorkbook.ActiveSheet.OLEObjects.Add _
(ClassType:="Forms.CommandButton.1", _
Height:=32, _
Width:=200, _
Left:=292, _
Top:=34, _
Width:=102)
Cmb.Name = "MyCommandButton"
Call HookButton
End Sub
Private Sub HookButton()
Set oCmbInstance = New CmbClass
Set oCmbInstance.GetCommandButton = ActiveSheet.OLEObjects("MyCommandButton").Object
End Sub