+ Reply to Thread
Results 1 to 3 of 3

VLOOKUP with REPLACE Function

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408

    VLOOKUP with REPLACE Function

    Hi everyone,

    I am trying to modify the following code:

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count = 1 And Target.Column = 6 Then
    If (Range("F" & Target.Row) = "" And Range("G" & Target.Row) <> "") Or Mid(Target.Value, 3, 2) <> "12" Then
    Range("G" & Target.Row) = ""
    Else
    If Mid(Target.Value, 3, 2) = "12" Then
    With Range("G" & Target.Row)
    .Formula = "=IF($F" & Target.Row & "="""","""",VLOOKUP($F" & _
    Target.Row & "," & "Pname,2,FALSE))"
    End With
    If IsError(Target.Offset(0, 1).Value) Then
    Range("G" & Target.Row).Value = ""
    MsgBox "The number you entered is not found in the list, please enter the associated name.", _
                vbExclamation, "Number Not Found"
    Else
    Range("G" & Target.Row).Value = Range("G" & Target.Row).Value
    End If
    End If
    End If
    End If
    End Sub
    to include the following:

    If MID(Target.Row,3,2)=”12”,VLOOKUP(Replace(Target.Row,3,2,”12”),Pname,2,FALSE), VLOOKUP(Target.Row,,Pname,2,FALSE))

    That is, if the user enters 3636512690 - for example, I want to replace the 3rd and 4th characters with 12 and look up 3612512690 instead; but if the user enters 3612512690, then look up 3612512690.

    I tried:

    .Formula = "=VLOOKUP($F" & _
    Target.Row & ",REPLACE($F"&Target.Row&",3,2,"12"),Pname,2,FALSE)"

    but it is now working - I'm getting, Compile error: Expected: end of statement."

    I have attached a sample file - VlookupReplace.

    Can someone help me, please?

    Thank you,
    Gos-C

    P.S. I have modified the code (see VlookupReplace_2) and now I am getting "Unable to get the VLookup property of the WorksheetFunction class.
    Attached Files Attached Files
    Last edited by Gos-C; 10-22-2012 at 09:19 AM. Reason: Attached sample files and error messages
    Using Excel 2010 & Windows 10
    "It is better to be prepared for an opportunity and not have one than to have an opportunity and not be prepared."

  2. #2
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: VLOOKUP with REPLACE Function

    Hi Gos-C

    I didn't try to decipher all your code to see what it's doing. You can try changing this line of code from this
    x = Replace(Target.Value, 3, 2, "12")
    to this
    x = WorksheetFunction.Replace(CStr(Target.Value), 3, 2, "12")
    and this line of code from this
    Range("G" & Target.Row) = Application.WorksheetFunction.VLookup(x, Pname, 2, False)
    to this
     Range("G" & Target.Row) = Application.WorksheetFunction.VLookup(Val(x), Pname, 2, False)
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  3. #3
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408

    Re: VLOOKUP with REPLACE Function

    Hi jaslake, it works! Thank you very much.

    Cheers,
    Gos-C

+ 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