[英]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.