Consulting

Results 1 to 14 of 14

Thread: Adding label for msoDropdown control type

  1. #1

    Adding label for msoDropdown control type

    Hello,

    Is there anyway to position the caption for the msoDropdown control type on a custom command bar?

    My Add-Ins include a msoControlDropdown but the caption for the dropdown is like a tooltip. I would like for it to be positioned to the left of the dropdown control like a label.

    Is that possible or is there another solution that can display a label next to the dropdown?

    'Let's create the menubarDim sht As Worksheet
    Dim MyBar As CommandBar
    Dim MyButton, MyButton2 As CommandBarButton
    Dim MyList As CommandBarComboBox
    
    
    On Error Resume Next
    Application.CommandBars("Sheet Navigate").Delete    'If the command bar was created before, it is deleted.
    On Error GoTo 0
    
    
    Set MyBar = CommandBars.Add("Sheet Navigate", , False, True)
    With MyBar
    
    
    Set MyList = .Controls.Add(msoControlDropdown)   'Drop-down list is being created.
    With MyList
       
        .AddItem "One"
        .AddItem "Two"
        .AddItem "Three"
        .Caption = "my dropdown"
        .ListIndex = 1
    
    
       '.TooltipText = "Sheet Navigate"
        .OnAction = "Sheet_Navigate"            'Macro name that triggered by drop-down list
    End With
    
    
    .Protection = msoBarNoCustomize
    .Position = msoBarTop
    .Visible = True
    End With

  2. #2
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,197
    Location
    Maybe the below will help:
        'Let's create the menubarDim sht As Worksheet
        Dim MyBar As CommandBar
        Dim MyButton, MyButton2 As CommandBarButton
        Dim MyList As CommandBarComboBox
        
        
        On Error Resume Next
        Application.CommandBars("Sheet Navigate").Delete    'If the command bar was created before, it is deleted.
        On Error GoTo 0
        
        Set MyBar = CommandBars.Add("Sheet Navigate", , False, True)
        With MyBar
            With .Controls.Add(Type:=msoControlButton)
              .Caption = "Here is a label"
              .Style = msoButtonCaption
            End With
            Set MyList = .Controls.Add(msoControlDropdown)   'Drop-down list is being created.
            With MyList
                .AddItem "One"
                .AddItem "Two"
                .AddItem "Three"
                .Caption = "my dropdown"
                .ListIndex = 1
                '.TooltipText = "Sheet Navigate"
                .OnAction = "Sheet_Navigate"            'Macro name that triggered by drop-down list
            End With
            .Protection = msoBarNoCustomize
            .Position = msoBarTop
            .Visible = True
        End With
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  3. #3
    Great! Thank you!!! That worked perfectly.

  4. #4
    Oh wow .. now I'm in tears.

    After getting my menu bar to work, I've closed out the spreadsheet.

    Once I opened it again, it throws the following error: Method 'Add' of object 'CommandBars' failed.

    I click ok, and the message goes away; however my Add-Ins menu is not showing up.

    OMG What happened?? Please help me

  5. #5
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,197
    Location
    Do you have the code inside a Workbook_Open sub?
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  6. #6
    No.. its a Public Function. But I am getting the error for existing Excel.xslm files that used to work.

    They each are failing on the following line of code.
    Appliation.Commandbars("Worksheet Menu Bar").Controls.Add(msoControlPopup)

  7. #7
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,197
    Location
    I thought you were working with 'msoControlDropdown'?
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  8. #8
    Yes.. the initial issue I had was trying to add label to the msoControlDropdow and your solution worked fine.

    msoContrlDropdown is part of a custom menu bar that has other controls... it also includes a popup control.

    When I closed out the spreadsheet and re-opened, it is when I received the error.

    I continued to try and troubleshoot but it still gave the error.

    I tried opening other Excel workbooks that have custom command bar that I created just the same and those Workbooks are no longer displaying the "Add-Ins" menu and also immediately gives the error upon opening the workbook.

    I have no idea what happened to my Excel Application.

    Could it be that I've corrupted something?

  9. #9
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,197
    Location
    Effectively all you have done is added an empty button to your dropdown menu, i can't see how that would corrupt anything to be honest. I know that is not a lot of help but it is my thinking.

    Maybe you would be able to share a file that displays the error, obviously removing all sensitive data OR creating a file that displays the error and upoading it here?
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  10. #10
    so .. I never figured out the issue with the error but I've moved on.

    Would you know how to return the value in the dropdown list when selected?

  11. #11
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,058
    Location
    Quote Originally Posted by talytech View Post
    so .. I never figured out the issue with the error but I've moved on.
    Are we to assume that you are finished with the intent of the thread then? Is that because you are unable to post a sample workbook or don't want to?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  12. #12
    Hello Aussiebear.. no I never figured out my issue; however I did not need to send a sample workbook because I already confirmed that it is not an issue with the code rather it is related to my MS Office Excel. I've sent the file to others on the job and they had no issues at all. I'm continuing to work with my file outside on my local machine which is not giving me the issue that I have on my remote machine for my job.

    The initial thread is pertaining to the msoControlDropdown. With @georgiboy's help, I successfully added the label. But I am having trouble trying to return the selected value from the dropdown.

    Do I need to end this thread and start a new one? Please advise.

  13. #13
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,197
    Location
    Move the below line out of the sub and place it above the sub:
    Dim MyList As CommandBarComboBox
    See below:
    Option Explicit
    
    Dim MyList As CommandBarComboBox
    
    Sub test()
        'Let's create the menubarDim sht As Worksheet
        Dim MyBar As CommandBar
        Dim MyButton, MyButton2 As CommandBarButton
        
        On Error Resume Next
        Application.CommandBars("Sheet Navigate").Delete    'If the command bar was created before, it is deleted.
        On Error GoTo 0
        
        Set MyBar = CommandBars.Add("Sheet Navigate", , False, True)
        With MyBar
            With .Controls.Add(Type:=msoControlButton)
              .Caption = "Button that is not a button"
              .Style = msoButtonCaption
            End With
        
            Set MyList = .Controls.Add(msoControlDropdown)   'Drop-down list is being created.
            With MyList
                .AddItem "One"
                .AddItem "Two"
                .AddItem "Three"
                .OnAction = "Sheet_Navigate"           'Macro name that triggered by drop-down list
                .ListIndex = 1
            End With
            .Protection = msoBarNoCustomize
            .Position = msoBarTop
            .Visible = True
        End With
    End Sub
    
    Sub Sheet_Navigate()
        Debug.Print MyList.List(MyList.ListIndex) ' value from selected option
        Debug.Print MyList.ListIndex ' selected list index
    End Sub
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  14. #14
    Yesss!!! Perfection .. thank you so much georgiboy.. You saved me..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •