WorkSheet_Change 隐藏基于两个单元格和 Select 中不同 Case 的行

问题描述 投票:0回答:1

我为

Worksheet_Change
编写了弗兰肯斯坦 VBA 代码,通过查看从数据验证列表中选择的选项来隐藏空白行。

它适用于一种数据验证选项。

用户可以选择两个数据验证列表。

  • 如果他们选择其中之一(假设 $B$2),则可能需要隐藏 5 行,而其他数据验证为空。
  • 如果他们选择另一个(例如 $C$3),则可能需要隐藏 7 行,并且其他数据验证再次为空。

我希望它查看两个数据验证范围,如果一个为空,则查看另一个,当用户更改数据验证条目时,它会隐藏直到第 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
excel vba if-statement worksheet
1个回答
0
投票

如果有人想知道的话,以下内容有效,我错过了一个案例, 在第一个案例部分中结束选择,不需要结束功能:

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
© www.soinside.com 2019 - 2024. All rights reserved.