Solved

Convert a ComboBox to a ListBox?

Posted on 2014-10-03
19
94 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 8
19 Comments
 
LVL 26

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 47

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 47

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 47

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 47

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 47

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 47

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 47

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 47

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

734 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