遍历隐藏那些不包含数组中列出的值的行
Posted
技术标签:
【中文标题】遍历隐藏那些不包含数组中列出的值的行【英文标题】:Loop through rows hiding those that do not contain values listed in an array 【发布时间】:2021-12-11 07:47:22 【问题描述】:我想隐藏不包含某些值的行。
我在一个数组中定义我要查找的值,然后在整个 for 循环中迭代一个索引值。我也类似地遍历复选框。
Private Sub FilterResults_Click()
'checkbox count variables
CB_Start = 2
CB_End = 15
'row count variables
StartRow = 2
EndRow = 2999
ColNum = 7
'Array
Dim SubProduct() As Variant
SubProduct = Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N")
'product count variable
k = 0
'loop through checkboxes
For i = CB_Start To CB_End
If UserForm1.Controls("CheckBox" & i).Value = True Then
'loop through rows of data
For j = StartRow To EndRow
'check if cells contains array index of SubProduct
If InStr(Cells(j, ColNum).Value, SubProduct(k)) = 0 Then
Cells(j, ColNum).EntireRow.Hidden = True
Else '<> 0
Cells(j, ColNum).EntireRow.Hidden = False
End If
Next j
Else 'Checkbox not true
For j = StartRow To EndRow
'check if cells do not contain array index of SubProduct
If InStr(Cells(j, ColNum).Value, SubProduct(k)) <> 0 Then
Cells(j, ColNum).EntireRow.Hidden = False
Else '= 0
Cells(j, ColNum).EntireRow.Hidden = False
End If
Next j
End If
'increment
k = k + 1
Next i
'hide userform on filter
UserForm1.Hide
End Sub
【问题讨论】:
究竟是什么不工作?有什么错误吗? 您的意思是行隐藏/显示不起作用?另外,Checkbox2
到 Checkbox15
的 Caption 是否与您的预期子产品相对应?
没有错误。当我选择 A 时,它会隐藏我的所有行。但是,当我在我的用户表单上没有选择任何行时,它会将我的所有行都带回来(它按我的意愿运行)。是的,复选框具有与我的子产品数组相对应的标签。他们的名字是 CheckBox2-CheckBox15。然后我有一个全选和另一个复选框,它也选择所有特定类别的子产品,但目前上面的代码中没有使用这些
所以例如您的目标是在选中 A
复选框并且 G 列中的单元格值包含 a
? 时显示该行
是的,先生。并隐藏所有其他不包含 A
的行
【参考方案1】:
隐藏行
未经测试。Option Explicit
Private Sub FilterResults_Click()
' Crucial info: 14 checkboxes associated with 14 elements in 'SubProduct'.
'checkbox count variables
Const cbStart As Long = 2
Const cbEnd As Long = 15
'row count variables
Const StartRow As Long = 2
Const EndRow As Long = 2999
Const ColNum As Long = 7
'Array
Dim SubProduct() As Variant
SubProduct = VBA.Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N")
' 'VBA' is ensuring a zero-based array ('Option Base' related).
'product count variable
Dim k As Long
'range
Dim rg As Range
Set rg = Cells(StartRow, ColNum).Resize(EndRow - StartRow + 1)
' Hide all rows. Then, in the loop, only unhide the matching ones.
rg.EntireRow.Hidden = True
Dim cCell As Range
Dim cProduct As String
Dim i As Long
'loop through checkboxes
For i = cbStart To cbEnd
If UserForm1.Controls("CheckBox" & i).Value = True Then
'write the current product to a variable
cProduct = SubProduct(k)
'loop through rows of data
For Each cCell In rg.Cells
'check if current cell contains current product
' 'CStr' will prevent failure if error value
' 'vbTextCompare' will ignore case 'A = a'
If InStr(1, CStr(cCell.Value), cProduct, vbTextCompare) > 0 Then
cCell.EntireRow.Hidden = False
End If
Next cCell
End If
'increment product index
k = k + 1
Next i
'hide userform on filter
UserForm1.Hide
End Sub
【讨论】:
这很棒!如果没有选中复选框,我在第一个循环之后添加了另一个 for 循环,显示所有行:For j = cbStart To cbEnd If UserForm1.Controls("CheckBox" & j).Value = False Then q = q + 1 If q >= 14 Then rg.EntireRow.Hidden = False End If End If Next j
以上是关于遍历隐藏那些不包含数组中列出的值的行的主要内容,如果未能解决你的问题,请参考以下文章