Omit blanks from activex combo box list

j2mbs

New Member
Joined
Jul 15, 2011
Messages
13
Hello Wizards

I am stuck after 2 hours of google searching, and trial and error. I have decided to defer to you for help.

I found some cool code to turn my data validation boxes into searchable combo boxes making the spreadsheet user's life much easier. However the code does not omit blank in my data validation name range causing the scroll bar to be very sensitive due to the name range being the entire column B. Is there a way to modify my code below to omit blank cells?

A little bit of backgound:

1. I have 100+ rows with data validation in column B of Sheet1. Each cell in column B now defaults to the searchable combo box.
2. The name range is located on Sheet2 and is the entire column B.
I use the entire column in order to keep the range dynamic because another macro is used to update the data on Sheet2 which can increase/decrease from month to month. Naming
the entire column ensures I always capture everything.


Any help you can provide is always greatly appreciated.

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Update by Extendoffice: 2017/8/15
    Dim xCombox As OLEObject
    Dim xStr As String
    Dim xWs As Worksheet
    Set xWs = Application.ActiveSheet
    On Error Resume Next
    Set xCombox = xWs.OLEObjects("EmpListCombo")
    With xCombox
        .ListFillRange = ""
        .LinkedCell = ""
        .Visible = False
    End With
    If Target.Validation.Type = 3 Then
        Target.Validation.InCellDropdown = False
        Cancel = True
        xStr = Target.Validation.Formula1
        xStr = Right(xStr, Len(xStr) - 1)
        If xStr = "" Then Exit Sub
        With xCombox
            .Visible = True
            .Left = Target.Left
            .Top = Target.Top
            .Width = Target.Width + 5
            .Height = Target.Height + 5
            .ListFillRange = xStr
            .LinkedCell = Target.Address
        End With
        xCombox.Activate
        Me.EmpListCombo.DropDown
    End If
End Sub
Private Sub EmpListCombo_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Select Case KeyCode
        Case 9
            Application.ActiveCell.Offset(0, 1).Activate
        Case 13
            Application.ActiveCell.Offset(1, 0).Activate
    End Select
End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Hi jm2bs,

Assuming that you are just trying to eliminate the blanks after your list and you don't have any blanks within the list range, you could use a Dynamic Named Range instead of a reference to all of Column B.

To do that:
1. Define a Range with name "Employees" that refers to:
=MyData!$B$2:INDEX(MyData!$B:$B,MAX(1,COUNTA(MyData!$B:$B)-1))

Replace MyData with the actual name of your worksheet. This formula assumes your list starts on Row 2 of that sheet and has a header on Row 1.

There's a couple of problems with that code example from ExtendOffice. The statement Cancel = True is not valid (this was probably originally written to be used in a BeforeDoubleClick event that has a Cancel parameter). Also the placement On Error Resume Next near the top of the code is not a best practice. It actually causes the code to skip over this statement when it errors on a cell without Validation...
Code:
If Target.Validation.Type = 3 Then

...then execute next lines of code as if that statement had been True.

Here's a slightly modified version you might consider using instead...

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Update by Extendoffice: 2017/8/15, modified by jsullivan 2017/12/16
    Dim bHasListValidation As Boolean
    Dim xCombox As OLEObject
    Dim xStr As String
    Dim xWs As Worksheet
    Set xWs = Application.ActiveSheet
    Set xCombox = xWs.OLEObjects("EmpListCombo")
    
    With xCombox
        .ListFillRange = ""
        .LinkedCell = ""
        .Visible = False
    End With
    
    On Error Resume Next
    bHasListValidation = Target.Validation.Type = 3
    On Error GoTo 0
    
    If bHasListValidation Then
        Target.Validation.InCellDropdown = False
        xStr = Target.Validation.Formula1
        xStr = Right(xStr, Len(xStr) - 1)
        If xStr = "" Then Exit Sub
        With xCombox
            .Visible = True
            .Left = Target.Left
            .Top = Target.Top
            .Width = Target.Width + 5
            .Height = Target.Height + 5
            .ListFillRange = xStr
            .LinkedCell = Target.Address
        End With
        xCombox.Activate
        Me.EmpListCombo.DropDown
    End If
End Sub

Private Sub EmpListCombo_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Select Case KeyCode
        Case 9
            Application.ActiveCell.Offset(0, 1).Activate
        Case 13
            Application.ActiveCell.Offset(1, 0).Activate
    End Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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