插入按钮的代码有效,但在向按钮添加背景颜色时出现 438 错误
Code to insert button works but get 438 error when add background color to button
插入按钮的代码工作正常,但是当我尝试向按钮添加背景颜色时我得到 error 438
ActiveSheet.CommandButton1.BackColor = RGB(255, 0, 0)
我试过这个的变体,但我不明白
谢谢
Sub CreateButton()
Dim Obj As Object
Dim Code As String
Sheets(1).Select
'create button
Set Obj = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", _
Link:=False, DisplayAsIcon:=False, Left:=200, Top:=100, Width:=100, Height:=35)
Obj.Name = "CommandButton1"
'buttonn text
ActiveSheet.OLEObjects(1).Object.Caption = "Test Button"
'button color
ActiveSheet.CommandButton1.BackColor = RGB(255, 0, 0)
'macro text
Code = "Sub CommandButton1_Click()" & vbCrLf
Code = Code & "Call Tester" & vbCrLf
Code = Code & "End Sub"
'add macro at the end of the sheet module
With ActiveWorkbook.VBProject.VBComponents(Worksheets(1).CodeName).CodeModule
.insertlines .CountOfLines + 1, Code
End With
End Sub
Sub Tester()
MsgBox "You have click on the test button"
End Sub
您必须引用 OleObject
的 Object
对象。我知道这听起来有点多余,所以请参阅下面的简化代码,如果您需要更多帮助,请告诉我。
Sub GiveItAWhirl()
Dim oCmd As OLEObject
Set oCmd = Sheet1.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Left:=100, Top:=100, Width:=100)
oCmd.Object.BackColor = RGB(255, 0, 0)
End Sub
插入按钮的代码工作正常,但是当我尝试向按钮添加背景颜色时我得到 error 438
ActiveSheet.CommandButton1.BackColor = RGB(255, 0, 0)
我试过这个的变体,但我不明白
谢谢
Sub CreateButton()
Dim Obj As Object
Dim Code As String
Sheets(1).Select
'create button
Set Obj = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", _
Link:=False, DisplayAsIcon:=False, Left:=200, Top:=100, Width:=100, Height:=35)
Obj.Name = "CommandButton1"
'buttonn text
ActiveSheet.OLEObjects(1).Object.Caption = "Test Button"
'button color
ActiveSheet.CommandButton1.BackColor = RGB(255, 0, 0)
'macro text
Code = "Sub CommandButton1_Click()" & vbCrLf
Code = Code & "Call Tester" & vbCrLf
Code = Code & "End Sub"
'add macro at the end of the sheet module
With ActiveWorkbook.VBProject.VBComponents(Worksheets(1).CodeName).CodeModule
.insertlines .CountOfLines + 1, Code
End With
End Sub
Sub Tester()
MsgBox "You have click on the test button"
End Sub
您必须引用 OleObject
的 Object
对象。我知道这听起来有点多余,所以请参阅下面的简化代码,如果您需要更多帮助,请告诉我。
Sub GiveItAWhirl()
Dim oCmd As OLEObject
Set oCmd = Sheet1.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Left:=100, Top:=100, Width:=100)
oCmd.Object.BackColor = RGB(255, 0, 0)
End Sub