选择范围并按前缀过滤然后隐藏(设置范围问题)

问题描述 投票:0回答:1
Sub Group()

     Dim arrList As Variant
     Dim rng As Excel.Range
     Dim rCell As Range
     Dim N As Long

Set rng = Range(ActiveCell, ActiveCell.End(xlDown)).Select
 
 arrList = Array("TP001P*")

For Each rCell In rng.Cells
For N = LBound(arrList) To UBound(arrList)

       If VBA.InStr(1, rCell.Value, arrList(N), vbTextCompare) > 0 Then
       rCell.EntireRow.Hidden = True
       Exit For
       End If
       Next 'N
       Next 'rCell
 
End Sub
excel vba select filter hide
1个回答
0
投票

要“按前缀”过滤,请使用 Like 功能。然后通过将所有发现添加到一个范围内,您可以将所有行隐藏在一起

Option Explicit

Sub Group()
   Dim arrList As Variant, rng As Range, rCell As Range, N As Long
   Dim lb As Long, ub As Long, toHide As Range
   Set rng = Range(ActiveCell, ActiveCell.End(xlDown))
   arrList = Array("TP001P*")
   lb = LBound(arrList)
   ub = UBound(arrList)
   
   For Each rCell In rng.Cells
      For N = lb To ub
         If rCell.Value Like arrList(N) Then
            If toHide Is Nothing Then
               Set toHide = rCell
            Else
               Set toHide = Union(toHide, rCell)
            End If
            Exit For
         End If
      Next
   Next
   If Not toHide Is Nothing Then toHide.EntireRow.Hidden = True
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.