Option
Explicit
Public
Sub
HideColumns()
On
Error
GoTo
ErrHandler
Application.ScreenUpdating =
False
Application.EnableEvents =
False
Dim
rng
As
Excel.Range
Dim
rngHideRow
As
Excel.Range
Dim
rngCell
As
Excel.Range
Set
rng = ActiveSheet.UsedRange
Call
rng.Rows(1).Insert(XlInsertShiftDirection.xlShiftDown)
Set
rngHideRow = rng.Rows(1).Offset(-1)
rngHideRow.FormulaR1C1 =
"=COUNTA("
& rng.Columns(1).Address(
False
,
False
, ReferenceStyle:=xlR1C1) &
")"
For
Each
rngCell
In
rngHideRow.Cells
rngCell.EntireColumn.Hidden = (rngCell.Value = 0)
Next
Call
rngHideRow.Delete(XlDeleteShiftDirection.xlShiftUp)
SafeExit:
Application.EnableEvents =
True
Application.ScreenUpdating =
True
Exit
Sub
ErrHandler:
Call
MsgBox(Err.Description, vbCritical,
"Fehler "
& Err.Number)
GoTo
SafeExit
End
Sub