VBA Delete blank Columns in a table

Home/VBA/VBA Delete blank Columns in a table

VBA code to delete blank Columns in table example will helps to delete empty Columns in specific table range from excel worksheet. We can use Delete method of Columns to delete the Columns in a table. In this example we will see how to delete the Columns from a table in excel worksheet using VB. Excel VBA Macro code for deleting Columns in a table should work for all the version of Microsoft Excel 2003, Excel 2007, Excel 2010, and Excel 2013.



50+ Project Management Templates Pack
Excel PowerPoint Word

Advanced Project Plan & Portfolio Template
Excel Template

Business Presentations Templates Pack
PowerPoint Slides

20+ Excel Project Management Pack
Excel Templates

20+ PowerPoint Project Management Pack
PowerPoint Templates

10+ MS Word Project Management Pack
Word Templates

VBA code to delete Columns in a table

Here is the Example VBA syntax and Example VBA Macro to delete Columns in table from excel worksheets. This will help you to know how to delete specific Columns in a table from Excel workbook using VBA.

VBA delete blank Columns in table: Syntax

Following is the VBA syntax and sample VBA code to delete Columns in a table from worksheet using VBA. We are using the Delete method of the Columns object of worksheet.

If Application.WorksheetFunction.CountA(Columns(ColumnNumber)) = 0 Then Columns(ColumnNumber).EntireColumn.Delete

Here Application.WorksheetFunction.CountA will check if the Column is blank. And the Columns.delete method will delete the Column if Column is blank.

Delete blank Columns in Table: Examples

The following VBA code is to delete blank Columns in Table1 in the active sheet if Columns are blank.

Sub sbVBS_To_Delete_Blank_Columns_In_Table()
Dim iCntr As Long
Dim rng As Range
Set rng = ActiveSheet.ListObjects("Table1").Range
For iCntr = rng.Column + rng.Columns.Count - 1 To rng.Column Step -1
If Application.WorksheetFunction.CountA(Columns(iCntr)) = 0 Then Columns(iCntr).EntireColumn.Delete
End Sub 

Instructions to run the VBA code to delete blank Columns in table

Please follow the below steps to execute the VBA code to delete blank Columns in Table1 from Excel worksheets.
Step 1: Open any Excel workbook
Step 2: Press Alt+F11 – This will open the VBA Editor
Step 3: Insert a code module from then insert menu
Step 4: Copy the above code and paste in the code module which have inserted in the above step
Step 5: Enter some sample data and insert table. And leave some blank Columns for testing purpose.
Step 6: Now press F5 to execute the code

Now you can observe that the all blank Columns are deleted from worksheet in in Table1.

Explained VBA Code to Delete Columns in Table

Start writing the Excel VBA Macro to delete blank Columns in a particular range.
Sub sbVBS_To_Delete_Blank_Columns_In_Table()

‘Declaring a variable iCntr as Long to store the Column number iteration to use in for loop.
Dim iCntr As Long
‘Declaring range variable rng to set the range
Dim rng As Range

‘Assigning the range A10 to D20 to rng range object
Set rng = ActiveSheet.ListObjects(“Table1”).Range

‘Looping through the Columns in the table tange from last to frist. Here step statement will helps.
For iCntr = rng.Column + rng.Columns.Count – 1 To rng.Column Step -1
‘Checking the Column if it is blank.
‘And deleting the Column if it is blank.
If Application.WorksheetFunction.CountA(Columns(iCntr)) = 0 Then Columns(iCntr).EntireColumn.Delete

End Sub
Ending the sub procedure to delete blank Columns in a table range.

By |April 22nd, 2014|VBA|0 Comments

About the Author:

PNRao is a passionate business analyst and having close to 10 years of experience in Data Mining, Data Analysis and Application Development. This blog is his passion to learn new skills and share his knowledge to make you expertise in Data Analysis (Excel, VBA, SQL, SAS, Statistical Methods, Market Research Methodologies and Data Analysis Techniques).

Leave A Comment

Related pages

excel how to identify duplicatesprogramming in vbaworksheetfunction vbainsert in excel formulamacro to merge excel fileshow to use the sumif function in microsoft excel 2010isna excel formularemove duplicates excel 2013how do you do a tick on excelprograming excelcreating a dashboard in excel 2007arrays in excel vbavb6 directory existsms access vba message boxmerge excel rowsvba excel recordsetexcel deleting duplicate rowsexcel isnumber functionhighlighting duplicates in excelvba file copyexcel macro save workbookvba getting startedddl dml dcl in sqldbms interview questions and answers for freshers pdfvba write functionms access vba update queryhow to remove duplicates in excel 2013append string in excelexcel 2003 pivot tablesexcel hide a columnexcel offset functionexample of excel macrodelete rows in excel using vbaconcatenate in excel 2007vba excel select rangeexcel 2007 pivot chartvba excel form controlssql questions and answers for experiencedpivot table in excel 2007 tutorialopen xml file in excel 2010shortcut to insert new row in excelexcel dashboard tutorialsweekly project status report template powerpointhow to use advanced filter in excelinsert new worksheet excel 2010excel 2010 developer modeif statements on excelvba 2007creating dashboardsexcel macro write to text filecreate command button in excelexcel macro file locationvba uppercase functionhow to add columns in excel 2007vb complete reference pdfhow to merge two columns in excel 2007excel vba cleanexcel vba database examplesvba vlookupexcel combobox vbarows excel vbacombine multiple worksheets into one excelexcel 2007 graph tutorialvba activeworkbook.savecombine multiple worksheets into oneteam capacity planning xlslook up function in excelexcel macro exitexcel workbook vbaproject timeline powerpoint template freenested if in vbscriptexcel macros usesvba status bar progressproject management excel gantt chart templatefind duplicate values in exceldeveloper menu in excelaccess vba open excel workbook