Delete blank Columns in a range using VBA

Home/VBA/Delete blank Columns in a range using VBA

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

PREMIUM TEMPLATES LIMITED TIME OFFER

ON SALE80% OFF

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 range


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

VBA delete blank Columns in range: Syntax


Following is the VBA syntax and sample VBA code to delete Columns in a range 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 Range: Examples


The following VBA code is to delete blank Columns in range A10 to D20 if Columns are blank.

Sub sbVBS_To_Delete_Blank_Columns_In_Range()
Dim iCntr
Dim rng As Range
Set rng = Range("A10:D20")
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
Next
End Sub 

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


Please follow the below steps to execute the VBA code to delete blank Columns in range 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 in range A1 to D20 in any column. 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 Range A10 to D20.

Explained VBA Code to Delete Blank Columns in Range


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

‘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 = Range(“A10:D20”)

‘Looping through the Columns in the range 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
Next

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

LIMITED TIME OFFER
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


vba show message boxaddition in columns worksheetsvbscript fileexistshide tabs in excelexcel color cellsvba excel create new workbookexplain activex controlexcel vba range clearauto sort in excel without macroseasy vbawriting vba code in excel 2010ddl dml dcl in sqlexcel combine worksheets into one worksheetmerge cells in excel 2010userform excel 2010vba displayexcel vba create rangehow to hide rows in excel 2010excel macro filtervba timevaluehow to convert xlsx to xmlvba sum functionvba getfilenamelistbox multiselect vbacreate userform in excel 2007excel test for empty cellpivot table instructionsvba dynamic rangedynamic named range excel 2010formula to remove special characters in excelmacros in excel tutorial for beginnershow does vlookup work in excel 2013application screenupdating falsetimeline spreadsheet template excelhow to create excel macroexcel vba averageexcel vba backcolorcontoh minutes of meetingexcel formula correctorexcel workbook examplesinterview questions for freshers with answersexcel vba current sheetfor each loop vbaautofilter field vbahow to create a data entry form in excel 2007project charter template wordtask planner templateappend excelhow to use excel vlookup functionvba block commentexport from access to excel vbacount if excel formulahow to protect excel sheet with passwordvlookup in excel vba with examplesexcel vba snippetshide and unhide columns in excelunprotect a workbook without passwordvba range syntaxexcel formulas leftinsert tick box into excelproject milestone template excelhow to unhide all worksheets in excelvba excel pivot tablelessons learned template powerpointconcatinate in excelvba connection string sql serverconditional function in excelms access convert macro to vbahow to make pivot tables in excel 2007lookups exceladvance excel tipsexcel macro selectexcel vba write to text filewhere to find developer tab in excel 2010where is autofit in excel 2013excel 2007 record macroadvanced vlookuphow to unprotect a workbook in excel 2010 without password