Solved

Convert a ComboBox to a ListBox?

Posted on 2014-10-03
19
85 Views
Last Modified: 2014-10-06
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
Comment
Question by:RWayneH
  • 10
  • 8
19 Comments
 
LVL 25

Expert Comment

by:ProfessorJimJam
ID: 40359611
0
 

Author Comment

by:RWayneH
ID: 40359644
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
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40359691
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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:RWayneH
ID: 40359723
0
 

Author Comment

by:RWayneH
ID: 40359733
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
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40359824
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
 

Author Comment

by:RWayneH
ID: 40360133
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
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40360307
Okay, I made it a listbox after all. I also changed the message on the form.
Q-2853068.xlsm
0
 

Author Comment

by:RWayneH
ID: 40360369
For some reason the old userform1 is still popping up when Ctrl+h is used.
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40360388
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
 

Author Comment

by:RWayneH
ID: 40363611
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
 
LVL 46

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 40363695
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
 

Author Comment

by:RWayneH
ID: 40363787
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
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40363900
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
 

Author Comment

by:RWayneH
ID: 40363950
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
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40363999
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
 

Author Comment

by:RWayneH
ID: 40364096
Sorry I should have read your previous reply closer...  it is working fine now.   Excellent!!
0
 

Author Closing Comment

by:RWayneH
ID: 40364100
Thanks for the help!!  It is much better than it was...
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40364149
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

774 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question