1

I have column H that contains long GET requests on sheet 1 such as:

H
GET /profiles/text/23493495_3492/g93id93kd
GET /edit/result/393493/te3903k4d

I would like to have a second sheet with the following type of list in columns A and B:

A                  B
23493495           identifier1
3903k4             realid2
g93id              realid3

Ultimately, I would like a function that will search sheet 1 column H for any of the values in sheet 2 column A. Most of the time there is no separator so I need it to search for strings within the GET string. Once a value in sheet 2 column A is matched with a value in sheet 1 column H, I would like the function to take the corresponding text in sheet 2 column B and print it in sheet 1 column I. There may be multiple matches in a cell, so that would need to be taken into account. So if using the example above:

In H1, there would be a match of 23493495 and g93id within the string. I would like sheet 1 column I to display:

I
identifier1, realid3

I initially started with the below code where I had to specify the list but it doesn't use a second sheet or print the corresponding text of the match. So I would rather have something that meets my needs above, but below is an example of what I have tried so far:

=ListSearchB(J2, "23493495 g93id")

With this module I found that I modified a little:

    Function ListSearchB(text As String, wordlist As String, Optional caseSensitive As Boolean = False)
    Dim strMatches As String
    Dim res As Variant
    Dim arrWords() As String
    arrWords = Split(wordlist)
    On Error Resume Next
    Err.Clear
    For Each word In arrWords
        If caseSensitive = False Then
            res = InStr(LCase(text), LCase(word))
        Else
            res = InStr(text, word)
        End If
        If res > 0 Then
            strMatches = strMatches & word
        End If
    Next word
    If Len(strMatches) <> 0 Then
        strMatches = Right(strMatches, Len(strMatches))
    End If
    ListSearchB = strMatches
End Function

That gives me:

23493495g93id in column I, and I wasn't sure how to separate the two with a comma.

In general though, I would prefer, to use some way to pull the list from sheet 2 and display the value in column I as specified initially.

1 Answer 1

1

Give this a try - just adjust the sheet names where commented before running

Sub your_sub()
    Dim sGet As Worksheet
    Dim sIDs As Worksheet
    Dim rget As Range
    Dim rIds As Range

    'ADJUST SHEET NAME
    With Worksheets("GET")
        Set rget = Range(.Range("H1"), .Range("h" & .Rows.count).End(xlUp))
    End With

    'ADJUST SHEET NAME
    With Worksheets("IDs")
        Set rIds = Range(.Range("A1"), .Range("A" & .Rows.count).End(xlUp))
    End With

    mys = vbNullString
    i = 1

    For Each cget In rget

        For Each cIds In rIds
            If InStr(cget.Value, cIds) <> 0 Then
                mys = mys & ", " & cIds.Offset(0, 1).Value
            End If
        Next cIds

        If mys <> vbNullString Then
            mys = Right(mys, Len(mys) - 2)
            'ADJUST SHEET NAME
            Worksheets("GET").Range("I" & i).Value = mys
        End If

        i = i + 1
        mys = vbNullString

    Next cget

End Sub
1
  • That works great, thanks! Btw, I added: Option Compare Text to the top line to allow for case insensitivity. Jul 18, 2014 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.