我为
Worksheet_Change
编写了弗兰肯斯坦 VBA 代码,通过查看从数据验证列表中选择的选项来隐藏空白行。
它适用于一种数据验证选项。
用户可以选择两个数据验证列表。
我希望它查看两个数据验证范围,如果一个为空,则查看另一个,当用户更改数据验证条目时,它会隐藏直到第 22 行的空白行。在另一个数据验证列表上反之亦然。
我在使用 If Else Then 和 Case 获取多个选项时遇到问题。
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
我明白了
否则没有 IF
开
ElseIf Target.Address(True, True) = "$C$13" Then
如果有人想知道的话,以下内容有效,我错过了一个案例, 在第一个案例部分中结束选择,不需要结束功能:
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