隐藏行 Excel 2013

Hiding Rows with Excel 2013

所以我试图根据几个不同的条件使用 VBA 隐藏 Excel 2013 中的行:

Range("CNonTest") 在 Col C 中,应该检查的额外列是 Col AQ。

为了增加难度,我需要这个宏来 运行 每次 8 个不同的验证框中的任何一个发生变化时。

下面是我目前的代码:

    Sub CompHide()

    With Sheets("Comparison").Cells
       .EntireRow.Hidden = False

    If Range("C9").Value = "Unused" Then
        Range("CMarket1").EntireRow.Hidden = True
    End If

    If Range("C115").Value = "Unused" Then
        Range("CMarket2").EntireRow.Hidden = True
    End If

    If Range("C221").Value = "Unused" Then
        Range("CMarket3").EntireRow.Hidden = True
    End If

    If Range("C329").Value = "Unused" Then
        Range("CMarket4").EntireRow.Hidden = True
    End If

    If Range("C437").Value = "Unused" Then
        Range("CMarket5").EntireRow.Hidden = True
    End If

    If Range("C545").Value = "Unused" Then
        Range("CMarket6").EntireRow.Hidden = True
    End If

    If Range("C653").Value = "Unused" Then
        Range("CMarket7").EntireRow.Hidden = True
    End If

    If Range("C761").Value = "Unused" Then
        Range("CMarket8").EntireRow.Hidden = True
    End If

    If Range("C869").Value = "Unused" Then
        Range("CMarket9").EntireRow.Hidden = True
    End If

    If Range("C977").Value = "Unused" Then
        Range("CMarket10").EntireRow.Hidden = True
    End If

    For Each C In Range("CNonTest")
        If C.Value = "" And C.Columns(41).Value = "" Then
            C.EntireRow.Hidden = True
        End If
    Next



    Range("CBlank").EntireRow.Hidden = True

    End With
End Sub

然后在 Sheet 我有这个代码:

    Private Sub Worksheet_Change(ByVal Target As Range)

If Intersect(Target, Me.Range("A4")) Is Nothing _
    Or _
    Intersect(Target, Me.Range("D4")) Is Nothing _
    Or _
    Intersect(Target, Me.Range("G4")) Is Nothing _
    Or _
    Intersect(Target, Me.Range("K4")) Is Nothing _
    Or _
    Intersect(Target, Me.Range("AO4")) Is Nothing _
    Or _
    Intersect(Target, Me.Range("AR4")) Is Nothing _
    Or _
    Intersect(Target, Me.Range("AU4")) Is Nothing _
    Or _
    Intersect(Target, Me.Range("AY4")) Is Nothing _
    Then Exit Sub

    Application.EnableEvents = False 'to prevent endless loop
    Application.ScreenUpdating = False

    Call CompHide

    Application.ScreenUpdating = True
    Application.EnableEvents = True


End Sub

对于Sheet代码我也试过了,没用

Private Sub Worksheet_Change(ByVal Target As Range)

If Intersect(Target, Me.Range("A4")) Is Nothing Then Exit Sub

    Application.EnableEvents = False 'to prevent endless loop
    Application.ScreenUpdating = False

    Call CompHide

    Application.ScreenUpdating = True
    Application.EnableEvents = True



If Intersect(Target, Me.Range("D4")) Is Nothing Then Exit Sub

    Application.EnableEvents = False 'to prevent endless loop
    Application.ScreenUpdating = False

    Call CompHide

    Application.ScreenUpdating = True
    Application.EnableEvents = True



If Intersect(Target, Me.Range("G4")) Is Nothing Then Exit Sub

    Application.EnableEvents = False 'to prevent endless loop
    Application.ScreenUpdating = False

    Call CompHide

    Application.ScreenUpdating = True
    Application.EnableEvents = True



If Intersect(Target, Me.Range("K4")) Is Nothing Then Exit Sub

    Application.EnableEvents = False 'to prevent endless loop
    Application.ScreenUpdating = False

    Call CompHide

    Application.ScreenUpdating = True
    Application.EnableEvents = True



If Intersect(Target, Me.Range("AO4")) Is Nothing Then Exit Sub

    Application.EnableEvents = False 'to prevent endless loop
    Application.ScreenUpdating = False

    Call CompHide

    Application.ScreenUpdating = True
    Application.EnableEvents = True



If Intersect(Target, Me.Range("AR4")) Is Nothing Then Exit Sub

    Application.EnableEvents = False 'to prevent endless loop
    Application.ScreenUpdating = False

    Call CompHide

    Application.ScreenUpdating = True
    Application.EnableEvents = True



If Intersect(Target, Me.Range("AU4")) Is Nothing Then Exit Sub

    Application.EnableEvents = False 'to prevent endless loop
    Application.ScreenUpdating = False

    Call CompHide

    Application.ScreenUpdating = True
    Application.EnableEvents = True


If Intersect(Target, Me.Range("AY4")) Is Nothing Then Exit Sub

    Application.EnableEvents = False 'to prevent endless loop
    Application.ScreenUpdating = False

    Call CompHide

    Application.ScreenUpdating = True
    Application.EnableEvents = True


End Sub

这段代码似乎一切正常,当我使用 F8 单步执行 CompHide 时,它​​运行良好。所以我认为问题出在 sheet 本身的代码上。您会在该代码中看到一条注释,其中提到要防止无限循环,该注释来自一些递给我的代码,我不太确定它的用途,但根据注释我会留下它。

当我更改验证框时,它不再隐藏所有正确的东西,只隐藏其中的一些。幸运的是,我还没有看到它隐藏了一些它不应该隐藏的东西。我说不再是因为起初这段代码只查看了第一个验证框,但现在它查看了所有 8 个。

对事件处理程序的一些调整:

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rng As Range

    On Error GoTo haveError

    Set rng = Application.Intersect(Target, Me.Range("A4,D4,G4,K4,AO4,AR4,AU4,AY4"))

    If Not rng Is Nothing Then
        Application.EnableEvents = False 'to prevent endless loop
        Application.ScreenUpdating = False
        CompHide
        Application.EnableEvents = True
    End If
    Exit Sub

haveError:
    'always re-enable events
    '  (screenupdating setting is not persistent)...
    Application.EnableEvents = True

End Sub

其他部分:

Sub CompHide()

    Dim sht As Worksheet, C As Range

    Set sht = Sheets("Comparison")
    sht.Rows.Hidden = False

    SetRowVis "C9", "CMarket1"
    SetRowVis "C115", "CMarket2"
    '...and the rest

    For Each C In sht.Range("CNonTest")
        If C.Value = "" And C.EntireRow.Columns(43).Value = "" Then
            C.EntireRow.Hidden = True
        End If
    Next

    sht.Range("CBlank").EntireRow.Hidden = True
End Sub

'utility sub...
Sub SetRowVis(addr As String, rngName As String)
    With Sheets("Comparison")
        If .Range(addr).Value = "Unused" Then
            .Range(rngName).EntireRow.Hidden = True
        End If
    End With
End Sub

第一,您的 CompHide Sub.
存在引用问题 您需要将所有 Range 对象调用完全引用到工作表。

With Sheets("Comparison")
    .Cells.EntireRow.Hidden = False
    'Notice the dot in front of the Range object
    If .Range("C9").Value = "Unused" Then .Range("CMarket1").EntireRow.Hidden = True
    'Also notice that I used a one liner IF which I think is applicable for you
    'Rest of your code go here
    '.
    '.
    '.

End With

2、看看蒂姆的post。他打败了我。 :)