Identify column header name instead of column number

excel 2003
vba

The following code executes if in column 3

Problem:
I need to execute the code only if the (target) column header is "Customer Mfgnumber Search"

else

msgbox "Not in correct column to execute search"


If target.Column = 3 Then
        KeyHook
    Else
        KeyUnhook
    End If

End Sub


Thanks
fordraiders
LVL 3
FordraidersAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jppintoCommented:
If (target.Column = 3) And (target.cells(1,3).Value="Customer Mfgnumber Search") Then
        KeyHook
    Else
        KeyUnhook
    End If
End Sub
0
Ingeborg Hawighorst (Microsoft MVP / EE MVE)Microsoft MVP ExcelCommented:
Hello,

Assuming the header is in row 1, how about

If Cells(1, target.column) = "Customer Mfgnumber Search" then
' rest of your code

Open in new window


cheers, teylyn
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jppintoCommented:
Picking teylyn sugestion, you could try like this:

If (target.Column = 3) And (Cells(1,target.column).Value="Customer Mfgnumber Search") Then
        KeyHook
    Else
        KeyUnhook
    End If
End Sub
0
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Ingeborg Hawighorst (Microsoft MVP / EE MVE)Microsoft MVP ExcelCommented:
As I understand it, the code should run if the column header is "Customer Mfgnumber Search", regardless of which column it is in. That's what my suggestion does.

cheers, teylyn
0
jppintoCommented:
OK teylyn, I can agree with that but at the same time I have some doubts...let's see what the author has to say.
0
Ingeborg Hawighorst (Microsoft MVP / EE MVE)Microsoft MVP ExcelCommented:
Well, the title says "instead of column number" ....
0
zorvek (Kevin Jones)ConsultantCommented:
This should work. Change "Rows(1)" to the row containing the headers is they are not in row 1.

    If Target.Columns(1).EntireColumn.Rows(1) = "Customer Mfgnumber Search" Then
        KeyHook
    Else
        KeyUnhook
    End If

Kevin
0
FordraidersAuthor Commented:
teylyn,
"Well, the title says "instead of column number" ...."   correct..
because it will vary  from workbook/sheet to workbook/sheet...


0
FordraidersAuthor Commented:
Thanks to all
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.