Share via

An UDF for Concatenate Function

Anonymous
2023-11-03T01:48:49+00:00

Hello I was wondering whether it is possible to achieve this result using VBA/UDF?

https://ibb.co/2yn729f

Basically what I wanted to do is to be able to concatenate various string from different cell and achieve the shown result without typing the concatenate formula over and over but only one function with VBA/UDF, so we can get different kind of result based on the chosen cells

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

2 answers

Sort by: Most helpful
  1. Andreas Killer 144.1K Reputation points Volunteer Moderator
    2023-11-04T10:42:54+00:00

    .

    Sub Test()
    Dim Source As Range
    Dim i As Long
    Dim Value

    Set Source = Selection
    If Source.Areas.Count < 1 Then
    MsgBox "Select at min. 2 cells and the last cell as destination."
    Exit Sub
    End If
    For i = 1 To Source.Areas.Count - 1
    Value = Value & Source.Areas(i) & " "
    Next
    Source.Areas(Source.Areas.Count) = Left(Value, Len(Value) - 1)
    End Sub

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2023-11-03T18:02:37+00:00

    Hi,

    try this vba code

    select the desired cells and run the below vba macro

    result in activesheet in cell C40

    Sub Selection_Cells()

    Dim rng As Range, r As Range, tgtCell As Range

    Set rng = Selection

    Set tgtCell = Range("C40") '<< result

    Dim s As String

    If MsgBox("NOTE!!! selected area is" & vbNewLine & rng.Address & " ??", vbOKCancel) = vbCancel Then Exit Sub

    s = ""

    For Each r In rng

    If s = "" Then

    s = r.Value

    Else

    s = s & " " & r.Value

    End If

    Next

    tgtCell = s

    End Sub

    =====================

    note

    add a button and assign the macro

    Was this answer helpful?

    0 comments No comments