Hide/Unhide 基于各种下拉列表的行

Hide/Unhide Rows Based on Various Drop Downs

我正在尝试编写一个代码,该代码将 hide/unhide 行基于下拉值。我可以让第一个下拉 LLknownunknown 正常工作,但所有其他下拉菜单都不起作用。每个下拉菜单都正确命名,每个行号都正确,所以我现在确定发生了什么。

因此,当 LLknownunknown 更改时,正确的行将 hide/unhide。当 KnownUnknown 或 Zeroknownunknown 改变时,什么也不会发生。每个人都需要独立工作。

有什么想法吗?我检查了三次,单元格名称是正确的。

Private Sub Worksheet_Change(ByVal Target As Range)

'The follow code will hide and unhide the Load Loss cell based on the user selection
    If Intersect(Target, Range("LLknownunknown")) Is Nothing Or Target.Cells.Count > 1 Then
        Exit Sub
        
    ElseIf Range("LLknownunknown").Value = "Yes" Then
        Rows("14:15").EntireRow.Hidden = False
        
    ElseIf Range("LLknownunknown").Value = "No" Then
        Rows("14:15").EntireRow.Hidden = True
        
    End If

'The follow code will hide and unhide the impedance cells based on the user selection
    If Intersect(Target, Range("KnownUnknown")) Is Nothing Or Target.Cells.Count > 1 Then
        Exit Sub
        
    ElseIf Range("KnownUnknown").Value = "Yes" Then
        Rows("21:30").EntireRow.Hidden = False
        Rows("36:52").EntireRow.Hidden = True

    ElseIf Range("KnownUnknown").Value = "No" Then
        Rows("21:30").EntireRow.Hidden = True
        Rows("36:52").EntireRow.Hidden = False
        
    End If
    
'The follow code will hide and unhide the zero sequence cells based on the user selection
    If Intersect(Target, Range("Zeroknownunknown")) Is Nothing Or Target.Cells.Count > 1 Then
        Exit Sub
        
    ElseIf Range("Zeroknownunknown").Value = "Yes" Then
        Rows("31:35").EntireRow.Hidden = False
        Rows("53:61").EntireRow.Hidden = True
        
    ElseIf Range("Zeroknownunknown").Value = "No" Then
        Rows("31:35").EntireRow.Hidden = True
        Rows("53:61").EntireRow.Hidden = False
        
    End If

End Sub

这是一个逻辑错误:当您的范围是 KnownUnknown 或 IS Zeroknownunknown 时,那么第一个 IF 测试 -

If Intersect(Target, Range("LLknownunknown")) Is Nothing

return TRUE 并退出子程序,因此 none 其余代码甚至被解析。

您的退出条件应该类似于

If (Intersect(Target, Range("LLknownunknown")) Is Nothing AND Intersect(Target, Range("KnownUnknown")) Is Nothing AND Intersect(Target, Range("Zeroknownunknownn")) Is Nothing) or Target.Cells.Count > 1 Then

如果您的范围只能是“是”或“否”,那么您可以稍微简化每个块:

With Me.Range("KnownUnknown")
    If Not Intersect(Target, .Cells(1)) Is Nothing Then
        Rows("21:30").EntireRow.Hidden = .Value = "No"
        Rows("36:52").EntireRow.Hidden = .Value = "Yes"
    End If
End With

工作表更改:隐藏或取消隐藏行

Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim iCell As Range
    
    ' Hide or unhide the load loss cells.
    Set iCell = Intersect(Target, Range("LLknownunknown"))
    If Not iCell Is Nothing Then
        Select Case LCase(CStr(iCell.Value))
        Case "yes"
            Rows("14:15").Hidden = False
        Case "no"
            Rows("14:15").Hidden = True
        'Case Else
        End Select
        Set iCell = Nothing
    End If

    ' Hide or unhide the impedance cells.
    Set iCell = Intersect(Target, Range("KnownUnknown"))
    If Not iCell Is Nothing Then
        Select Case LCase(CStr(iCell.Value))
        Case "yes"
            Rows("21:30").Hidden = False
            Rows("36:52").Hidden = True
        Case "no"
            Rows("21:30").Hidden = True
            Rows("36:52").Hidden = False
        'Case Else
        End Select
        Set iCell = Nothing
    End If
    
    ' Hide or unhide the zero sequence cells.
    Set iCell = Intersect(Target, Range("Zeroknownunknown"))
    If Not iCell Is Nothing Then
        Select Case LCase(CStr(iCell.Value))
        Case "yes"
            Rows("31:35").Hidden = False
            Rows("53:61").Hidden = True
        Case "no"
            Rows("31:35").Hidden = True
            Rows("53:61").Hidden = False
        'Case Else
        End Select
        Set iCell = Nothing
    End If

End Sub