[英]VBA Excel “Expected Function or variable” error when inserting form control button by macro
我想通过宏创建表单控制按钮,如下所述:
并在这里解释:
不幸的是,突然我收到一个错误:预期的函数或变量”,调试器大致指向Selection
语句。
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
我不知道这里有什么问题。
根据这个线程:
https://www.mrexcel.com/board/threads/compile-error-expected-function-or-variable.308885/
当您有另一个名为“选择”的宏但我没有它时,它就会发生。
如何删除此错误并继续录制的宏?
您的代码是错误的,因为您将按钮属性应用于其字体。 尝试:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.