VBA to find a column label and delete that column

General Ledger

Active Member
Joined
Dec 31, 2007
Messages
460
Dear All,

In a macro using Excel 2010, I am looking for code that finds certain column labels and deletes those columns.

Something like, find the column labels "Date of Birth" and "SSN" in row 1 and delete the entire columns. If a desired label does not exist, continue on to search for the next label. After all the labels have been searched for, simply end.

I have found some code to get me close but am not sure how to delete the correct column or if this is really the best approach.

Code:
For Each Word In Array("Date of Birth, "SSN")
Set aRange = Range("a1:iv1").Find(What:=Word, LookAt:=xlWhole, MatchCase:=False)
If aRange Is Not Nothing Then
?code to delete the entire column?
End If
Next Word

Thanks,

GL
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Apart from the missing bit that you are asking about, some of your syntax is a bit mixed up. Try this in a copy of your workbook.
Code:
For Each Word In Array("Date of Birth", "SSN")
    Set aRange = Rows(1).Find(What:=Word, LookAt:=xlWhole, MatchCase:=False)
    If Not aRange Is Nothing Then
        aRange.EntireColumn.Delete
    End If
Next Word
 
Upvote 0
Peter,

Your code no doubt solves my original problem. Now that I am trying to implement your solution, I came across an issue. My raw data has just a few columns to keep and many columns to delete. I would prefer to list the labels of the few columns to keep rather than the labels of the many columns to be deleted.

How can I adapt your code to keep only the columns of the entered words and delete the columns of any unmatching words? For example, if I wanted to delete every column except the Date of Birth and SSN column, what would the code be?

Thanks,

GL
 
Upvote 0
I would use completely different code. If I wanted to keep columns with headers "H3", "H4" and "H7" then one way would be to use code like this. Note the comma before and after the list of headers to keep, and no extra spaces between the headers in that string.

<font face=Courier New><br><SPAN style="color:#00007F">Sub</SPAN> Keep_Cols()<br>    <SPAN style="color:#00007F">Dim</SPAN> LC <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <br>    <SPAN style="color:#00007F">Const</SPAN> Hkeep <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN> = ",H3,H4,H7,"<br>    <br>    LC = Cells(1, Columns.Count).End(xlToLeft).Column<br>    Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>    <SPAN style="color:#00007F">For</SPAN> i = LC <SPAN style="color:#00007F">To</SPAN> 1 <SPAN style="color:#00007F">Step</SPAN> -1<br>        <SPAN style="color:#00007F">If</SPAN> InStr(1, Hkeep, "," & Cells(1, i).Value & ",", _<br>                vbTextCompare) = 0 <SPAN style="color:#00007F">Then</SPAN><br>            Columns(i).Delete<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <SPAN style="color:#00007F">Next</SPAN> i<br>    Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0
Thanks Peter for providing the solution. Sorry for reviving this old thread but I figured this is the quickest way to build on this.

Is there a way to deal with a much longer string? In the above example, we simply wanted to keep columns with headers "H3,H4,H7". What if the string length is towards 3000 characters? Is it even possible? Yes the headers I have are really long and have a few more columns I'd like to keep.

Thanks for all of your input!
 
Upvote 0
Thanks Peter for providing the solution. Sorry for reviving this old thread but I figured this is the quickest way to build on this.

Is there a way to deal with a much longer string? In the above example, we simply wanted to keep columns with headers "H3,H4,H7". What if the string length is towards 3000 characters? Is it even possible? Yes the headers I have are really long and have a few more columns I'd like to keep.

Thanks for all of your input!
Welcome to the MrExcel board!

The previous code still works for me with headers of length 3,000. The problem you have is how to provide those long strings to the code.

1. Are the headers you want to keep stored somewhere in the workbook, or could they be?

2. Instead of the whole headers, are there shorter substrings in them that would still uniquely identify the column header?
 
Upvote 0
Welcome to the MrExcel board!

The previous code still works for me with headers of length 3,000. The problem you have is how to provide those long strings to the code.

1. Are the headers you want to keep stored somewhere in the workbook, or could they be?

2. Instead of the whole headers, are there shorter substrings in them that would still uniquely identify the column header?

Thanks for the warm welcome Peter!

Yes unfortunately the columns that I want to keep has their names changing a bit, while the ones that are not to be kept have a fixed name but numerous.
There are substrings that I can identify with the columns that needs to be kept.
In the code above, would I be able to use wildcards?


For example, Const Hkeep As String = ",H3*,H4*,H7*," . Would that work out?


Thanks again.
 
Upvote 0
Yes, you could use wildcards but I would change the structure of the code to do so.

I assume that with your suggestion ..
Const Hkeep As String = ",H3*,H4*,H7*,"
.. you were indicating that you would want to keep columns whose headings started with H3, H4 or H7

The code below should do that. The code generally has changed but also note the omission of the leading/trailing commas in the HKeep constant string

<font face=Courier New><br><SPAN style="color:#00007F">Sub</SPAN> Keep_Cols()<br>    <SPAN style="color:#00007F">Dim</SPAN> LC <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, j <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, NumKeeps <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> Keeps<br>    <SPAN style="color:#00007F">Dim</SPAN> Found <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> s <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <br>    <SPAN style="color:#00007F">Const</SPAN> HKeep <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN> = "H3*,H4*,H7*"<br>    <br>    Keeps = Split(HKeep, ",")<br>    NumKeeps = <SPAN style="color:#00007F">UBound</SPAN>(Keeps) + 1<br>    LC = Cells(1, Columns.Count).End(xlToLeft).Column<br>    Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>    <SPAN style="color:#00007F">For</SPAN> i = LC <SPAN style="color:#00007F">To</SPAN> 1 <SPAN style="color:#00007F">Step</SPAN> -1<br>      s = Cells(1, i).Value<br>      j = 0<br>      Found = <SPAN style="color:#00007F">False</SPAN><br>      <SPAN style="color:#00007F">Do</SPAN><br>        <SPAN style="color:#00007F">If</SPAN> s <SPAN style="color:#00007F">Like</SPAN> Keeps(j) <SPAN style="color:#00007F">Then</SPAN> Found = <SPAN style="color:#00007F">True</SPAN><br>        j = j + 1<br>      <SPAN style="color:#00007F">Loop</SPAN> <SPAN style="color:#00007F">Until</SPAN> Found <SPAN style="color:#00007F">Or</SPAN> j = NumKeeps<br>      <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> Found <SPAN style="color:#00007F">Then</SPAN> Columns(i).Delete<br>    <SPAN style="color:#00007F">Next</SPAN> i<br>    Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br></FONT>


If you wanted to keep columns that included H3, H4 or H7 anywhere in the title then change the HKeep line to

<font face=Courier New><SPAN style="color:#00007F">Const</SPAN> HKeep <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN> = "*H3*,*H4*,*H7*"</FONT>
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,566
Messages
6,179,555
Members
452,928
Latest member
101blockchains

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top