• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 107
  • Last Modified:

Convert a ComboBox to a ListBox?

How would I change the following to be a ListBox instead of a ComboBox, that would show selections as soon as it is opened?  Also, is there a way to add something like a suffix to the end of those in the list that are hidden sheet tabs?

I know that I need to chg the object in the userform to a ListBox, but how would the code chg to do this?

Option Explicit
Private Sub cmdGoTo_Click()

'Dim bHiddenState As Boolean
    If cboSheets.ListIndex <> -1 Then
         Worksheets(cboSheets.Value).Visible = xlSheetVisible
         Application.Goto Worksheets(cboSheets.Value).Range("A1")
         Unload Me
    End If
End Sub

Private Sub UserForm_Initialize()
Dim I As Long

    For I = 1 To Worksheets.Count
        cboSheets.AddItem Worksheets(I).Name
    Next I
End Sub
0
RWayneH
Asked:
RWayneH
  • 10
  • 8
1 Solution
 
RWayneHAuthor Commented:
Thanks but I did not see any examples that deal with sheet tabs...  I get the just of what they do and how it is done.  Now wondering how the code edits would work.
0
 
Martin LissOlder than dirtCommented:
Would having the combobox automatically drop-down help? This open the list when the mouse moves over it but the line of code could be put someplace else.

Private Sub cboSheets_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
     cboSheets.DropDown
End Sub

Open in new window

0
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

 
RWayneHAuthor Commented:
0
 
RWayneHAuthor Commented:
Yes that would help a lot.... I was thinking instead of a mouse over to just have to open when the userform opens.  That and some sort of suffix that helps identify the hidden sheets
0
 
Martin LissOlder than dirtCommented:
You could put cboSheets.DropDown in the Initialize event of the userform. And as for a suffix, are you aware that the combobox can have more than one column? If not then this article shows one being used. You could use a second column to hold True/False based on the Visible state of the sheets.
0
 
RWayneHAuthor Commented:
What I do not know is how to code that True/False that would be in the second column, how to make it blank if not hidden, how to get the value that declares True/False so instead of saying True/False, it adds (hidden).  I added a sample file.  I was hoping for someone to do some edits to it.
0
 
Martin LissOlder than dirtCommented:
Okay, I made it a listbox after all. I also changed the message on the form.
Q-2853068.xlsm
0
 
RWayneHAuthor Commented:
For some reason the old userform1 is still popping up when Ctrl+h is used.
0
 
Martin LissOlder than dirtCommented:
As you can see below that doesn't happen when I do it in the project I uploaded. In order to use my userform in your workbook, you'll need to right-click on my userform in the 'Project VBA Project' window and then click 'Export...'. After that delete your userform and Import mine.
Works for me.
0
 
RWayneHAuthor Commented:
Ok.. that worked, thanks.  Is there a way to have the list active without having to press tab to get it there?  And once on the desired sheet, to press enter there, so I do not have to tab to the "Goto" button?

To execute, just a few down arrows and enter keys?
0
 
Martin LissOlder than dirtCommented:
I'm not sure I understand what you are asking for but in the userform add line 12...

Private Sub UserForm_Initialize()
Dim I As Long
    For I = 1 To Worksheets.Count
        lstSheets.AddItem
        lstSheets.List(I - 1, 0) = Worksheets(I).Name
        If Worksheets(I).Visible = False Then
            lstSheets.List(I - 1, 1) = "hidden"
        End If
    Next I
    
    'new
    lstSheets.SetFocus
End Sub

Open in new window


...and add this code.
Private Sub lstSheets_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    'new
    If KeyCode = vbKeyReturn Then
        cmdGoTo = True
    End If
End Sub

Open in new window

0
 
RWayneHAuthor Commented:
1stSheets.SetFocus  is returning 1 stSheets.SetFocus  (with a space there).

All I would like is that the ListBox in the userform has the focus and preferable the first sheet tab, so the user can skip having to hit the Tab key prior to the down arrow to select.
0
 
Martin LissOlder than dirtCommented:
1stSheets.SetFocus  is returning 1 stSheets.SetFocus  (with a space there).
It should be the letter "l" and not the number "1".
All I would like is that the ListBox in the userform has the focus and preferable the first sheet tab...
The setfocus requirement will be taken care of by the above and to have it be at the first sheet name add line 13.
Private Sub UserForm_Initialize()
Dim I As Long
    For I = 1 To Worksheets.Count
        lstSheets.AddItem
        lstSheets.List(I - 1, 0) = Worksheets(I).Name
        If Worksheets(I).Visible = False Then
            lstSheets.List(I - 1, 1) = "hidden"
        End If
    Next I
    
    'new
    lstSheets.SetFocus
    lstSheets.ListIndex = 0
End Sub

Open in new window

0
 
RWayneHAuthor Commented:
so why does mine after typing 1stSheets.SetFocus and moving to a new line it changes to: 1 stSheets.SetFocus
It is adding a space after the "1".  It is doing it also with the new Ln13?  Do I need to chg that name?

When rename and try  FirstSheets it say Variable not defined...   Not sure what is up?
0
 
Martin LissOlder than dirtCommented:
Are you copy/pasting my code or are you typing it manually?

The names of controls can not start with a number and the name of your control is lstSheets not 1stSheets. Do you see the difference?

In any case here is your workbook updated with the recent changes.
Q-2853068.xlsm
0
 
RWayneHAuthor Commented:
Sorry I should have read your previous reply closer...  it is working fine now.   Excellent!!
0
 
RWayneHAuthor Commented:
Thanks for the help!!  It is much better than it was...
0
 
Martin LissOlder than dirtCommented:
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2014
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 10
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now