Excel – How to Hide Columns Using VBA

There might be spreadsheets with a lot of columns, and you might want to hide some of them to make the table easier to view or understand. You might be aware of some of the standard features that are used to hide and unhide columns. However, writing a visual basic for application (VBA) code to perform the function gives you better control and is very easy to create the code.

Follow the steps below to hide a column using VBA.

Instructions

  • Open the [gs spreadsheet] that you would like to hide the columns.
  • Make note of the column that needs to be hidden. The columns are labeled as alphabets with A as the first column.
  • Open the Visual Basic Editor. Click on the "Developer" tab and then click on the "Visual Basic" tab.
  • Select the "Insert" tab and select "Module". A blank screen pops up where you could write the code. The screen is called the VBA Editor.
  • Copy and paste the code below in your VBA editor. Note that the "column range to be hidden" mentioned in the code below should be replaced with the actual column to be hidden. For example if you want to hide column B, the "column range to be hidden" should be replaced with "B:B". If you want to hide multiple columns, use comma to separate the columns.

 

Sub Hide_Column()

Range("column range to be hidden").Select

Selection.EntireColumn.Hidden = True

End Sub

 

NOTE: set the Selection.EntireColumn.Hidden to False to unhide the selected column.

 

  • Click "Save" tab on top of the VBA editor to save your code.
  • Hit the "Run" tab present on top of the VBA editor and the select "run user/form". The code is checked for errors and reported at this point. You will have to run the code before it is executed.
  • Click on the green colored excel icon to exit the VBA Editor and get back to the [gs table]. Now you will see the table with the column you specified hidden.
  • To unhide the columns, click on "Developer" and select "Macro". Under the macro name, look for "Hide_column" and then hit "Delete" to delete the code and unhide the column.
SOURCE

LINK (Ehow.com)

LANGUAGE
ENGLISH