Userform ListBox - Remove Item in VBA

Cat129

Board Regular
Joined
Oct 20, 2012
Messages
96
Hi All,

Not sure where I am going wrong. I have a userform and I want items within the listbox to be removed depending on the value of a textbox in the form.

I currently have

Code:
Private Sub UserForm_Initialize()

NameTextBox.Value = Application.UserName


With DepartmentListBox
    .AddItem "Select Process"
    .AddItem "Goods In"
    .AddItem "Machine Shop"
    .AddItem "First off Inspection"
    .AddItem "In Process Check"
    .AddItem "Final Inspection"
    .AddItem "DMN Rework"
    .AddItem "DMN Scrap"
    .AddItem "Quality Out"
    .AddItem "Goods Out"
    .AddItem "Production Office"
    .AddItem "Laser Marking"
    .AddItem "Kitted"
    .AddItem "Out Plant"
    .ListIndex = 0
End With


'If Application.UserName = "Cat Edwards" Then
'    DepartmentListBox.RemoveItem "Select Process"
'End If

I have tried .RemoveItem"Select Process"

But each variation google gives me, I get error messages.

Help please

Thanks,
Cat
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
You need the item position, for example:

Code:
DepartmentListBox.RemoveItem 0

to remove the first item.
 
Upvote 0
Another way would be to not add the option in the first place:

Code:
Option Explicit
Private Sub UserForm_Initialize()

    NameTextBox.Value = Application.UserName
    
    With DepartmentListBox
        If Application.UserName <> "Cat Edwards" Then
            .AddItem "Select Process"
        End If
        .AddItem "Goods In"
        .AddItem "Machine Shop"
        .AddItem "First off Inspection"
        .AddItem "In Process Check"
        .AddItem "Final Inspection"
        .AddItem "DMN Rework"
        .AddItem "DMN Scrap"
        .AddItem "Quality Out"
        .AddItem "Goods Out"
        .AddItem "Production Office"
        .AddItem "Laser Marking"
        .AddItem "Kitted"
        .AddItem "Out Plant"
        .ListIndex = 0
    End With
    
End Sub

Robert
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,208
Members
452,618
Latest member
Tam84

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