Add an item to a listbox and select it

Emperor

Board Regular
Joined
Mar 25, 2010
Messages
225
Hi all,

I think this can't be hard but I can't seem to fix it...

Code:
If ListBox5.Value = "Manually" Then
    omschrijving = InputBox(Prompt:="Fill the value pls", _
          Title:="Geef een omschrijving van de behandeling", Default:="Value")
    'add new item on top
    Me.ListBox5.AddItem omschrijving, 0
    
    'Select inserted value...

'What goes here?!?

End if

Thanks in advance!

Mathijs.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hello Andrew,

Thanks for your reply...
The weird thing is, when I do this the by you sugested way, I get another inputwindow (after the first) and the new item doest get selected.

Have you got any other ideas?

Mathijs.
 
Upvote 0
I have added the tags for disabeling the event, still no changes...

This is what I have done;
1. added "Public EnableEvents As Boolean" at the top
2. added "Me.EnableEvents = True" in Private Sub UserForm_Initialize()
3. added tags arround the change event:
Code:
If ListBox5.Value = "Manually" Then
    omschrijving = InputBox(Prompt:="Fill the value pls", _
          Title:="Geef een omschrijving van de behandeling", Default:="Value")
    'add new item on top
    Me.ListBox5.AddItem omschrijving, 0
    
    'Select inserted value...
    Me.EnableEvents = False
    Me.ListBox5.ListIndex = 0
    Me.EnableEvents = True
End if

Is this correct? Or am I interpretering the code wrong?

Greetings Mathijs.
 
Upvote 0
No I did not, and no (sadly enough) this doesn't help.
My original code, without translation etc;

Code:
Private Sub ListBox5_Click()
If Me.EnableEvents = False Then Exit Sub

Dim omschrijving
If ListBox5.Value = "Handmatig invoeren" Then
    Me.EnableEvents = False
    omschrijving = InputBox(Prompt:="Geef een omschrijving van de behandeling", _
          Title:="Geef een omschrijving van de behandeling", Default:="Verf")
    'Nieuw ingevoerde behandeling bovenaanzetten
    Me.ListBox5.AddItem omschrijving, 0
    
    'Nieuw ingevoerde waarde selecteren
    Me.ListBox5.ListIndex = 0
    
    Me.EnableEvents = True
        
End If
End sub

I don't get what I'm doing wrong!

Mathijs.


Edit: Could it be I'm still at Excel 2003?
 
Last edited:
Upvote 0
Try using a CommandButton instead of the ListBox's Click event procedure. In tests I couldn't programatically change the selection while the ListBox had the focus.

Code:
Private Sub CommandButton1_Click()
    Dim omschrijving
    With ListBox5
        If .Value = "Handmatig invoeren" Then
            omschrijving = InputBox(Prompt:="Geef een omschrijving van de behandeling", _
                Title:="Geef een omschrijving van de behandeling", Default:="Verf")
'           Nieuw ingevoerde behandeling bovenaanzetten
            .AddItem omschrijving, 0
'           Nieuw ingevoerde waarde selecteren
            .ListIndex = 0
        End If
    End With
End Sub
 
Upvote 0
Andrew,

Thanks for your help, this is, I think, the only possibility.
I tried changing the focus on another listbox in the start of the event but this also doesn't help, i'll change the form so a button is integrated.

Mathijs.
 
Upvote 0

Forum statistics

Threads
1,226,225
Messages
6,189,736
Members
453,566
Latest member
ariestattle

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