什么是引用一组非连续行的紧凑方式?

时间:2014-03-06 18:12:34

标签: excel-vba vba excel

以下代码似乎不优雅且浪费。是否有更紧凑的表示法来引用所有这些行?

Sub HideEBRedData()
'un/hide EIRP Budget rows with redundant path data
Application.ScreenUpdating = False

Set EIRPBudget = Sheets("EIRP Budget")

With EIRPBudget
    .Rows(29).Hidden = Not .Rows(29).Hidden
    .Rows(30).Hidden = Not .Rows(30).Hidden
    .Rows(50).Hidden = Not .Rows(50).Hidden
    .Rows(51).Hidden = Not .Rows(51).Hidden
    .Rows(54).Hidden = Not .Rows(54).Hidden
    .Rows(55).Hidden = Not .Rows(55).Hidden
    .Rows(65).Hidden = Not .Rows(65).Hidden
    .Rows(66).Hidden = Not .Rows(66).Hidden
End With

Application.ScreenUpdating = True
End Sub

Simoco提供了我正在寻找的答案。这是:

Sub HideEBRedData()
'un/hide EIRP Budget rows with redundant path data
Application.ScreenUpdating = False

Dim r As Variant

Set EIRPBudget = Sheets("EIRP Budget")

For Each r In Array(29, 30, 50, 51, 54, 55, 65, 66)
    EIRPBudget.Rows(r).Hidden = Not EIRPBudget.Rows(r).Hidden
Next

Application.ScreenUpdating = True
End Sub

2 个答案:

答案 0 :(得分:2)

作为评论的后续内容,此代码是OP寻找的内容:

Sub HideEBRedData()
    Dim r As Variant

    Application.ScreenUpdating = False

    Set EIRPBudget = Sheets("EIRP Budget")
    For Each r In Array(29, 30, 50, 51, 54, 55, 65, 66)
        EIRPBudget.Rows(r).Hidden = Not EIRPBudget.Rows(r).Hidden
    Next

    Application.ScreenUpdating = True
End Sub

答案 1 :(得分:1)

考虑:

Sub FlipHidden()
    Dim r As Range
    Set r = Range("A29,A30,A50,A51,A54,A55,A65,A66")
    r.EntireRow.Hidden = Not r.EntireRow.Hidden
End Sub

修改#1

如果在运行宏时所有行都可见,则此宏将起作用。