0

如果我在 AE49 或 Z40 中有值但随后将其删除,则以下脚本会显示运行时错误“13”类型不匹配。理想情况下,如果该值被删除,它应该“隐藏”行(这是我试图通过 Case Else 行实现的)。

Private Sub Script2(ByVal Target As Range)

    If (Not Intersect(Target, Range("Z40")) Is Nothing) Then

        Select Case Target.Value
            Case "PowerPoint", "Verbal"
                  Range("A41").EntireRow.Hidden = False
            Case Else
                  Range("A41").EntireRow.Hidden = True
        End Select
    End If
End Sub

Private Sub Script3(ByVal Target As Range)

    If (Not Intersect(Target, Range("AE49")) Is Nothing) Then
       Select Case Target.Value
          Case 1
             Range("A50:A52").EntireRow.Hidden = False
             Range("A53:A55").EntireRow.Hidden = True
          Case 2
             Range("A50:A53").EntireRow.Hidden = False
             Range("A54:A55").EntireRow.Hidden = True
          Case 3
             Range("A50:A54").EntireRow.Hidden = False
             Range("A55").EntireRow.Hidden = True
          Case 4
             Range("A50:A55").EntireRow.Hidden = False
          Case Else
             Range("A50:A55").EntireRow.Hidden = True
       End Select
    End If
End Sub
4

2 回答 2

0

在不更具体地说明您要实现的目标的情况下,我向我们提供了 WorkSheet_Change 事件:

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
With Application
    .ScreenUpdating = False
    .EnableEvents = False
End With

If (Not Intersect(Target, Range("Z40")) Is Nothing) Then
    If Target.Value <> "PowerPoint" And Target.Value <> "Verbal" Then
        Rows(41).Hidden = True
    Else
        Rows(41).Hidden = False
    End If
End If

If (Not Intersect(Target, Range("AE49")) Is Nothing) Then
    Select Case Target.Value
     Case 1
          Range("A50:A52").EntireRow.Hidden = False
          Range("A53:A55").EntireRow.Hidden = True
       Case 2
          Range("A50:A53").EntireRow.Hidden = False
          Range("A54:A55").EntireRow.Hidden = True
       Case 3
          Range("A50:A54").EntireRow.Hidden = False
          Range("A55").EntireRow.Hidden = True
       Case 4
          Range("A50:A55").EntireRow.Hidden = False
       Case Else
          Range("A50:A55").EntireRow.Hidden = True
    End Select
End If

With Application
    .EnableEvents = True
    .ScreenUpdating = False
End With
End Sub

打开 VBA IDE,双击您想在其上运行代码的工作表并复制我在上面发布的整个代码块(如果您已经设置了 Option Explicit,则可以省略它)。每张工作表只能有一个 Worksheet_Change 事件,因此如果您已经在使用该事件,则必须发布该代码,我们可以尝试合并两者。

于 2013-08-06T12:45:42.667 回答
0

我相信没有什么适用于对象。

尝试

if nz(yourRange, "") <> "" then
于 2013-08-06T12:09:20.973 回答