VBA-过滤器样式复选框中的#N / A和(空白)

时间:2018-08-14 12:57:59

标签: excel vba userform

我正在尝试创建过滤器样式复选框,但不知道如何处理#N / A(显示为Error 2042)和(空白)。

我的代码的相关部分是-

'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

我尝试-

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

适用于Blanks,但不适用于#N / A。

当我尝试-

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

运行Run-time error '13': Type mismatch时得到UniqueType(ClientType(i, 1)) = 1

是否有更聪明的方式来做我想做的事?

1 个答案:

答案 0 :(得分:1)

在OP澄清后进行了编辑:

'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