Forum Discussion
Molekula
Aug 23, 2022Copper Contributor
Extract text between two characters
Hello everybody, Recently I encountered a quirky problem while trying to retrieve some data from a cell. Hope you may help me. So I have an export csv table as following (5 rows for example): ...
- Aug 23, 2022
Here is a small custom VBA function:
Function ExtractURL(ByVal s As String) As String Dim p1 As Long Dim p2 As Long Dim p3 As Long s = Replace(s, Chr(160), " ") p1 = InStr(1, s, ".") p2 = InStrRev(s, " ", p1 - 1) p3 = InStr(p1 + 1, s, " ") If p3 > 0 Then s = Mid(s, p2 + 1, p3 - p2 - 1) Else s = Mid(s, p2 + 1) End If If Left(s, 4) <> "www." Then s = "www." & s End If ExtractURL = s End Function
Use like this: in a cell formula:
=ExtractURL(A2)
HansVogelaar
Aug 23, 2022MVP
Here is a small custom VBA function:
Function ExtractURL(ByVal s As String) As String
Dim p1 As Long
Dim p2 As Long
Dim p3 As Long
s = Replace(s, Chr(160), " ")
p1 = InStr(1, s, ".")
p2 = InStrRev(s, " ", p1 - 1)
p3 = InStr(p1 + 1, s, " ")
If p3 > 0 Then
s = Mid(s, p2 + 1, p3 - p2 - 1)
Else
s = Mid(s, p2 + 1)
End If
If Left(s, 4) <> "www." Then
s = "www." & s
End If
ExtractURL = s
End Function
Use like this: in a cell formula:
=ExtractURL(A2)
Molekula
Aug 24, 2022Copper Contributor
Thank you very much HansVogelaar!
Your answer corresponds exactly to what I was looking for even though it is a VBA code. (I am still learning to write in VB)
Works like a charm,