隐藏表中隐藏在单独图纸VBA上另一个表中的行



我正在开发一个程序,当具有匹配序列号的行隐藏在单独的表上时,该程序将隐藏表中的行,并且需要帮助。

在一张表中,我有一份标有00001至01000序列号的企业列表,在另一张表上的另一张表格中,我为不同的企业有1000多个联系人,但对一些企业来说,我可能有不止一个联系人(例如,00002路公交车有5个联系人,而00161路公交车只有2个联系人(。基本上,我希望该程序在表/表1中找到任何隐藏的业务线,并在表2/表2中隐藏任何具有匹配序列号的联系人。

这就是我目前所拥有的,一开始它已经工作了几次,但只会隐藏每个"联系人"的第一个联系人;商业;那是隐藏的。但最近它已经完全停止工作了。我不是一个专业的程序员,所以任何帮助或建议都将不胜感激。

Sub Testnumber14()
Dim table1 As ListObject
Set table1 = Sheet1.ListObjects("Bus_List")

Dim table2 As ListObject
Set table2 = Sheet2.ListObjects("Contact_List")

Dim myRow As Range
Dim busRow As Range
Dim i As Integer
i = 1

For Each myRow In table2.DataBodyRange.Rows
If Application.Match(myRow.Cells(1, 1).Value, table1.Range.Columns(1), 0) Then

For Each busRow In table1.DataBodyRange.Rows

If table1.DataBodyRange.Cells(1, 1).EntireRow.Hidden = True Then

table2.DataBodyRange.Cells(1, 1).EntireRow.Hidden = True

Else
table2.DataBodyRange.Cells(1, 1).EntireRow.Hidden = False

End If
Next busRow
End If
Next myRow

End Sub

您可以使用Dictionary来存储所有隐藏行,然后将其应用于第二个表:

Sub Testnumber14()
Dim table1 As ListObject, table2 As ListObject
Dim dict As Object, c As Range

Set table1 = Sheet1.ListObjects("Bus_List")
Set table2 = Sheet2.ListObjects("Contact_List")

Set dict = CreateObject("scripting.dictionary")

For Each c In table1.ListColumns(1).DataBodyRange.Cells
If c.EntireRow.Hidden Then dict(c.Value) = True
Next c

For Each c In table2.ListColumns(1).DataBodyRange.Cells
c.EntireRow.Hidden = dict.exists(c.Value)
Next c

End Sub

最新更新