+ Reply to Thread
Results 1 to 4 of 4

VBA extract text string from another cell formula problem

  1. #1
    Registered User
    Join Date
    01-16-2022
    Location
    Australia
    MS-Off Ver
    365
    Posts
    2

    VBA extract text string from another cell formula problem

    I’m currently trying to include a VBA code into a larger Macro however one particular line (in Part B) is throwing out an error with the FIND function. The objective of the code is to insert two formulas into two diiferrent cells that extract text string from other cell (this formula is copied down accordingly). Below is what I have to date

    ' Part A
    x = Range("A" & Rows.Count).End(xlUp).Row
    sConc = "=B2&"" - ""&XLOOKUP(B2,Table4[Code], Table4[Description])"
    Range("J2").Resize(x - 1).Formula = sConc


    ' Part B
    y = Range("B" & Rows.Count).End(xlUp).Row
    sConc = (LEFT(G1,FIND(" - ",G1)+1)) ' THIS LINE
    Range("K2").Resize(y - 1).Formula = sConc

    I gather the FIND function will not work within VBA and I need to use the INSTR function however I cannot figure out how to extract the required text ( before the " - ") from the cell using the INSTR function.

    Any help on this would be greatly appreciated

    Thanks

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,510

    Re: VBA extract text string from another cell formula problem

    Try:
    Please Login or Register  to view this content.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,510

    Re: VBA extract text string from another cell formula problem

    This works:

    Please Login or Register  to view this content.
    Though I suspect the reference should be to cell G2

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    03-19-2022
    Location
    TBD
    MS-Off Ver
    O365
    Posts
    46

    Re: VBA extract text string from another cell formula problem

    Hi ETMB,

    Instead of using Find(), you may try Excel.WorksheetFunction.Find(). Also, G1 must be changed to Range("G1") when doing the code in Part B.
    Therefore, sConc = (LEFT(G1,FIND(" - ",G1)+1)) will be changed to :

    Please Login or Register  to view this content.
    Note that I changed FIND("-", ..) +1 to be FIND("-", ..) -1 this is to extract the text before "-".
    For example, if the text in G1 is Test-Formula, FIND("-", G1) would return 5 (the position of "-"). So, if the required text is "Test", then we should use Left(G1, 4) not Left(G1, 6).

+ 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] Formula to extract text from string
    By rizmomin in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 03-03-2021, 02:23 PM
  2. Formula to Extract Text from a String
    By andrewc in forum Excel General
    Replies: 3
    Last Post: 12-21-2018, 02:58 PM
  3. Formula to extract text from a string
    By andrea5676 in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 11-01-2016, 03:45 PM
  4. Formula to extract word out of text string in cell
    By Thonkhan in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 05-08-2014, 02:00 AM
  5. [SOLVED] Extract text from a string of text (amend formula to include new criteria)
    By robertguy in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-10-2013, 04:53 PM
  6. [SOLVED] Extract text from string in cell
    By VBA FTW in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-08-2013, 04:14 PM
  7. MID function formula string extract problem
    By ExcelNewby in forum Excel General
    Replies: 3
    Last Post: 02-22-2009, 06:36 AM

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