+ Reply to Thread
Results 1 to 9 of 9

searching characters problem

  1. #1
    Registered User
    Join Date
    05-26-2007
    Posts
    53

    Question searching characters problem

    I'm almost done with my project, but I run into the following problem:
    In column B of Sheet 1 I have an ID number that looks like "SJUD56YK78T". I need to search the first 3 characters of the ID in a data base that I have in Sheet 2 (column B). When I find it, I need to copy the corresponding make of the boat from Sheet2 Column A into Sheet 1 Column T.
    Thanks for any idea.

  2. #2
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,887
    Try this formula:
    Please Login or Register  to view this content.
    Put this in Sheet1 cell T1 and fill downward. This will return the value in column A of sheet2 where column B of sheet2 matches the first 3 characters of column B in sheet1.

    I assumed your 'database' on sheet2 only contains 3 characters, while column B on sheet1 contains the full ID.

  3. #3
    Registered User
    Join Date
    05-26-2007
    Posts
    53

    Unhappy

    It doesn't work. Maybe I should have given a sample of my database on sheet2 (actualy it is Sheet5 and is has a name, "Make"). In column A i have a name of a boat, in column B I have different sets of 3 characters. For the same make I can have 2 or 3 or 10 or more groups of 3 characters, all in column B, separated by space. Example:
    Column A Column B
    Action Craft SSU AQC
    Advantage AZD RBV STY HUJ KMD

  4. #4
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    Microsoft 365
    Posts
    14,696
    Try a small adjustment to Paul's sugested formula

    =INDEX(Make!A:A,MATCH("*"&LEFT(B1,3)&"*",Make!B:B,0))

  5. #5
    Registered User
    Join Date
    05-26-2007
    Posts
    53

    Smile

    It worked like a charm. Thanks a zillion to both of you!

  6. #6
    Registered User
    Join Date
    05-26-2007
    Posts
    53

    Unhappy

    However, I need to use the formula in a macro, so I input the following code:
    ActiveCell.FormulaR1C1 = "=INDEX(Make!A:A,MATCH(" * "&LEFT(RC[-" & VAR4 & "],3)" & " & " * ",Make!B:B, 0))""
    where VAR4 is the difference between column# of Make (column U) & column # of ID (column O), so VAR4=7.
    I get an error "type mismatch". What am I doing wrong?

  7. #7
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    Microsoft 365
    Posts
    14,696
    I think the A:A and B:B references will need to change to R1C1 syntax and also a literal " in a formula needs to be "", i.e. where the formula has "*" this needs to be ""*"".

    The easisest way to do this is to use the macro recorder. record a macro in which you put that formula in the required cell and then you'll get the right syntax

  8. #8
    Valued Forum Contributor
    Join Date
    08-26-2006
    Location
    -
    MS-Off Ver
    2010
    Posts
    388
    Another way of getting the correct formula is to:
    1. Select cell with formula.
    2. In the VBE Immediate window type '? activecell.FormulaR1C1' (without the quotes)

    This should display something like =INDEX(Sheet2!C[1],MATCH("*"&LEFT(RC[-18],3)&"*",Sheet2!C[-5], 0))

  9. #9
    Registered User
    Join Date
    05-26-2007
    Posts
    53

    Unhappy

    I ended up with the following code:
    ActiveCell.FormulaR1C1 = "=INDEX(Make!A:A,MATCH(""*""&LEFT(rc[-" & Var4 & "],3)&""*"",Make!B:B,0))"
    where Var4 is calculated before.
    When I run the macro, The formula is correct in the formula bar, but in the cell I get "#NAME?". However, if I place the cursor at the ed of the formula bar, without changing anything and I press "Enter", I get the correct value.
    What is wrong?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1