Share via

User Defined function to concatenate string

Anonymous
2014-07-23T15:22:14+00:00

People,

I am trying to create a UDF in vba to concatenate a string with inverted commas, end results should look like  --- '1234567',

can anybody help me please.

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2014-07-23T18:36:14+00:00

Hi Hafeezur,

Considerably preferable to my earlier suggestion would be:

'=========>>

Public Function ConcatQuotes(ParamArray rng()) As Variant

    Dim arr() As Variant

    Dim rArea As Variant, rCell As Range

    Dim i As Long, j As Long

    ConcatQuotes = CVErr(xlErrNA)

    For Each rArea In rng

        For Each rCell In rArea.Cells

            If Not rCell.Value = vbNullString Then

                i = i + 1

                ReDim Preserve arr(1 To i)

                arr(i) = "'" & rCell.Value & "'"

            End If

        Next rCell

    Next rArea

        If CBool(i) Then

             ConcatQuotes = Join(arr, ",")

        End If

End Function

'<<========

===

Regards,

Norman

Was this answer helpful?

0 comments No comments

Answer accepted by question author

Anonymous
2014-07-23T15:41:49+00:00

People,

I am trying to create a UDF in vba to concatenate a string with inverted commas, end results should look like  --- '1234567',

can anybody help me please.

Hi Hafeezur,

Try:

Alt-F11 to open the VBA editor

Alt-IM to insert a new code module

In the new module, paste the following code

'=========>>

Public Function ConcatQuotes(aCell As Range)

   ConcatQuotes = "'" & aCell.Value & "'"

End Function

'<<========

Alt-Q to close the VBA editor

Use the UDF in a worksheet as

     =ConcatQuotes(D7)

===

Regards,

Norman

Was this answer helpful?

0 comments No comments

9 additional answers

Sort by: Most helpful
  1. Anonymous
    2014-07-24T12:43:11+00:00

    simple and elegant solution Norman !!! This solves my problem, thanks..

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2014-07-23T17:02:46+00:00

    Hi Hafeezur,

    Re-reading your post, I realise that I may have misinterpreted your requirement. 

    Try, therefore:

    '=========>>

    Public Function ConcatQuotes(rng As Range)

    Dim arr As Variant

    Dim i As Long, j As Long

    i = rng.Cells.Count

    ReDim arr(1 To i)

    For j = 1 To i

        arr(j) = "'" & rng(j).Value & "'"

    Next j

       ConcatQuotes = Join(arr, ",")

    End Function

    '<<========

    ===

    Regards,

    Norman

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2014-07-23T16:31:15+00:00

    Hi,

    Have a look to my add-in:

    JP's Extension

    It has a function

    =RangeConcat(ConcatRange;Separator;TextQualifier;Suppress)

    Note that my default seaparator is the semi^-colon but it will work with comma also.

    Was this answer helpful?

    0 comments No comments