Solved

Need custom function to combine cells in Excel 2010

Posted on 2016-11-30
3
48 Views
Last Modified: 2016-11-30
I need a function where I can give a range such as A2:D2
and have it combine them the text values.
I would also like an option to add a divider.

Example of Custom Function NeededCombine.xlsx
0
Comment
Question by:Alex972
3 Comments
 
LVL 7

Accepted Solution

by:
Mike in IT earned 500 total points
ID: 41907472
You will have to create your own function in VBA like this:

Function Combine(rng As Range, Sp As String)
Dim cell As Range
Dim Result

For Each cell In rng
    If Sp <> "" Then
        If cell.Value <> "" Then
            Result = Result & Sp & cell.Value
        End If
        Combine = Right(Result, Len(Result) - 1)
    Else
        If cell.Value <> "" Then
            Combine = Combine & cell.Value
        End If
    End If
Next cell
End Function

Open in new window


Then you use it just like a regular function. Mine needs 2 inputs so it would be used like this:
=combine(A9:D9,"")

Open in new window

or this
=combine(A7:D7,"/")

Open in new window


This will work even if it is multiple rows as well as multiple columns.
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 41907486
Try this:
Public Function CustomCombine(rng As Range, Optional strDivider As String) As String
    Dim cell As Range
    Dim strTemp As String
    If rng.Rows.Count = 1 Then
        For Each cell In rng.Cells
            strTemp = strTemp & cell.Value & strDivider
        Next
    Else
        ' more than 1 row, don't proccess
    End If
    CustomCombine = strTemp
End Function

Open in new window

0
 
LVL 1

Author Closing Comment

by:Alex972
ID: 41907760
Great, thanks!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
Use Windows Task Scheduler to print a Word document weekly so your printer ink won't dry out.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question