Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove everything including the comma from the last comma of a string. 1

Status
Not open for further replies.

Moss100

Technical User
Aug 10, 2004
578
0
16
GB
Hello,

I need to remove everything from the last comma of a string (including the last comma)

So for example I have an address

20 Bright Street, Walsham, England, CH41 0AD

And I need:

20 Bright Street, Walsham, England

If anyone is able to help me with this it would be great.

AND

If anyone fancies a further challenge, the actual data I am working with appears as

20 Bright Street {Smith}, Walsham, England, CT85 8AT

What ultimately I want to do is remove everything bewteen the brackets "{" & "}" (including the brackets)
AND
Also remove everything after the last comma, including the comma, so the end result is:

20 Bright Street, Walsham, England

Many thanks Mark
 
I am sure thee is a better, 'fancier' way to do it...

Code:
Option Explicit

Sub Test()
Dim str As String

str = "20 Bright Street {Smith}, Walsham, England, CT85 8AT"

Debug.Print RemoveStuff(str)

End Sub

Function RemoveStuff(ByRef strIn As String) As String
Dim ary() As String
Dim i As Integer
Dim strOut As String

If InStr(strIn, ",") Then
    ary = Split(strIn, ",")
    For i = LBound(ary) To UBound(ary) - 1
        If i = LBound(ary) Then
            strOut = ary(i)
        Else
            strOut = strOut & "," & ary(i)
        End If
    Next i
End If

If InStr(strOut, "{") And InStr(strOut, "}") Then
    strOut = Left(strOut, InStr(strOut, "{") - 2) & Mid(strOut, InStr(strOut, "}") + 1)
End If

RemoveStuff = strOut

End Function

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
Yes, I am. I can't help it [lol]

Here is a little shorter/different version of the Function:

Code:
Function RemoveStuff(ByRef strIn As String) As String
Dim i As Integer
Dim strOut As String

If InStr(strIn, ",") Then[blue]
    For i = Len(strIn) To 1 Step -1
        If Mid(strIn, i, 1) = "," Then
            strOut = Left(strIn, i - 1)
            Exit For
        End If
    Next i[/blue]
End If

If InStr(strOut, "{") And InStr(strOut, "}") Then
    strOut = Left(strOut, InStr(strOut, "{") - 2) & Mid(strOut, InStr(strOut, "}") + 1)
End If

RemoveStuff = strOut

End Function

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
Or ...

Code:
[COLOR=blue]Function RemoveStuff(ByRef strIn As String) As String
    With CreateObject("VBScript.RegExp")
        .Pattern = "( {.*}|,[^,]*?$)"
        .Global = True
        RemoveStuff = .Replace(strIn, "")
    End With
End Function[/color]

 
I like creating functions but you could use a fairly simple expression:

Code:
Left("20 Bright Street, Walsham, England, CH41 0AD",InstrRev("20 Bright Street, Walsham, England, CH41 0AD",",")-1)

If your field name is [Full Address]:
Code:
Left([Full Address],InstrRev([Full Address],",")-1)

Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016
 
My preferred solution would be:
Split the 'Full Address' into its separate fields in the table:

[pre]
Address || FieldX || FieldY || FieldZ || FieldQ
-----------------||--------||---------||---------||---------
20 Bright Street || Smith || Walsham || England || CT85 8AT
123 Main Street || Brown || Dundee || Scotland|| ABC XYZ
[/pre]

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
I totally agree with using multiple fields for multiple data items. It's super easy to combine them as needed.

Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016
 
Duane, your one liner does not address "remove everything bewteen the brackets "{" & "}" (including the brackets)
 
But it does replace my 6 lines of code with 1 line of his. [thumbsup2]

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top