VBA - #N/A and (Blanks) in filter-style 复选框
VBA - #N/A and (Blanks) in filter-style checkboxes
我正在尝试创建过滤器样式复选框,但不知道如何处理#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
当我 运行 UniqueType(ClientType(i, 1)) = 1
.
时,我得到一个 Run-time error '13': Type mismatch
有没有更聪明的方法来做我想做的事情?
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
我正在尝试创建过滤器样式复选框,但不知道如何处理#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
当我 运行 UniqueType(ClientType(i, 1)) = 1
.
Run-time error '13': Type mismatch
有没有更聪明的方法来做我想做的事情?
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