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-08-30T06:00:57+00:00

    Hi Norman,

    Hope you are doing good !!!

    Just an addition to my above question, can you please tell me how can i add this function to add-ins so that i can access this function whenever i open a new workbook.

    Regards,

    Hafeez

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2014-07-24T14:33:16+00:00

    This is just more than a answer for me !!! the is super cool solution

    Hi Hafeezur,

    Thank you for your kind feedback. I am pleased that the function is useful.

    ===

    Regards,

    Norman

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2014-07-24T14:11:14+00:00

    This is just more than a answer for me !!! the is super cool solution

    Was this answer helpful?

    0 comments No comments