0

I would like to create the form control button by macro, as discussed here:

Insert form control button via VBA

Adding command buttons to worksheet at run time and also define events

and explained here:

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

Unfortunately, suddenly I got an error: Expected fonctuon or variable" with debugger pointing roughly the Selection statement.

  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

I don't know what can be wrong here.

enter image description here

According to this thread:

https://www.mrexcel.com/board/threads/compile-error-expected-function-or-variable.308885/

it occurs, when you have another macro called "selection" but I don't have it in my case.

How can I remove this error and proceed my recorded macro?

0

2 Answers 2

1

Your code is wrong, as you are applying button properties to its Font. Try:

  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
Sign up to request clarification or add additional context in comments.

Comments

1

Anyhow, it is good to avoid 'Selection' when possible...

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

1 Comment

Thank you very much. The Selection comes with a recorded macro. In my other example, it was working without the problem, but here I was surprised. However your way is good-looking for the future, where I won't use recorded macro again, but start to develop my own code in order to set the button. Thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.