?
Solved

vba find column number based on header value

Posted on 2014-10-22
5
Medium Priority
?
6,460 Views
Last Modified: 2014-10-22
I wondering if there is a way I can get the column number based on a header value in row 1.   I am currently looping through  columns but the columns keep moving in the file that is being used.   The below is a sample of the code but instead defining it as column 5, I would like to find Customer Lead in row 1 and then get the column number.

WS.Range("D8").Value = wsData.Cells(i, 5).Value    'Customer Lead 

Open in new window


Thanks,
Montrof
0
Comment
Question by:montrof
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 33

Accepted Solution

by:
Rob Henson earned 2000 total points
ID: 40396703
You can use the worksheet function MATCH within a VBA script to get the column number:

ColOffset = Application.WorksheetFunction.MATCH("Customer Lead",Sheet1!$1:$1,0)

Thanks
Rob H
0
 
LVL 1

Author Closing Comment

by:montrof
ID: 40396718
Thank you very much

Montrof
0
 
LVL 1

Author Comment

by:montrof
ID: 40396735
Sorry one more question is there an easy way to skip the search if the value is not found.
0
 
LVL 34

Expert Comment

by:Norie
ID: 40396774
If the header might not be there you could try something like this.
Dim Res As Variant 

    Res = Application.Match("Customer Lead", Sheets("Sheet1").Rows(1), 0)

    If IsError(Res) Then 
        MsgBox "Customer Lead column not found."
        Exit Sub
    End If
    
     ' continue with code
    WS.Range("D8").Value = wsData.Cells(i, Res).Value

Open in new window

0
 
LVL 1

Author Comment

by:montrof
ID: 40397007
Thanks Again
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question