How to Hide Columns Using VBA

Save

Spreadsheets with numerous columns aren't always easy to read. Hiding a few of the columns in your table can make it easier to view or understand. Microsoft Excel offers several programmatic options to hide and unhide columns. However, writing visual basics application code to perform the function gives you better control and creating the code to hide columns is straightforward.

  • Add the Developer tab to the program's menu ribbon if you haven't done so already. Click File followed by Options and then Customize Ribbon. Click to activate the Developer's Tab check box in the Main Ribbon section, and then click OK.

  • Return to the spreadsheet and note the column that needs to be hidden. Excel labels the columns alphabetically with "A" as the first column.

  • Open the vsual basics editor. Click the Developer tab and then select Visual Basic.

  • Select the Insert tab and select Module. A blank VBA editor screen pops up in which you can write the code.

  • Copy and paste the code below in your VBA editor. Replace “column range to be hidden” with the actual column you want to hide. For example if you want to hide column "B" replace “column range to be hidden” with B: B. If you want to hide multiple columns, use commas to separate each column.

    Sub Hide_Column()

    Range("column range to be hidden").Select
    Selection.EntireColumn.Hidden = True

    End Sub

  • Click the Save tab in the VBA editor to save your code.

  • Press the Run in the VBA editor and select run user/form. This checks the code for errors and reports at this point. You must run the code before it executes.

  • Click the green Excel icon to exit the VBA editor and return to the table. Now you will see the table with the column you specified hidden.

Tips & Warnings

  • To unhide columns, click “Developer” and select “Macro." Under the macro name, look for “Hide_column” and then press “Delete” to remove the code and unhide the column.

References

Promoted By Zergnet

Comments

Resources

You May Also Like

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!