我是VBA的新手,并创建了一个宏,它将从工作表1中的单元格中获取值并将其与工作表2相匹配。如果该行隐藏在工作表1上,则会隐藏工作表2上的行。我需要知道如何在工作表1和2上的所有行循环它。然后我需要它继续将工作表1与工作表3,4和4相匹配。我看过但似乎无法在各种论坛中发现任何东西。
这就是我的工作原理。我可以发布我尝试过的各种各样的东西,但没有一个工作超过第一行。
Sub HideRows
If ActiveSheet.Range("A10") = Worksheets("Sheet2").Range("B5") And _
ActiveSheet.Range("A10").EntireRow.Hidden = True Then
Worksheets("Sheet2").Rows(5).EntireRow.Hidden = True
End Sub
我很抱歉,这是我第一次发帖,所以我对如何发表意见或发布内容非常不熟悉。
让我再试一次:
我有一张主表。它在行10:185的A列中有文本数据。如果这些行中的任何一行在B列到T列中没有数字数据,那么我写了一个隐藏这些行的宏。行10:185在其他5个工作表上按类别分类。我希望能够隐藏在主工作表上隐藏的其他工作表中的相同行。我希望这会使事情更加清晰。
感谢您的帮助。
答案 0 :(得分:0)
要使If语句返回true,以确定活动工作表上的行是否隐藏,您需要更改
ActiveSheet.Range("A10").EntireRow.Hidden = True Then
到
ActiveSheet.Range("A10").EntireRow.Hidden Then
这实际上会返回TRUE
或False
您还需要一个End If
来表示多行If语句。所以现在你的If语句看起来像这样:
Sub HideRows
If ActiveSheet.Range("A10") = Worksheets("Sheet2").Range("B5") And _
ActiveSheet.Range("A10").EntireRow.Hidden Then
Worksheets("Sheet2").Rows(5).EntireRow.Hidden = True
End If
End Sub
因此,此宏仅查看A10中的值并测试它是否等于第二个工作表上的B5以及是否在活动工作表上隐藏了第10行。如果您只想在第一个上查看单元格A10并在剩下的内容中将它们与B5进行比较,则下面的代码将执行此操作(注意:这假设工作表按其名称顺序排列:
Sub HideRows()
Dim i As Integer
For i = 2 To 5
If ActiveSheet.Range("A10") = Sheets(i).Range("B5") And ActiveSheet.Range("A10").EntireRow.Hidden = True Then
Sheets(i).Rows(5).EntireRow.Hidden = True
End If
Next i
End Sub
如果您需要对A10和B5以外的其他细胞进行更多分析,或者您需要的不仅仅是表格1到5,请更新您的问题,以便我们进一步提供帮助。
答案 1 :(得分:0)
如果我理解正确,你需要一个宏来检查主表上的每一行,如果它被隐藏,那么另外5张表中包含该条目的相应行也应该被隐藏。
因此,假设主工作表上的A列中的文本现在位于其他工作表的B列中,并且假设其他5个工作表是工作表(2)到工作表(6),则应该这样做:
Sub HideRows()
Dim i, j As Integer
Dim x As Range
For i = 10 To 185
'check if the row is hidden, if it isn't then no need to check the other sheets
If ActiveSheet.Cells(i, 1).EntireRow.Hidden Then
'loop through each of the other sheets and look for the value in column B, if found, hide the row.
For j = 2 To 6
'you have to "Set" an object variable such as a range reference
'This Finds the value passed to it in the range that this is called from (in this case the entire B column)
Set x = Sheets(j).Columns(2).Find(ActiveSheet.Cells(i, 1).Value, LookIn:=xlValues)
If Not x Is Nothing Then
x.EntireRow.Hidden = True
End If
Next j
End If
Next i
End Sub
答案 2 :(得分:0)
下面的两个版本以相同的方式工作
Sheet2.ColB
中的值与Sheet3.ColB
(Sheet1
等)中的值相匹配Sheet2
中的行被隐藏了
3
,Option Explicit
Public Sub MatchAndHideRows()
Const COL_1 = "A" 'column with text data in Sheet1 (Master Sheet)
Const COL_2 = "B" 'column with text data in Sheets 2, 3, etc
Dim ws1 As Worksheet, lr1 As Long, arr1 As Variant, d1 As Object
Dim ws2 As Worksheet, lr2 As Long, arr2 As Variant, d2 As Object, r As Long
Set ws1 = Sheet1 'Master Sheet (Or: Set ws1 = ThisWorkbook.Worksheets("Sheet1"))
lr1 = ws1.Cells(ws1.Rows.Count, COL_1).End(xlUp).Row
arr1 = ws1.Range(ws1.Cells(1, COL_1), ws1.Cells(lr1, COL_1)).Formula
Set d1 = CreateObject("Scripting.Dictionary")
Set d2 = CreateObject("Scripting.Dictionary")
For r = 10 To lr1 'skip Header rows
If ws1.Rows(r).Hidden Then d1(arr1(r, 1)) = 0 'remember all hidden rows
Next
For Each ws2 In ThisWorkbook.Worksheets 'iterate all sheets
If ws1.Name <> ws2.Name Then 'except Master Sheet (Sheet1)
lr2 = ws2.Cells(ws2.Rows.Count, COL_2).End(xlUp).Row
arr2 = ws2.Range(ws2.Cells(1, COL_2), ws2.Cells(lr2, COL_2)).Formula
For r = 5 To lr2 'skip Headers
If d1.Exists(arr2(r, 1)) Then d2(r) = 0
Next
ws2.UsedRange.Rows.Hidden = False
If d2.Count > 0 Then
ws2.Range("A" & Join(d2.Keys, ",A")).EntireRow.Hidden = True
End If
End If
Next
End Sub
等
版本1
Public Sub MatchAndHideRowsCheckRowByRow()
Const COL_1 = "A" 'column with text data in Sheet1 (Master Sheet)
Const COL_2 = "B" 'column with text data in Sheets 2, 3, etc
Dim ws1 As Worksheet, lr1 As Long, rng1 As Range, c1 As Range
Dim ws2 As Worksheet, lr2 As Long, rng2 As Range, c2 As Range
Set ws1 = Sheet1 'Master Sheet (Or: Set ws1 = ThisWorkbook.Worksheets("Sheet1"))
lr1 = ws1.Cells(ws1.Rows.Count, COL_1).End(xlUp).Row
Set rng1 = ws1.Range(ws1.Cells(10, COL_1), ws1.Cells(lr1, COL_1)) 'skip Header rows
Application.ScreenUpdating = False
For Each c1 In rng1.Cells 'iterate each cell with data in Sheet1.ColA
If Not IsError(c1) Then 'if current cell doesn't contain an error, continue
For Each ws2 In ThisWorkbook.Worksheets 'iterate all sheets
If ws1.Name <> ws2.Name Then 'except Master Sheet (Sheet1)
lr2 = ws2.Cells(ws2.Rows.Count, COL_2).End(xlUp).Row
Set rng2 = ws2.Range(ws2.Cells(5, COL_2), ws2.Cells(lr2, COL_2))
For Each c2 In rng2.Cells 'iterate each cell in current sheet
If Not IsError(c2) Then
If c1.Value2 = c2.Value2 Then
c2.EntireRow.Hidden = c1.EntireRow.Hidden
End If
End If
Next
End If
Next
End If
Next
Application.ScreenUpdating = True
End Sub
版本2
{{1}}