2010-11-02 3 views
1

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

В 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" 

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

ответ

0

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

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

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

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

Смежные вопросы