Keeping columns if header contains string - delete all others

kalamazoo

New Member
Joined
Aug 19, 2011
Messages
20
Hi all,

I have these 7 pieces of text that if the header contains them I want to keep them

[TABLE="width: 0"]
<tbody>[TR]
[TD]SurveyCusAttr
[/TD]
[TD] SurveyIssueDateSSTZ
[/TD]
[TD] Location
[/TD]
[TD] QuestionName
[/TD]
[TD] QuesAnsKeyEntry
[/TD]
[TD] QuestID
[/TD]
[TD] QuesType
[/TD]
[/TR]
</tbody>[/TABLE]

However the source I get them from has different text attached to them a lot of times that is always changing. For instance one quarter it would be 12345QuesType and the next quarter it would be 9871QuesType. Those pieces of text are the constants that I would be using as a unique identifier to keep the columns, and remove all other columns if they do not contain those unique identifiers.

Could anyone help set me on the correct path? I have worked with VBA before but this one seems a little over my head.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
I have code that does something similar.. I'm not an expert but I believe the "*" with each header will act "If header is like"..

Hope this is a good start.

Code:
Sub KeepHeaders()

    Dim currentColumn As Integer
    Dim columnHeading As String


    For currentColumn = ActiveSheet.UsedRange.Columns.Count To 1 Step -1


        columnHeading = ActiveSheet.UsedRange.Cells(1, currentColumn).Value


        'CHECK WHETHER TO KEEP THE COLUMN
        Select Case columnHeading



         Case "*SurveyCusAttr*", "*SurveyIssueDateSSTZ*", "*Location*", "*QuestionName*", "*QuesAnsKeyEntry*", "*QuestID*", "*QuesType*"

               'Do nothing
            Case Else
                    ActiveSheet.Columns(currentColumn).Delete
        End Select
    Next

End Sub
 
Upvote 0
Hi,

Thanks for the reply.

This actually ended up deleting everything. I thought it was maybe case sensitive so I changed that and it still seems to be deleting everything.

Anyone have any ideas? Thanks fort the help!
 
Upvote 0
Like this (add the other headings):
Code:
        Select Case True
            Case columnHeading Like "*SurveyCusAttr*"
            Case columnHeading Like "*SurveyIssueDateSSTZ*"
            Case Else
                 ActiveSheet.Columns(currentColumn).Delete
        End Select
 
Upvote 0
Thanks John,

Pardon my newbiness but am I just supposed to

Code:
Sub Test()        

            Select Case True
            Case columnHeading Like "*SurveyCusAttr*"
            Case columnHeading Like "*SurveyIssueDateSSTZ*"
            Case Else
                 ActiveSheet.Columns(currentColumn).Delete
        End Select


End Sub

Giving me a 1004 error on the ActiveSheet.Columns(currentColumn).Delete line saying application defined or object-defined error. Apologies if I am misunderstanding you.
 
Last edited:
Upvote 0
No. My code replaces the Select .... End Case block of code in post no. 2. You also need everything else above and below it.
 
Last edited:
Upvote 0
I thought that may be the case John. Thanks again for your help. Works like a charm.

So for any readers encountering a similar problem you have found the solution in this thread.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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