0

I need your help to get URL from a specific cell. For an example assume the below is the cell data I am referring to.


Microsoft Teams meeting

Join on your computer or mobile app

Click here to join the meeting https://teams.microsoft.com/l/meetup-join/19%3ameeting_OWEzN2JmZmEtOTVmMS00ZDc4LThlNzQtNjQyNWM0ZjllODIx%40thread.v2/0?context=%7c%22Tid%22%3a%227a916015-20ae-4ad1-9170-eafd915e9272%22%2c%24Oid%22%3a%22b8ed972c-91b4-4fe1-a5d5-1410ea30a159%22%7d

Learn More https://aka.ms/JoinTeamsMeeting | Meeting options https://teams.microsoft.com/meetingOptions/?organizerId=b8ed972c-79b4-4fe1-a5d5-1410ea30a159&tenantId=7a916015-20ae-4ad1-9170-eefd915e9272&threadId=19_meeting_OWEzN2JmZmEtOTVmMS00ZDc4LThlNzQtNjQyNWM0ZjllODIx@thread.v2&messageId=0&language=en-US


So from above cell text i want to copy the first URL to another cell. Request your guidance.

1 Answer 1

2

Please, try the next function:

Function extractURLs(strCell As String) As Variant
    Dim frst As Long, lst As Long, arr, k As Long, sp As Long
    Dim pos As Long, URLNo As Long, i As Long
    
    'count existing number if URLs:
    Do
       pos = InStr(pos + 1, strCell, "https:")
       If pos > 0 Then
            URLNo = URLNo + 1
        End If
    Loop While pos > 0
    If URLNo = 0 Then extractURLs = Array("Error"): Exit Function 'in case of  no URL being found
    ReDim arr(URLNo - 1) 'ReDim the array to keep the found URLs
    For i = 1 To URLNo    'loop between the above occurrences found number
        frst = InStr(frst + 1, strCell, "https:")            'determine the first occurrence for https: string
        lst = InStr(frst, strCell, vbLf)                       'determine the last occurrence (starting from frst) for end of line
        If lst > 0 Then                                         'if the string is found:
            sp = InStr(Mid(strCell, frst, lst - frst), " ")  'determine if a space exists in the string between first and last
            If sp > 0 Then                                     'if it exists:
                arr(k) = Mid(strCell, frst, sp): k = k + 1 'it returns the string up to the first space
            Else
                arr(k) = Mid(strCell, frst, lst - frst): k = k + 1 'if returns the string up to the end of line
            End If
        End If
    Next i
     extractURLs = arr   'return the array content
End Function

It can be tested like in such a code:

Sub testExtractURLs()
   Dim strTest As String, arr, i As Long
   strTest = ActiveCell.value
   arr = extractURLs(strTest)
   If UBound(arr) = 0 Then
        If arr(0) = "Error" Then
            MsgBox "No any URL could be found..."
        Else
            Debug.Print arr(0)
        End If
  Else
        For i = 0 To UBound(arr)
             Debug.Print arr(i)
        Next i
  End If
End Sub

Please, test it and send some feedback

Sign up to request clarification or add additional context in comments.

11 Comments

thank you for the quick response @FaneDuru and it worked too but in my actual work scenario it didn't because this function captured the URL till [.com/] only but in my cell text there is a URL after [.com/] also. I have updated the actual cell text in my question
@Kalpesh Koli This only mean that you did not correctly explain the "actual work scenario". I am not so sure that the updated one is always covered by your scenario. You must understand that the code cannot think like a human being... So, the 'scenario' you show must be something able to offer an algorithm based no what to extract the necessary URL(s). So, not only two such URLs can be involved? Should they be more then 3? Should always the first URL string will finish to the end of line, the second will end before the "|" character and the third one will end the whole string?
@Kalpesh Koli Please, note that not being able to completely describe your actual work scenario. nobody will be able to help you. We are not mind readers...
@Kalpesh Koli If not something confidential, can you share the workbook containing the strings to be analyzed? We maybe will better define your working scenario...
@Kalpesh Koli Please, try the updated code. It will return how many occurrences exist, if the above assumption is correct. I mean, the character where the url address ends... I would like to receive some feedback. I do not care too much about the notoriety points. I only want the confirmation that the offered solution works for you...
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.