Consulting

Results 1 to 3 of 3

Thread: Solved: search for string, write coordinate to variable

  1. #1
    VBAX Contributor
    Joined
    Jun 2004
    Location
    Texas
    Posts
    139
    Location

    Solved: search for string, write coordinate to variable

    Hello, all! I receive a spreadsheet which gets automatically generated, and the columns aren't always in the same order. I need to run a procedure, but first I need it to determine which column contains a particular field. What I would like to do is:

    1. Search for a defined string, like "Price." (Should use "Match exact cell contents" option. The strings I search for here appear only once in the workbook.)
    2. When the string is located, store the column letter of its location (A, B,...) in a variable.

    Can someone help me with the best way to search for a string, get the coordinates of the cell containing that string, and then store the column letter in a variable?

    Many thanks in advance!!
    Erin

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [vba]Sub ColLetter()
    Dim c As Range
    Dim Col As String
    Set c = Cells.Find("Price", lookat:=xlWhole)
    If Not c is Nothing Then Col = Split(c.Address, "$")(1)
    MsgBox Col
    End Sub
    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Contributor
    Joined
    Jun 2004
    Location
    Texas
    Posts
    139
    Location
    Perfect!! A million thanks, md. I figured it would not be something too complicated, but I am rusty with this stuff. Thank you, thank you, thank you.
    With program specs this fickle, you've just got to believe in Discord.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •