Hide columns using VBA in Excel
Hiding one or more columns on a worksheet
Hiding a column can be simple. Below code hides column C on the active worksheet. If you set the property
False the column becomes visible again.
Columns("D").Hidden = True
Below code hides multiple columns (D. E and F).
Columns("D:F").Hidden = True
Hiding columns in a range
When starting with a range which is not a complete column you will get run-time error '1004': Unable to set the Hidden property of the Range class.
Dim rng As Range: Set rng = Application.Range("B2") rng.Hidden = True
You can fix this by applying EntireColumn to the range:
Dim rng As Range: Set rng = Application.Range("B2") rng.EntireColumn.Hidden = True
Make a column xlVeryHidden
xlVeryHidden is not supported for Columns. A workaround is to hide it and then protect the sheet.
Getting the code using Code VBA
The animation below shows how Code VBA helps with finding and coding this task. Working from the toolbar would have been even shorter, but does not fit the animation (the action appears too much to the right)