Delete rows/columns/cell data using a macro in an excel report

You can delete the entire row and/or columns and/or just clear the cells much quicker automatically than manually, particularly, if you have to do it many times during the month but if you are doing this task once a year, forget about it.

formula excel
formula excel

 

When I use the macro ?

When I extract data from a program or from an intranet webpage, for instance, some data are merged in many columns and I need to delete those unnecessary columns.

 

How to create the macro ?

Read How to create, edit, hide and select a macro in an excel report

 

How to create the button to associate it with the macro ?

Read How to create a button and associated it to a macro in an excel report

 

How is the macro ?

Copy the code below and paste it into your macro. You will see my comments in green if exist so follow the help to adapt to your need.

To delete columns:

Sub test()
' delete entire columns C and from E to G
ActiveSheet.Range("C:C,E:G").EntireColumn.Delete
End Sub

To delete rows:

Sub test()
' delete entire rows from row 2 to row 50
ActiveSheet.Range("A2:A50").EntireRow.Delete
End Sub
formula excel

To clear contents:

Sub test()
' delete all data in cells from A2 to G50
ActiveSheet.Range("A2:G50").ClearContents
End Sub
formula excel

Interesting Management