+ Reply to Thread
Results 1 to 13 of 13

VBA VLookup a number in a text string within cells range

  1. #1
    Registered User
    Join Date
    12-10-2014
    Location
    Boise, Idaho, USA
    MS-Off Ver
    2011
    Posts
    11

    VBA VLookup a number in a text string within cells range

    Hello!

    I am encountering an issue I can't solve. I spent few hours trying to figure out but still nothing.

    I have a datatable in one tab with like column A list of numbers (123456; 789456; 456132; etc.) that are corresponding to a department name (purchasing, production, quality, etc.) column B.

    I have in another tab "report" a list of those numbers but they are included in a serie of other numbers like for instance 001-123456-008; 001-789456-008; etc.).

    I am trying to look up within the tab report the number in the middle and match them to my datatable. The 001 and the 008 does not interest me.

    I tried
    Range("B1:B20") = Application.WorksheetFunction.VLookup(Sheets("Report").Range("A1:A20"), Sheets("DataTableGL").Range("C1:D20"), 2, False) and it works when I remove manually the 001 at the beginning and the 008 at the end. Yet I have thousands of lines ...

    So I tried to Vlookup the value in the middle ... but does not work.
    Range("B1:B20") = Application.WorksheetFunction.VLookup(Sheets("Report").Range("A1:A20"), Sheets("DataTableGL").Range(Mid(Cells(1, 1), 5, 6), 2, False))

    I tried to extract the text and separate everything in several columns but the file gets slow and heavy.

    If someone has an idea, I would be more than happy to try things!

    Thank you for your help,

    Ronan
    Last edited by Ronanus43; 12-20-2014 at 02:32 PM.

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,873

    Re: VBA VLookup a number in a text string within cells range

    Attach a sample workbook. Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    View Pic
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,929

    Re: VBA VLookup a number in a text string within cells range

    Cant help with the VBA, but if your dept number is consistent with your sample, , this will extract it from that sample...
    =--MID(A1,5,6)

    You could then use that inside your vlookup (or index/match)
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  4. #4
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,519

    Re: VBA VLookup a number in a text string within cells range

    If they are not all like your example, but also if they are, you can find it by getting the value between the dashes (-)
    The following code will extract the values between the dashes from Column A and put them in Column B.
    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    08-17-2013
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    132

    Re: VBA VLookup a number in a text string within cells range

    Apply this in your code:
    Please Login or Register  to view this content.
    Regards,

  6. #6
    Forum Contributor
    Join Date
    08-17-2013
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    132

    Re: VBA VLookup a number in a text string within cells range

    Check this User defined function if you format for lookup value is consistent throughout the sheet:


    Please Login or Register  to view this content.
    Regards,

  7. #7
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: VBA VLookup a number in a text string within cells range

    Don't know if this can help, but Vlookup also has a wildcard feature. Vlookup("*" & A2 & "*",.....

  8. #8
    Registered User
    Join Date
    12-10-2014
    Location
    Boise, Idaho, USA
    MS-Off Ver
    2011
    Posts
    11

    Re: VBA VLookup a number in a text string within cells range

    Example.xlsmHello,

    Thank you all for your answers. I tried the last VBA proposition with the use of the Function but could not get it work. I still need to add the function in every cell. The idea was to open the workbook and run the macro and have all the values looked up.

    I think that the first VBA code, the one that looks Inside de dash could work but If I could avoid this one as it creates another step of copy/paste in another column.

    I attached a file where there is a tiny table as example with 2 tabs. Maybe it can help.
    If there is no way to do it, I will just try to extract the value between the dash to another column.

    Again thanks
    Ronan

  9. #9
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,519

    Re: VBA VLookup a number in a text string within cells range

    Give this (attached) a whirl

    Or:
    In Report Sheet Cell D2: =VLOOKUP(MID(C2,5,6)*1,DataTableGL!$C$2:$F$32,2)
    In Report Sheet Cell E2: =VLOOKUP(MID(C2,5,6)*1,DataTableGL!$C$2:$F$32,3)
    Change the Cell references as required.

    Copy on down
    Attached Files Attached Files
    Last edited by jolivanes; 12-12-2014 at 05:04 PM. Reason: Added VLookup

  10. #10
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,519

    Re: VBA VLookup a number in a text string within cells range

    If you have a large Range, this might be slightly faster

    Please Login or Register  to view this content.

  11. #11
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA VLookup a number in a text string within cells range

    Hello Ronanus43,

    Welcome to the Forum!

    The attached workbook has the macro below added to it. There is a button "Run" on the "Report" which executes the macro.

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  12. #12
    Registered User
    Join Date
    12-10-2014
    Location
    Boise, Idaho, USA
    MS-Off Ver
    2011
    Posts
    11

    Re: VBA VLookup a number in a text string within cells range

    Hello Leith,

    Sorry it took me a while before being able to test it. It works perfectly I am impressed! Thank you so much.
    I need to learn that now and progress.
    Thank you!
    Ronan

  13. #13
    Registered User
    Join Date
    12-10-2014
    Location
    Boise, Idaho, USA
    MS-Off Ver
    2011
    Posts
    11

    Re: VBA VLookup a number in a text string within cells range

    Again thank you, the code is really good.

    Ronan

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] IF range of cells contains string of text, return contents of cell where string is found
    By nobodyukno in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 07-03-2013, 09:56 AM
  2. Find row number of text string in a range of cells
    By Steven Fleck in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 01-06-2013, 08:38 PM
  3. [SOLVED] Finding text string within Range and returning Row/Column number
    By IanE78 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 09-09-2012, 12:13 PM
  4. Formula to Count the Number of Occurrences of a Text String in a Range
    By poug1903 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-10-2011, 06:49 AM
  5. Replies: 1
    Last Post: 09-18-2009, 04:15 PM

Tags for this Thread

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