Avatar of Mohammad Alsolaiman
Mohammad Alsolaiman
Flag for Saudi Arabia asked on

Delete Entire Column by column name - using VBA

Hello,
I need to delete Entire column in Excel sheet using column name.
In my example I need to delete the 3rd column with the name "DateOfBirth", instead of using column number.
delete column by its nameThe code I'm using right now is:
Sub sbVBS_To_Delete_EntireColumn ()
    Columns(1).EntireColumn.Delete
End Sub

Open in new window

VBAMicrosoft ExcelMicrosoft Office

Avatar of undefined
Last Comment
Subodh Tiwari (Neeraj)

8/22/2022 - Mon
Ryan Chong

try this:

Sub sbVBS_To_Delete_EntireColumn(columnName As String)
    Columns(columnName).EntireColumn.Delete
End Sub

Open in new window


to implement:

call sbVBS_To_Delete_EntireColumn("C")

Open in new window

ASKER CERTIFIED SOLUTION
Subodh Tiwari (Neeraj)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Mohammad Alsolaiman

ASKER
Thanks Ryan
That's much better than column number, but I would prefer if there is any way to delete it using the name appear in the first row, which in my case is " DateOfBirth" . is it possible to do so?
Mohammad Alsolaiman

ASKER
thanks to all of you
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Subodh Tiwari (Neeraj)

You're welcome!