VBA Excel“;预期函数或变量“;按宏插入窗体控件按钮时出错
我想按宏创建表单控件按钮,如下所述: 并解释如下: 不幸的是,我突然得到了一个错误:调试器粗略地指向VBA Excel“;预期函数或变量“;按宏插入窗体控件按钮时出错,excel,vba,Excel,Vba,我想按宏创建表单控件按钮,如下所述: 并解释如下: 不幸的是,我突然得到了一个错误:调试器粗略地指向选择语句时出现了“预期的FONCUTION或variable” Sub SunButton() ' ' Macro1 Macro ' ' ActiveSheet.Buttons.Add(964.2, 119.4, 139.2, 49.8).Select Selection.OnAction = "Sun" Selection
选择
语句时出现了“预期的FONCUTION或variable”
Sub SunButton()
'
' Macro1 Macro
'
'
ActiveSheet.Buttons.Add(964.2, 119.4, 139.2, 49.8).Select
Selection.OnAction = "Sun"
Selection.Characters.Text = "Sun"
With Selection.Characters(Start:=1, Length:=3).Font
.Name = "Calibri"
.FontStyle = "Bold"
.Size = 16
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = 32
.Placement = xlFreeFloating
.PrintObject = False
End With
End Sub
我不知道这里会出什么问题
根据这条线索:
当您有另一个名为“selection”的宏,但我的案例中没有该宏时,就会发生这种情况
如何删除此错误并继续录制宏?您的代码错误,因为您正在将按钮属性应用于其字体。请尝试:
Sub SunButton()
'
' Macro1 Macro
'
'
With ActiveSheet.Buttons.Add(964.2, 119.4, 139.2, 49.8)
.OnAction = "Sun"
.Caption = "Sun"
With .Characters.Font
.Name = "Calibri"
.FontStyle = "Bold"
.Size = 16
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = 32
End With
.Placement = xlFreeFloating
.PrintObject = False
End With
End Sub
您的代码错误,因为您正在将按钮属性应用于其字体。请尝试:
Sub SunButton()
'
' Macro1 Macro
'
'
With ActiveSheet.Buttons.Add(964.2, 119.4, 139.2, 49.8)
.OnAction = "Sun"
.Caption = "Sun"
With .Characters.Font
.Name = "Calibri"
.FontStyle = "Bold"
.Size = 16
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = 32
End With
.Placement = xlFreeFloating
.PrintObject = False
End With
End Sub
无论如何,在可能的情况下避免“选择”是很好的
Sub testButtonCharDif()
Dim bt As Button
Set bt = ActiveSheet.Buttons.Add(964.2, 119.4, 139.2, 49.8)
With bt
.OnAction = "Sun"
.Characters.Text = "Sun"
With .Characters(Start:=1, length:=3).Font
.name = "Calibri"
.FontStyle = "Bold"
.size = 16
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = 32
End With
.Placement = xlFreeFloating
.PrintObject = False
End With
End Sub
无论如何,在可能的情况下避免“选择”是很好的
Sub testButtonCharDif()
Dim bt As Button
Set bt = ActiveSheet.Buttons.Add(964.2, 119.4, 139.2, 49.8)
With bt
.OnAction = "Sun"
.Characters.Text = "Sun"
With .Characters(Start:=1, length:=3).Font
.name = "Calibri"
.FontStyle = "Bold"
.size = 16
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = 32
End With
.Placement = xlFreeFloating
.PrintObject = False
End With
End Sub
非常感谢。选择带有录制的宏。在我的另一个示例中,它运行时没有问题,但在这里我感到惊讶。然而,你的方式很好地展望未来,我不再使用录制的宏,而是开始开发我自己的代码来设置按钮。非常感谢!非常感谢。选择来了这是一个录制的宏。在我的另一个示例中,它运行时没有问题,但我感到惊讶。不过,您的方式很适合未来,我不再使用录制的宏,而是开始开发自己的代码来设置按钮。非常感谢!