Excel基于单元格值自动隐藏/取消隐藏行

时间:2014-11-07 12:45:51

标签: excel vba excel-vba

如果单元格B22中的值等于或大于25000,我有一个excel表单,我想隐藏第43到49行。

到目前为止我有这个代码,但它似乎只是崩溃excel而且我必须逃脱它。

Private Sub Worksheet_Calculate()

Rows("43:49").EntireRow.Hidden = False
Select Case Range("B22").Value
Case Is >= 25000
Rows("43:49").EntireRow.Hidden = True
Case Else
Rows("43:49").Select
Selection.EntireRow.Hidden = False

End Select
End Sub

2 个答案:

答案 0 :(得分:1)

不要使用Worksheet_Calculate。使用Worksheet_Change

请参阅此示例。移动Worksheet_Change here

Private Sub Worksheet_Change(ByVal Target As Range)
    On Error GoTo Whoa

    '~~> Change CountLarge to Count if using xl2003
    If Target.Cells.CountLarge > 1 Then Exit Sub

    Application.EnableEvents = False

    If Not Intersect(Target, Range("B22")) Is Nothing Then
        Select Case Target.Value
        Case Is >= 25000: Rows("43:49").EntireRow.Hidden = True
        Case Else: Rows("43:49").EntireRow.Hidden = False
        End Select
    End If

Letscontinue:
    Application.EnableEvents = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume Letscontinue
End Sub

答案 1 :(得分:1)

你也可以试试这个:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$22" Then
If Target.Value >= 25000 Then Rows("43:49").EntireRow.Hidden = True
If Target.Value < 25000 Then Rows("43:49").EntireRow.Hidden = False
End If
End Sub