Find a column through Macro

omair1051992

New Member
Joined
Mar 12, 2012
Messages
16
I am trying to write a Macro that will identify a particular column by its field header (i.e. first row) and will run a specific set of code on that particular column.

My current code looks like this, coverting New York to NY:-

Cells.Replace What:="New York", Replacement:="NY", LookAt:=xlPart, SearchOrder:=xlByRows

This code applies to 'New York' found in the entire sheet. I need to identify a column that is named 'State' and then make the code run ONLY on that column.
 

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).
Or to make it simple, how do I set the code to run only for a fixed column, say F, instead of making it search for the column beforehand?
 
Upvote 0
Or to make it simple, how do I set the code to run only for a fixed column, say F, instead of making it search for the column beforehand?


;) Don't make it simple.

Code:
col = Application.WorksheetFunction.Match("State", Range("1:1"), 0)
Columns(col).Replace What:="New York", Replacement:="NY", LookAt:=xlPart, SearchOrder:=xlByRows

This will dynamically find State in row 1 and only replace "New York" in that column.

If you WANTED to do it statically you would just need to know the range you wanted to run the search on.
In this case it would be F:F
Code:
Range("F:F").Replace What:="New York", Replacement:="NY", LookAt:=xlPart, SearchOrder:=xlByRows
 
Last edited:
Upvote 0
Thank you so much. That works perfectly.

However, when there is no exact word 'State' as a field name, the Macro gives a runtime error 1004 'Unable to get the match property of the WorksheetFunction class'.

How can I make it give out a simple message that no field named 'State' was found?

Plus, what if I had two possible options of a field header, 'State' or 'State/Province' for example? How can I make the Macro search for both but run on only what is found?
 
Upvote 0
See... that is why I voted against simple. LOL

This should do everything you are wanting with the new requirements.

Code:
Private Sub test()
Dim Found As Boolean
Found = False
For Each col In [1:1]
    If col.Value Like "*State*" Then
        Columns(col.Column).Replace What:="New York", Replacement:="NY", LookAt:=xlPart, SearchOrder:=xlByRows
        Found = True
    End If
Next
If Not Found Then MsgBox "No field named 'State' was found in Row 1!", vbExclamation + vbOKOnly
End Sub

Note that this will also replace multiple columns if there are multiple columns with the word State in row 1.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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