VBA code to delete Columns if cell contains string value example will help us to delete Columns if cell contains specific word, text or value from excel worksheet. We can use Delete method of Columns to delete the Columns if the cell value matches the given string. In this example we will see how to delete the Columns in excel worksheet using VBA if cell contains string value. VBA code for deleting Columns if cell contains string value macro should work for all the version of Microsoft Excel 2003, Excel 2007, Excel 2010, and Excel 2013.
VBA code to delete Columns if cell contains string value
Here is the Example VBA syntax and Example VBA Macro to delete Columns from excel worksheets if cell contains string value. This will help you to know how to delete specific Columns if cell contains string value from Excel workbook using VBA.
VBA Delete Columns if cell contains string value: Syntax
Following is the VBA syntax and sample VBA code to delete Columns based on cell value from worksheet using VBA. We are using the Delete method of the Columns object of worksheet.
If
[Column Numbers]”).EntireColumn.Delete
Here
Delete Columns if cell contains string value using VBA: Examples
The following VBA code is to delete Columns based on cell value from the excel worksheet. This code will delete the Columns (1 to 20) if cell value is “Your String”.
Sub sbDelete_Columns_IF_Cell_Cntains_String_Text_Value() Dim lColumn As Long Dim iCntr As Long lColumn = 20 For iCntr = lColumn To 1 Step -1 If Cells(1, iCntr) = “Your String” Then ‘ You can change this text Columns(iCntr).Delete End If Next End Sub
Instructions to run the VBA code to delete Columns if cell contains string value
Please follow the below steps to execute the VBA code to delete Columns if cell contains string value 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 first row from Column 1 to 20 and enter the “Your String” in some cell for testing purpose.
Step 6: Now press F5 to execute the code
Now you can observe that the Columns are deleted from worksheet if the cell value is “Your String”.
Explained VBA Code to Delete Columns based on cell value
Starting program and sub procedure to write VBA code to delete Columns if cell contains string text value.
Sub sbDelete_Columns_IF_Cell_Cntains_String_Text_Value_C()
‘Declaring the variable lColumn as long to store the last Column number
Dim lColumn As Long
‘Declaring the variable iCntr as long to use in the For loop
Dim iCntr As Long
‘Assigning the last Column value to the variable lColumn
lColumn = 20
‘Using for loop
‘We are checking the each cell value if it cell value equals “Your String”
‘And deleting the Column if true
For iCntr = lColumn To 1 Step -1
If Cells(1, iCntr) = “Your String” Then
Columns(iCntr).Delete
End If
Next
End Sub
Ending the macro to delete the Columns if cell contains string value using VBA.
Here you can observe that we are looping through the cells from right to left. This is the best approach to check if cell contains string text or value and then delete the Columns.