我有一个包含产品的表格,如果可以在不同的表格中找到所述产品,我想隐藏该行。 因此,对于表1的每一行,检查表A列中是否可以找到A列中的值,如果是,则隐藏表1中的该行。
我使用此代码作为基础,但我还没有弄清楚如何引用第二个表第1列进行检查。
For Each row In myTable.DataBodyRange.Rows
If row.Cells(1, 1).Value = 'can be found in column A table 2 Then
row.Hidden = True
End If
Next
编辑:
@Vityata为所有感兴趣的人提供的最终工作代码:
Public Sub TestMe()
Dim tbl1 As ListObject
Set tbl1 = ActiveSheet.ListObjects("Table1")
Dim tbl2 As ListObject
Set tbl2 = ActiveSheet.ListObjects("Table2")
Dim myRow As Range
For Each myRow In tbl1.DataBodyRange.Rows
If Not myRow.Cells(1, 1).EntireRow.Hidden Then
Dim hideMe As Variant
hideMe = Application.Match(myRow.Cells(1, 3).Value2, tbl2.Range.Columns(1).Cells, 0)
If IsError(hideMe) Then hideMe = False
myRow.Cells(1, 1).EntireRow.Hidden = hideMe
End If
Next myRow
End Sub
答案 0 :(得分:2)
你走了:
Public Sub TestMe()
Dim tbl1 As ListObject
Set tbl1 = ActiveSheet.ListObjects("Table1")
Dim tbl2 As ListObject
Set tbl2 = ActiveSheet.ListObjects("Table2")
Dim myRow As Range
For Each myRow In tbl1.Range.Rows
Dim hideMe As Variant
hideMe = Application.Match(myRow.Cells(1, 1).Value2, tbl2.Range.Columns(1).Cells, 0)
If IsError(hideMe) Then hideMe = False
myRow.Cells(1, 1).EntireRow.Hidden = hideMe
Next myRow
End Sub
tbl1.Range.Rows
; True
找到该单元格,则向hideMe
分配“布尔”值Application.Match
; False
; myRow.Cells(1,1).EntireRow.Hidden = hideMe
隐藏了这一行; 如果要从搜索中排除表格的Header
,请将循环更改为:
For Each myRow In tbl1.DataBodyRange.Rows
答案 1 :(得分:1)
您还可以使用工作表2中“产品”列中的值来过滤工作表1表中“产品”列上的表。我花了更长时间进行调试,因此只看到一些类似于已经给出的答案的元素
Option Explicit
Public Sub test()
Dim rng As Range
Dim lookupRange As Range
Dim loopRange As Range
Dim found As Variant
Set loopRange = ThisWorkbook.Worksheets("Sheet1").ListObjects("Table2").DataBodyRange.Columns(1)
Set lookupRange = ThisWorkbook.Worksheets("Sheet2").ListObjects("Table1").DataBodyRange.Columns(1)
Dim arr()
ReDim arr(loopRange.Count)
With ThisWorkbook.Worksheets("Sheet1").ListObjects("Table2").Range
.AutoFilter
For Each rng In loopRange.Cells
found = Application.Match(rng.Value, lookupRange, 0)
If IsError(found) Then
arr(counter) = rng.Value
counter = counter + 1
End If
Next rng
.AutoFilter Field:=1, Criteria1:=arr, Operator:=xlFilterValues
End With
End Sub