+ Reply to Thread
Results 1 to 4 of 4

Challenge to find exact text string match

Hybrid View

  1. #1
    Registered User
    Join Date
    01-21-2014
    Location
    Oregon, USA
    MS-Off Ver
    Excel 2007
    Posts
    74

    Challenge to find exact text string match

    In the four rows of text below I'm trying to match each gas stick to its corresponding gasline.

    I think I can count over to "GAS STICK" and get the number. <15>
    From that point I can count over to "," (comma) and get that number. <26>
    So the gas stick will be between the first number and one less than the second number. <GAS STICK 1>

    I need help completing this code to place a the line or stick number in a cell B of the same row.
    Column A would have the description and column B of the same row would either be "1" or "11" respectively.
    Column C would be the shortened description (GAS STICK 1).


    1] "HUNGA, DUNGA, GAS STICK 1, THINGY"
    2] "DOHICKY, GAS STICK 11, THINGUS"
    3] "SCAPPEN, DAPPER, GASLINE 1"
    4] "SUCH, AND, SUCH, GASLINE 11"


    Sub test()
    
    Dim MainWS As Worksheet
    Dim FirstNum As Integer
    Dim LastNum As Integer
    Dim ParentSTR As String
    Dim ActiveRow As Long
    Dim ActiveCol As Long
    Dim LastRow As Long
    
    LastRow = MainWS.Cells(Rows.Count, 1).End(xlUp).Row
    ActiveCol = 1 'Column A
    
    For ActiveRow = 2 To LastRow
    
      ParentSTR = Cells(ActiveRow, ActiveCol).Value
    
      FirstNum = InStr(1, ParentSTR, "GAS STICK")
      LastNum = InStr(FirstNum, ParentSTR, ",")
      LastNum = LastNum -1
    
    Next ActivRow
    
    End Sub
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Challenge to find exact text string match

    You'll need MID(parentstr,firstnum,(lastnum-firstnum))
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

  3. #3
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Challenge to find exact text string match

    Try:

    Sub RunMe()
    Dim ws As Worksheet:    Set ws = Sheets("Sheet1")
    Dim rCell As Range
    Dim arrSplit() As String
    Dim lSplit As Long
    
    For Each rCell In ws.Range("A2:A" & ws.Range("A" & Rows.Count).End(xlUp).Row)
        If Not rCell.Value = "" Then
            arrSplit = Split(rCell.Value, ",")
                For lSplit = LBound(arrSplit) To UBound(arrSplit)
                    If Not InStr(1, arrSplit(lSplit), "GASLINE") = 0 Then
                        If IsNumeric(Right(arrSplit(lSplit), 2)) Then
                            ws.Range("B" & rCell.Row).Value = Right(arrSplit(lSplit), 2)
                            ws.Range("C" & rCell.Row).Value = arrSplit(lSplit)
                        End If
                    End If
                    If Not InStr(1, arrSplit(lSplit), "GAS STICK") = 0 Then
                        If IsNumeric(Right(arrSplit(lSplit), 2)) Then
                            ws.Range("B" & rCell.Row).Value = Right(arrSplit(lSplit), 2)
                            ws.Range("C" & rCell.Row).Value = arrSplit(lSplit)
                        End If
                    End If
                Next lSplit
        End If
    Next rCell
    
    End Sub

  4. #4
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Challenge to find exact text string match

    Heres another..

    Private Sub CommandButton1_Click()
      Dim n, i As Long
        With CreateObject("vbscript.regexp")
            .Pattern = "(GAS STICK |GASLINE )\d+"
            For i = 3 To Range("A" & Rows.Count).End(xlUp).Row
                Set myMatches = .Execute(Cells(i, 1))
                For Each n In myMatches
                If UBound(Split(n, " ")) > 1 Then
                    Cells(i, Range("IV" & i).End(xlToLeft).Column).Offset(, 1).Resize(, 2).Value = Array(Split(n, " ")(2), n)
                    Else
                    Cells(i, Range("IV" & i).End(xlToLeft).Column).Offset(, 1).Resize(, 2).Value = Array(Split(n, " ")(1), n)
                    End If
                Next n
            Next i
        End With
    End Sub
    Attached Files Attached Files

+ 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] Instr to look for a complete, exact match of a string
    By SandPounder1 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 04-30-2014, 03:50 PM
  2. [SOLVED] Possible to search for a substring withing a string of text which is an exact match?
    By Urugmo88 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-07-2014, 04:09 AM
  3. Find an exact word within a text string
    By juanmimr in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 08-08-2013, 10:30 AM
  4. [SOLVED] Find Exact Match Text String Using Formula
    By sweetrevelation in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 09-12-2012, 10:53 AM
  5. [SOLVED] Count if - not exact match - from cell not string
    By s_twigge in forum Excel General
    Replies: 2
    Last Post: 06-27-2012, 10:44 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