Excel VBA-展开所有列切换?

时间:2019-07-02 12:46:08

标签: excel vba

我有一个宏,它要求扩展所有列,并且我正在使用此代码来实现这一目的

Sub Expand_All()
    ActiveSheet.Outline.ShowLevels RowLevels:=8, ColumnLevels:=8
End Sub

是否有一种方法可以记住工作表组是否在操作之前折叠/展开,以便在宏完成后可以将其恢复为原始状态?

1 个答案:

答案 0 :(得分:0)

这是在工作表中查找隐藏列范围的代码。您应该可以使用它来存储范围,然后再将其隐藏

Sub TestCols()
    Dim rHidden As Range: Set rHidden = HiddenColumns()
    'Your Code
    rHidden.EntireColumn.Hidden = True
End Sub

Function HiddenColumns(Optional wsSrc As Worksheet) As Range
    Dim UnusedRow As Long
    Dim HiddenStatus As Boolean: HiddenStatus = False
    If wsSrc Is Nothing Then Set wsSrc = ActiveSheet

    Application.ScreenUpdating = False
    With wsSrc
        On Error GoTo ExitFunction
        UnusedRow = .Cells.Find("*", , xlFormulas, xlRows, xlPrevious).Row + 1
        With .Rows(UnusedRow)
            HiddenStatus = .Hidden
            .Hidden = False
            .SpecialCells(xlCellTypeVisible).value = "x"
            Set HiddenColumns = Range(.SpecialCells(xlCellTypeBlanks).EntireColumn.Address(0, 0))
            .Clear
            .Hidden = HiddenStatus
        End With
    End With

ExitFunction:
    Application.ScreenUpdating = True
End Function