Concatenate problem

Craig1

Active Member
Joined
Jun 11, 2009
Messages
322
Hi guys,
I obtained some code from your good selves a while ago about a concatenate problem I came across.
Basically what i wanted was no double comma's when concatenating multiple cells and one in the middle was empty.
The code below works like a treat except for one little issue, if the first cell is empty I still get that darned comma, I have tried quite a few ways to solve this and all to no avail.

Function myConCat(S As Range) As String
Dim cell As Range
Dim con As String
Dim I As Integer
I = 1
For Each cell In S
If I = 1 Then
con = cell
ElseIf cell <> "" Then
con = con & ", " & cell
End If
I = I + 1
Next cell
myConCat = con
End Function

Any help would be appreciated.

Thanks Craig.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hi Craig,

See how this goes:

Code:
Option Explicit
Function myConCat(S As Range) As String
    
    Dim cell As Range
    Dim con As String
    Dim I As Integer
    
    I = 1
    
    For Each cell In S
        If I = 1 Then
            con = cell
        ElseIf cell <> "" Then
            If con = "" Then
                con = con & cell
            Else
                con = con & ", " & cell
            End If
        End If
        I = I + 1
    Next cell
    
    myConCat = con
    
End Function

Regards,

Robert
 
Upvote 0
Thanks for that Robert, it works fantastic. Thanks for the continued help and support.

I see what you did now, I was trying to get to that, I had the right idea about having another IF statement but just couldn't get there.

Thanks again.

Craig.
 
Upvote 0
No problem. I'm glad we were able to provide you with a solution :)
 
Upvote 0

Forum statistics

Threads
1,214,922
Messages
6,122,281
Members
449,075
Latest member
staticfluids

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