Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XL 2003 Menu Question 1

Status
Not open for further replies.

RonRepp

Technical User
Feb 25, 2005
1,031
US
Hi, all:

I have a few procedures that create a menu (called "Worksheets") and then adds the sheets of the active workbook. However, when I try to run it, when I click on the sheet name, I always get the last menu name.

Code:
Public Sub AddWorksheetsMenu()
Dim myMenuBar As CommandBar
Dim i As Integer
Set myMenuBar = CommandBars.ActiveMenuBar
Set newMenu = myMenuBar.Controls.Add(Type:=msoControlPopup, Temporary:=True)
newMenu.Caption = "Worksheets"
For i = 1 To ActiveWorkbook.Worksheets.Count
    'MsgBox Worksheets(i).Name

'Caller = Worksheets(i).Name

Set ctrl1 = newMenu.CommandBar.Controls _
    .Add(Type:=msoControlButton, ID:=i)
With ctrl1
    .Caption = ActiveWorkbook.Worksheets(i).Name
    .TooltipText = ActiveWorkbook.Worksheets(i).Name
    .Style = msoButtonCaption
    .OnAction = ActiveWorkbook.Worksheets(i).Name '"GoToSheet"
End With
Next i

End Sub

Public Sub DeleteWorksheetsMenu()
    On Error Resume Next
    
    Application.CommandBars("Worksheet Menu Bar").Controls("Worksheets").Delete
End Sub

Public Sub GoToSheet()
Sheets(Caller).Select
End Sub

I realize WHY it's happening, but I don't know any other way around it, because the OnAction needs a string.

Any help will be greatly appreciated.

Thanks,

Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.

My newest novel: Wooden Warriors
 
Change your OnAction to be GotoSheet

Change your GotoSheet macro to be:
Code:
Public Sub GoToSheet()
Sheets(Application.Caller(1)).Select
End Sub

Cheers, Glenn.

Beauty is in the eye of the beerholder.
 
My pleasure! :)

Cheers, Glenn.

Beauty is in the eye of the beerholder.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top