提问人:Elliot Trofimowicz 提问时间:9/8/2023 最后编辑:braXElliot Trofimowicz 更新时间:9/11/2023 访问量:34
工作表 更改以根据两个不同的单元格和不同的大小写隐藏行
WorkSheet Change to hide rows based on two different cells and different cases
问:
第一次在堆栈溢出上发帖,所以如果我没有做对,我深表歉意。
我是一个相当新的VBA用户,主要包括谷歌搜索不同的VBA片段并将它们放在一起。
我已经科学化了一个用于工作表更改的 VBA,通过查看从数据验证列表中选择哪个选项来隐藏空白行。我可以让它适用于一个数据验证选项,但是,用户可以选择两个数据验证列表。如果他们选择一个行(假设 $B$2),则可能需要隐藏 5 行,而另一个数据验证为空,如果他们选择另一个行(例如$C$3),则可能需要隐藏 7 行,并且另一个数据验证再次为空。
单个代码有效,但是如上所述,我在使用多个选项的If Else Then和Case时遇到了问题。以下任何帮助将不胜感激。从本质上讲,我希望它查看两个数据验证范围,如果一个是空白的,则查看另一个,当用户更改数据验证条目时,它会将空白行隐藏到第 22 行。反之亦然。
Private Sub Worksheet_Change(ByVal Target As Range)
'Define Dims
Dim counter As Long
Dim iRange As Range
Dim AreaToHide As Range
Rows("13:22").Hidden = False
'Calculate book
Worksheets("Home").Calculate
'Case select view by pack
If Target.Address(True, True) = "$D$13" Then
Select Case Target
Case "Product 1", "Product 2", "Product 3"
'Find last row to hide for view by pack
With ActiveSheet.Range("E13:E22")
'loop through each row from the used range
For Each iRange In .Rows
'check if the row contains a cell with a value
If Application.CountA(iRange) > 0 Then
'counts the number of rows non-empty Cells
counter = counter + 1
End If
Next
End With
'hide blank rows for view by pack
Set AreaToHide = ActiveSheet.Rows(counter + 13 & ":22")
AreaToHide.Hidden = True
'Case select view by country
ElseIf Target.Address(True, True) = "$C$13" Then
Select Case Target
Case "Country 1", "Country 2", "Country 3"
'find last row to hide for view by country
With ActiveSheet.Range("G13:G22")
'loop through each row from the used range
For Each iRange In .Rows
'check if the row contains a cell with a value
If Application.CountA(iRange) > 0 Then
'counts the number of rows non-empty Cells
counter = counter + 1
End If
Next
End With
'hide blank rows for view by country
Set AreaToHide = ActiveSheet.Rows(counter + 13 & ":22")
AreaToHide.Hidden = True
Case Else
'Do nothing
End Select
End If
End Function
End Sub
提前致谢。
答:
0赞
Elliot Trofimowicz
9/11/2023
#1
如果有人想知道,以下工作是有效的,我错过了一个案例 Else, 在第一种情况部分中结束选择,并且不需要结束功能:
Private Sub Worksheet_Change(ByVal Target As Range)
'Define Dims
Dim counter As Long
Dim iRange As Range
Dim AreaToHide As Range
Rows("13:22").Hidden = False
'Calculate book
Worksheets("Home").Calculate
'Case select view by pack
If Target.Address(True, True) = "$D$13" Then
Select Case Target
Case "Product 1", "Product 2", "Product 3"
'Find last row to hide for view by pack
With ActiveSheet.Range("E13:E22")
'loop through each row from the used range
For Each iRange In .Rows
'check if the row contains a cell with a value
If Application.CountA(iRange) > 0 Then
'counts the number of rows non-empty Cells
counter = counter + 1
End If
Next
End With
'hide blank rows for view by pack
Set AreaToHide = ActiveSheet.Rows(counter + 13 & ":22")
AreaToHide.Hidden = True
Case Else
End Select
'Case select view by country
ElseIf Target.Address(True, True) = "$C$13" Then
Select Case Target
Case "Country 1", "Country 2", "Country 3"
'find last row to hide for view by country
With ActiveSheet.Range("G13:G22")
'loop through each row from the used range
For Each iRange In .Rows
'check if the row contains a cell with a value
If Application.CountA(iRange) > 0 Then
'counts the number of rows non-empty Cells
counter = counter + 1
End If
Next
End With
'hide blank rows for view by country
Set AreaToHide = ActiveSheet.Rows(counter + 13 & ":22")
AreaToHide.Hidden = True
Case Else
'Do nothing
End Select
End If
End Sub
评论