VBA Match Function for Finding and Selecting Columns

mctopher

Board Regular
Joined
Jun 23, 2011
Messages
192
I need help for part of a VBA function that will find nine different header titles in the header row and then select all of those columns so they can be pasted into another worksheet. The location of the headers changes constantly, so my thought was a "Match" statement could return the column numbers and then those columns could be selected. The headers include "Item Number", "Item Description", "Ship Date" and so on.

If anyone could suggest how the Match function could be used in VBA to accomplish this, or if there are other suggestions, I'd be greatly appreciative.

I'm using Excel 2010. Thanks!
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
You could use the .Find method

Code:
[color=darkblue]Sub[/color] Find_10_Headers()


    [color=darkblue]Dim[/color] arrHeaders [color=darkblue]As[/color] [color=darkblue]Variant[/color]
    [color=darkblue]Dim[/color] Header [color=darkblue]As[/color] [color=darkblue]Variant[/color]
    [color=darkblue]Dim[/color] Found [color=darkblue]As[/color] Range
    [color=darkblue]Dim[/color] rngAllHeaders [color=darkblue]As[/color] Range
    
    
    arrHeaders = Array("Item Number", "Item Description", "Ship Date", "Header4", "Header5", _
                       "Header6", "Header7", "Header7", "Header8", "Header9", "Header10")
    
    [color=darkblue]For[/color] [color=darkblue]Each[/color] Header [color=darkblue]In[/color] arrHeaders
        [color=darkblue]Set[/color] Found = Range("1:1").Find(What:=Header, _
                                      LookIn:=xlValues, _
                                      LookAt:=xlWhole, _
                                      MatchCase:=False)
        [color=darkblue]If[/color] [color=darkblue]Not[/color] Found [color=darkblue]Is[/color] [color=darkblue]Nothing[/color] [color=darkblue]Then[/color]
            [color=darkblue]If[/color] rngAllHeaders [color=darkblue]Is[/color] [color=darkblue]Nothing[/color] [color=darkblue]Then[/color]
                [color=darkblue]Set[/color] rngAllHeaders = Found
            [color=darkblue]Else[/color]
                [color=darkblue]Set[/color] rngAllHeaders = Union(rngAllHeaders, Found)
            [color=darkblue]End[/color] [color=darkblue]If[/color]
        [color=darkblue]End[/color] [color=darkblue]If[/color]
    [color=darkblue]Next[/color] Header
    
    rngAllHeaders.EntireColumn.Select
    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Upvote 0
Thank you! I had tried with an If statement that evaluated each column and it just wasn't effective.
 
Upvote 0

Forum statistics

Threads
1,214,952
Messages
6,122,454
Members
449,083
Latest member
Ava19

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