我正在尝试隐藏工作表中的列。我有以下脚本。我希望范围是动态的。现在它正在查看工作表,表格,然后是第3行第3行到表格的末尾。
我希望脚本查看表中的第3列,然后在第3列中,找到包含等于Cell A4值的行。然后使用该行到表的末尾来执行If Not search。
我无法想办法让范围根据单元格值设置行值。
Sub Role_Filter_Button()
Dim cl As Range, rTest As Range
'Sets the range by starting with the third column in a table
Set rTest = Range(Worksheets("Know Our Business").ListObjects("Know_Our_Business").DataBodyRange(3, 3), Worksheets("Know Our Business").ListObjects("Know_Our_Business").DataBodyRange(3, 3).End(xlToRight))
For Each cl In rTest
If Not InStr(1, cl.Value, Worksheets("Know Our Business").Range("A4").Value) > 0 Then
cl.EntireColumn.Hidden = True
End If
Next cl
End Sub
答案 0 :(得分:0)
我添加了第二个循环,首先查看第3列。计数器对行进行计数,如果第3列中的值与a4中的值匹配,则i用于设置用于的行你的代码。
Sub Role_Filter_Button()
Dim cla As Range, clb As Range, rTest As Range
Dim i As Long
Dim dTable As ListObject
Set dTable = ThisWorkbook.Worksheets("Know Our Business").ListObjects("Know_Our_Business")
i = 1
For Each cla In dTable.ListColumns(3).Range
If cla = ThisWorkbook.Worksheets("Know Our Business").Cells(4, 1) Then
Set rTest = Range(dTable.DataBodyRange(i, 3), dTable.DataBodyRange(i, 3).End(xlToRight))
For Each clb In rTest
If Not InStr(1, cla.Value, Worksheets("Know Our Business").Range("A4").Value) > 0 Then
clb.EntireColumn.Hidden = True
End If
Next clb
End If
i = i + 1
Next cla
End Sub