Почему моя кнопка VBA ведет себя странно при вызове onAction?

Я получаю следующее сообщение, когда вызывается метод onAction моей кнопки VBA. Макрос может быть недоступен в этой книге или все макросы могут быть отключены

В test.xlsm в файле ThisWorkbook у меня очень простой код

Option Explicit
Private Sub Workbook_Open()
    UpdateMenuSR
End Sub
Private Sub UpdateMenuSR()
Dim cb As CommandBarButton
Dim Solver As CommandBar

For Each Solver In Application.CommandBars
    If Solver.name = "Test" Then Exit Sub
Next Solver

Set Solver = Application.CommandBars.Add("Test", msoBarFloating, False)
With Solver
    .Visible = True
    With .Controls
        Set cb = .Add(Type:=msoControlButton)
        With cb
            .FaceId = 31
            .Visible = True
            .OnAction = "!b"
        End With
        Set cb = .Add(Type:=msoControlButton)
        With cb
            .FaceId = 19
            .Visible = True
            .OnAction = "!c"
        End With
        Set cb = .Add(Type:=msoControlButton)
        With cb
            .FaceId = 30
            .Visible = True
            .OnAction = "!a"
        End With
        Set cb = .Add(Type:=msoControlButton)
        With cb
            .FaceId = 8
            .Visible = True
            .OnAction = "!d"
        End With
   End With
End With
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim ctrl As CommandBarControl
Application.CommandBars("Test").Delete
For Each ctrl In Application.CommandBars("Tools").Controls
    If ctrl.Tag = "Test" Or ctrl.Tag = "Test" Then
        ctrl.Delete
    End If
Next ctrl
End Sub

В основном модуле у меня только

Public Sub a()
MsgBox "a"
End Sub


Public Sub d()
MsgBox "d"
End Sub


Public Sub b()
MsgBox "b"
End Sub


Public Sub c()
MsgBox "c"
End Sub

Если я сделаю следующую процедуру:

  • Создайте новый файл test2.xlsx

  • Откройте test.xlsm

  • Откройте test2.xslx

  • Нажимая по очереди на кнопки:

Результаты в: "b"

"Cannot run the macro '[test.xslm]Sheet1A:A'. The macro may not be available in this   workbook or all macros may be disabled"

"a"

"Cannot run the macro 'd'. The macro may not be available in this workbook or all macros may be disabled"
  • Переключитесь на test.xlsm

Результаты в:

 "b"

"Cannot run the macro '[test.xslm]Sheet1A:A'. The macro may not be available in this wo rkbook or all macros may be disabled"

 "a"

 "d"

Кто-нибудь может мне помочь?


person nikaan    schedule 02.11.2010    source источник


Ответы (1)


Я взломал проблему. Просто добавьте две невидимые кнопки:

    Set cb = .Add(Type:=msoControlButton)
    With cb
        .Visible = False
    End With

    Set cb = .Add(Type:=msoControlButton)
    With cb
        .Visible = False
    End With

Поэтому видимые кнопки работают правильно.

person nikaan    schedule 07.11.2010