Concatenate problem

Craig1

Active Member
Joined
Jun 11, 2009
Messages
320
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

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

Trebor76

Well-known Member
Joined
Jul 23, 2007
Messages
4,945
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
 

Craig1

Active Member
Joined
Jun 11, 2009
Messages
320
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.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,428
Messages
5,837,177
Members
430,481
Latest member
nizambakhshi

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
Top