繁体   English   中英

通过宏插入表单控制按钮时出现 VBA Excel “预期 Function 或变量”错误

[英]VBA Excel “Expected Function or variable” error when inserting form control button by macro

我想通过宏创建表单控制按钮,如下所述:

通过 VBA 插入表单控制按钮

在运行时将命令按钮添加到工作表并定义事件

并在这里解释:

https://www.mrexcel.com/board/threads/vba-code-to-create-macro-insert-form-control-button-assign-macro-to-button.832394/

不幸的是,突然我收到一个错误:预期的函数或变量”,调试器大致指向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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM