Hide/Remove Userform Combobox Blank at End of List

Cyril Beki

Board Regular
Joined
Sep 18, 2021
Messages
57
Office Version
  1. 2016
Platform
  1. Windows
Hello Expert,

I have dependent combo box. The code works fine until i found out there's blank at end of list (Refer image for references) . How can i get rid of it ?

Below is the code i used for dependent combobox
VBA Code:
Private Sub txtSection_Change()
Me.txtMachine.Clear
Me.txtTube.Clear
'Set Worksheet
Dim sh As Worksheet
Set sh = Sheets("Machine Problems")
'Declare Variable
Dim i As Long
For i = 2 To sh.Range("A10000").End(xlUp).Row
If sh.Cells(i, 1) = Me.txtSection.Value Then

'Removing Duplicate Value
If Application.WorksheetFunction.CountIf(sh.Range("B2", "B" & i), sh.Cells(i, 2)) = 1 _
Or Application.WorksheetFunction.CountIf(sh.Range("C2", "C" & i), sh.Cells(i, 3)) = 1 Then
Me.txtMachine.AddItem sh.Cells(i, 2)
Me.txtTube.AddItem sh.Cells(i, 3)
End If
End If
Next i
End Sub
 

Attachments

  • blank space.PNG
    blank space.PNG
    3.1 KB · Views: 8

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Maybe add something like this to the end of your sub to remove the last element if it is blank

VBA Code:
    With Me.txtTube
        If Trim(CStr(.List(.ListCount - 1))) = "" Then
            .RemoveItem .ListCount - 1
        End If
    End With

Repeat for any other combo boxes.
 
Upvote 0
Solution
Maybe add something like this to the end of your sub to remove the last element if it is blank

VBA Code:
    With Me.txtTube
        If Trim(CStr(.List(.ListCount - 1))) = "" Then
            .RemoveItem .ListCount - 1
        End If
    End With

Repeat for any other combo boxes.
You mean before the end sub right ? Like below

VBA Code:
Private Sub txtSection_Change()
Me.txtMachine.Clear
Me.txtTube.Clear
'Set Worksheet
Dim sh As Worksheet
Set sh = Sheets("Machine Problems")
'Declare Variable
Dim i As Long
For i = 2 To sh.Range("A10000").End(xlUp).Row
If sh.Cells(i, 1) = Me.txtSection.Value Then

'Removing Duplicate Value
If Application.WorksheetFunction.CountIf(sh.Range("B2", "B" & i), sh.Cells(i, 2)) = 1 _
Or Application.WorksheetFunction.CountIf(sh.Range("C2", "C" & i), sh.Cells(i, 3)) = 1 Then
Me.txtMachine.AddItem sh.Cells(i, 2)
Me.txtTube.AddItem sh.Cells(i, 3)
End If
End If
Next i
 With Me.txtMachine
        If Trim(CStr(.List(.ListCount - 1))) = "" Then
            .RemoveItem .ListCount - 1
        End If
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,255
Members
448,556
Latest member
peterhess2002

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