Solved

Need custom function to combine cells in Excel 2010

Posted on 2016-11-30
3
69 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:Alex Campbell
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 8

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:Alex Campbell
ID: 41907760
Great, thanks!
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

632 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