Clickable listbox items

eliza6

New Member
Joined
Jul 14, 2011
Messages
11
Hi everybody. I need a help. I have a listbox with two items. There is an option to remove one item, or the other, and then the list is empty and you can re-add it again if you want. I need whenever items are back on the list, to be clickable. The problem is that each item has to open separate form. Item 1 has to open form 1, item 2 - open form 2. Any ideas? I am over my head.


ListBox1.Clear
ListBox1.AddItem Sheets("Data").Range("A1")
ListBox1.AddItem Sheets("Data").Range("A2")

For x = ListBox1.ListCount - 1 To 0 Step -1
If Trim(ListBox1.List(x)) = "" Then
ListBox1.RemoveItem x
End If
Next x


the code above, removes an item from the listbox when one of the cells is empty. as my items are populeted from cells.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
@ Norie, Jaafar: What I am trying to do is make a training environment for one of the programs we use at work. I would like mine to look as similar to the original version as possible. Therefore I need listbox, not checkbox or buttons which would make the whole thing look different and very confusing for new starters who are trying to learn.

The values I have in the cells is text, like: Token 1, Token 2. When I remove the text from the cell, one of the items from the list disappears, just like I want to. At some point one of the buttons will change the token status, so the text in the cell will change, which should be reflected in the listbox. So I can say the values change and are not constant. I hope I explained myself correctly.

Hi eliza.

The explanation is still vague.

When I remove the text from the cell, one of the items from the list disappears
How do you actually remove the items from the cells ? manually, via code etc...

At some point one of the buttons will change the token status, so the text in the cell will change, which should be reflected in the listbox
What buttons ? If you have some buttons involved in this can you tell us more about them.

If you can give us all the info in an orderly and logical way, i am sure someone will provide you with an answer as this is not difficult.
 
Upvote 0
Code:
Private Sub UserForm_Activate()

ListBox1.Clear
ListBox1.AddItem Sheets("Data").Range("A1")
ListBox1.AddItem Sheets("Data").Range("A2")

For x = ListBox1.ListCount - 1 To 0 Step -1
If Trim(ListBox1.List(x)) = "" Then
ListBox1.RemoveItem x
End If
Next x

End Sub


Private Sub RemoveToken_Click()
Sheets("Data").Range("A1").ClearContents
End Sub

Private Sub RemovePass_Click()
Sheets("Data").Range("A2").ClearContents
End Sub

Private Sub AddToken_Click()
Sheets("Data").Range("A1") = "Token"
End Sub

Private Sub AddPass_Click()
Sheets("Data").Range("A2") = "Password"
End Sub
My listbox is part of the form. Listbox content is taken from two cells within my worksheet. There are buttons which add text into cells, there are buttons which remove text from the cells. Once that happen, there is one, two or no items in my listbox. Very primitive coding, but that's all I need.

Now, I want to be able to open forms when I click on the items in my listbox. When I click on the Item 'Token' I want it to open form 'Tokenform', when I click on item 'Password', to open form 'Passwordform'.

The code provided by Jaafar works, as long as you don't remove one of the items from the list. Then the index change, any my item 'Password' opens form 'Tokenform'.

Code:
[FONT=monospace]Private Sub ListBox1_Click()

    If ListBox1.ListIndex = 0 Then
        [/FONT]Tokenform[FONT=monospace].Show  'first form
    Else
        [/FONT]Passform[FONT=monospace].Show  'second form
    End If

End Sub
[/FONT]
 
Last edited:
Upvote 0
Check the value if the selected item in the listbox instead of the usual ListIndex.
 
Upvote 0
Instead of testing the list index one could test the .Value of the Listbox

Code:
Select Case ListBox1.Value
    Case "Token 1"
        UserForm1.Show
    Case "Token 2"
        UserForm2.Show
    ' ...
End Select
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,291
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top