K
keiji kounoike
test the code below on new workbook. if it success, then try this one on
your workbook with problems. if this fails, then check if the added
worksheet name and worksheet object's name(codename) is the same. In
VBE, you can see something like sheet1(sheet1) in project explore.
sheet1 without parentheses is the object name(codename) and sheet1 with
parentheses is the worksheet name.
Sub macrotest()
macro1
macro2
End Sub
Sub macro1()
MsgBox "Macro1"
End Sub
Sub macro2()
MsgBox "macro2"
Worksheets.Add
macro3
End Sub
Sub macro3()
Dim ObjNG As Object
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
Dim LineNum As Long
Set ObjNG = ActiveSheet.OLEObjects.Add _
(ClassType:="Forms.CommandButton.1", Link:=False, _
DisplayAsIcon:=False, Left:=201.75, Top:=12.75, _
Width:=99.75, Height:=21.75)
ObjNG.Name = "GreenFlag"
ActiveSheet.OLEObjects(1).Object.Caption = "Green Flag (Ctrl + g)"
ActiveSheet.OLEObjects(1).Object.BackColor = vbGreen
Set VBProj = ActiveWorkbook.VBProject
Set VBComp = ActiveWorkbook.VBProject.VBComponents(ActiveSheet.Name)
Set CodeMod = VBComp.CodeModule
With CodeMod
LineNum = .CountOfLines + 1
.InsertLines LineNum, "Private Sub GreenFlag_Click()"
LineNum = LineNum + 1
.InsertLines LineNum, "msgbox ""Green"""
LineNum = LineNum + 1
.InsertLines LineNum, "End Sub"
End With
End Sub
Keiji
your workbook with problems. if this fails, then check if the added
worksheet name and worksheet object's name(codename) is the same. In
VBE, you can see something like sheet1(sheet1) in project explore.
sheet1 without parentheses is the object name(codename) and sheet1 with
parentheses is the worksheet name.
Sub macrotest()
macro1
macro2
End Sub
Sub macro1()
MsgBox "Macro1"
End Sub
Sub macro2()
MsgBox "macro2"
Worksheets.Add
macro3
End Sub
Sub macro3()
Dim ObjNG As Object
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
Dim LineNum As Long
Set ObjNG = ActiveSheet.OLEObjects.Add _
(ClassType:="Forms.CommandButton.1", Link:=False, _
DisplayAsIcon:=False, Left:=201.75, Top:=12.75, _
Width:=99.75, Height:=21.75)
ObjNG.Name = "GreenFlag"
ActiveSheet.OLEObjects(1).Object.Caption = "Green Flag (Ctrl + g)"
ActiveSheet.OLEObjects(1).Object.BackColor = vbGreen
Set VBProj = ActiveWorkbook.VBProject
Set VBComp = ActiveWorkbook.VBProject.VBComponents(ActiveSheet.Name)
Set CodeMod = VBComp.CodeModule
With CodeMod
LineNum = .CountOfLines + 1
.InsertLines LineNum, "Private Sub GreenFlag_Click()"
LineNum = LineNum + 1
.InsertLines LineNum, "msgbox ""Green"""
LineNum = LineNum + 1
.InsertLines LineNum, "End Sub"
End With
End Sub
Keiji