Replace last comma with and in cell

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
<code style="box-sizing: inherit; margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; vertical-align: baseline; max-height: 300px; overflow: auto;">
Code:
Sub [COLOR=#303336]example[/COLOR][COLOR=#303336]()[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#101094]Dim[/COLOR][COLOR=#303336] s1 [/COLOR][COLOR=#101094]As[/COLOR][COLOR=#101094]String[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] s2 [/COLOR][COLOR=#101094]As[/COLOR][COLOR=#101094]String[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#101094]Dim[/COLOR][COLOR=#303336] pos [/COLOR][COLOR=#101094]As[/COLOR][COLOR=#101094]Integer[/COLOR][COLOR=#303336]
    s1 [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] ActiveSheet[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]ActiveCell[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Value
    s2 [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#7D2727]""[/COLOR][COLOR=#303336]
    pos [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] InStr[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]1[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] s1[/COLOR][COLOR=#303336],[/COLOR][COLOR=#7D2727]" "[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
    [/COLOR][COLOR=#101094]While[/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336]pos [/COLOR][COLOR=#303336]<>[/COLOR][COLOR=#7D2727]0[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
        s2 [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] s2 [/COLOR][COLOR=#303336]&[/COLOR][COLOR=#303336] Mid[/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336]s1[/COLOR][COLOR=#303336],[/COLOR][COLOR=#7D2727]1[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] pos [/COLOR][COLOR=#303336]-[/COLOR][COLOR=#7D2727]1[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]&[/COLOR][COLOR=#7D2727]","[/COLOR][COLOR=#303336]
        s1 [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] Mid[/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336]s1[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] pos [/COLOR][COLOR=#303336]+[/COLOR][COLOR=#7D2727]1[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
        pos [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] InStr[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]1[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] s1[/COLOR][COLOR=#303336],[/COLOR][COLOR=#7D2727]" "[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
    [/COLOR][COLOR=#101094]Wend[/COLOR][COLOR=#303336]
    s2 [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] s2 [/COLOR][COLOR=#303336]&[/COLOR][COLOR=#303336] s1
    ActiveSheet[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]ActiveCell[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Value [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] s2
[/COLOR][COLOR=#101094]End[/COLOR][COLOR=#101094]Sub
[/COLOR]


Found this code online and need help make it do what I want.

I want replace the last comma with "and" instead of this replacing all spaces with comma .

There may be :
1. No word
2. Only one word
3. Only two words
4. More than two words.

So I want all those factors considered.

Thanks
</code>
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
[Table="width:, class:grid"][tr][td="bgcolor:#C0C0C0"][/td][td="bgcolor:#C0C0C0"]
A​
[/td][td="bgcolor:#C0C0C0"]
B​
[/td][td="bgcolor:#C0C0C0"]
C​
[/td][/tr][tr][td="bgcolor:#C0C0C0"]
1​
[/td][td="bgcolor:#F3F3F3"]
Input
[/td][td="bgcolor:#F3F3F3"]
Output
[/td][td="bgcolor:#F3F3F3"]
[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
2​
[/td][td]a[/td][td="bgcolor:#CCFFCC"]a[/td][td="bgcolor:#CCFFCC"]B2: =km(A2)[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
3​
[/td][td]a, b[/td][td="bgcolor:#CCFFCC"]a and b[/td][td][/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
4​
[/td][td]a, b, c[/td][td="bgcolor:#CCFFCC"]a, b and c[/td][td][/td][/tr]
[/table]


Code:
Function km(sInp As String) As String
  Dim i As Long
  
  i = InStrRev(sInp, ",")
  If i Then
    km = Left(sInp, i - 1) & " and" & Mid(sInp, i + 1)
  Else
    km = sInp
  End If
End Function
 
Last edited:
Upvote 0
Hi,

In case a formula solution is acceptable:


Book1
AB
7aa
8a, ba and b
9
10a, b, ca, b and c
11a, b, c, da, b, c and d
Sheet622
Cell Formulas
RangeFormula
B7=IF(A7="","",IFERROR(SUBSTITUTE(A7,","," and",LEN(A7)-LEN(SUBSTITUTE(A7,",",""))),A7))
 
Upvote 0
Hi Kelly,

I have made a function and sub that will convert the final comma of a string into " and" - if no commas are present then the string will stay as is. Is this what you're looking for?

Code:
Function COUNTCOMMAS(txt As String) As String
    With CreateObject("VBScript.RegExp")
        .Global = True
        .Pattern = "\,"
        COUNTCOMMAS = Len(txt) - Len(.Replace(txt, ""))
    End With
End Function


Sub ChangeLastComma()
On Error GoTo Capture
    Dim numCommas As Integer
    Dim txt As String
    Dim k As Integer
    Dim commaPosit As Integer
                
    txt = ActiveCell.Value
    numCommas = COUNTCOMMAS(txt)
    
        For k = 1 To numCommas
            commaPosit = WorksheetFunction.Search(",", txt, commaPosit + 1)
        Next k
        ActiveCell.Value = Left(txt, commaPosit - 1) & Replace(txt, ",", " and ", commaPosit)
        ActiveCell.Value = Application.Trim(ActiveCell.Value)
End


Capture:
    End
    
End Sub

Running this procedure will change only the active cell value.
 
Last edited:
Upvote 0
If that's what the op wants, then how about this:

Code:
Function ReplaceLastComma(r As String) As String
Dim x As Long
x = Len(r) - Len(Replace(r, ",", ""))
If x Then
    ReplaceLastComma = Application.WorksheetFunction.Substitute(r, ",", " and ", x)
Else
    ReplaceLastComma = r
End If
End Function
 
Upvote 0
I am very sorry for not stating clearly what I wanted.

Meanwhile these codes have also been useful to me as the have solve some of my earlier challenges .

Thanks
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,208
Members
452,618
Latest member
Tam84

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top