Solved

Need custom function to combine cells in Excel 2010

Posted on 2016-11-30
3
29 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 6

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
 

Author Closing Comment

by:Alex972
ID: 41907760
Great, thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to read all the sheets in excel 12 35
COPY from excel to notepad 3 35
VBA MS Word "Table of Contents" extractor` 4 23
Easy Excel formula needed 4 26
A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

910 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now