Solved

Convert a ComboBox to a ListBox?

Posted on 2014-10-03
19
82 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 45

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
 

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 45

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 45

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 45

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 45

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 45

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 45

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 45

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
VBA code to clear all filter and select 1 48
integer8 values 1 10
Excel 3 24
Moving Range Names/Formulas over to new WB 21 16
A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now