Search code examples
excelvbauserform

VBA - #N/A and (Blanks) in filter-style checkboxes


I am trying to create filter style checkboxes, but do not know how to handle #N/A (which shows up as Error 2042) and (Blanks).

The relevant part of my code is -

'Read Client Type Column
ClientType = Sht.Range(StartCell, Sht.Cells(LastRow, StartCell.Column))

'Find unique client names
Set UniqueType = CreateObject("Scripting.Dictionary")

For i = LBound(ClientType, 1) To UBound(ClientType, 1)
    UniqueType(ClientType(i, 1)) = 1
Next i  

Temp = UniqueType.Keys()

Cntr = 1

On Error Resume Next
For Each j In Temp
    Set Cbx = UserForm1.Controls.Add("Forms.CheckBox.1")
    Cbx.Caption = j
    Cbx.Left = 15
    Cbx.Top = 10 + (15 * (Cntr - 1))
    Cntr = Cntr + 1
Next j

I tried -

If UniqueType.Exists("") Then
    UniqueType.Remove ""
    UniqueType.Add "(Blanks)", 1
End If

which works for Blanks but not for #N/A.

When I tried -

ClientType = Sht.Range(StartCell, Sht.Cells(LastRow, StartCell.Column)).Text

I get a Run-time error '13': Type mismatch when I run UniqueType(ClientType(i, 1)) = 1.

Is there a smarter way to do what I am trying to do?


Solution

  • edited after OP's clarification:

    'Read Client Type Column
    Set UniqueType = CreateObject("Scripting.Dictionary")
    Dim cell As Range
    For Each cell In Sht.Range(StartCell, Sht.Cells(LastRow, StartCell.Column))
        'Find unique client name
        UniqueType(cell.Text) = 1
    Next
    
    Temp = UniqueType.Keys()
    
    '... rest of your code