我的电子表格如下:
A B C
12 Row1 Click to Hide
12 Row2 Click to Hide
5 Row3 Click to Hide
4 Row4 Click to Hide
12 Row5 Click to Hide
我使用以下vba代码尝试隐藏与用户单击隐藏的activeCell行共享相同编号(在A列中)的所有行。
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(ActiveCell, Range("C:C")) Is Nothing And ActiveCell.Value = "Click to Hide" Then
Dim Cell As Range
Application.ScreenUpdating = False
Range("A4").End(xlDown).Select
For Each Cell In Range(ActiveCell, "A4")
Cell.EntireRow.Hidden = ActiveCell.Offset(0, -2)
Next
Application.ScreenUpdating = True
End If
End Sub
例如,如果用户点击了“点击隐藏”字样。在Row1的C列中,应隐藏A列中编号为12的所有行。
目前我收到错误代码,对象或范围的1004偏移失败。 请有人告诉我我哪里出错了?提前谢谢。
答案 0 :(得分:1)
这将隐藏替代行,但隐藏隐藏使用ActiveCell的行:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("C:C")) Is Nothing And Target.Value = "Click to Hide" Then
valu = Cells(Target.Row, 1).Value
For i = 1 To ActiveSheet.UsedRange.Rows.Count
If Cells(i, 1).Value = valu Then
Cells(i, 1).EntireRow.Hidden = True
Else
Cells(i, 1).EntireRow.Hidden = False
End If
Next i
Target.EntireRow.Hidden = False
Else
Rows.Hidden = False
End If
End Sub