Concatenate until blank cell

Posted on 2006-04-08
Last Modified: 2008-03-04

I have the following information on an excel sheet and i want a macro to concatenate the data untill the blank cell is found i have tried to explain it below


A      B      C      D      D
Folder 1      Folder 2      Folder 3      Folder 4      
Folder 3      Folder 4      Folder 5      Folder 6      Folder 7
Folder 1      Folder 2      Folder 3      Folder 4      
Folder 3      Folder 4      Folder 5            

Output required

I used Concatanate(CellA&"\"& Cell B&...etc)

Folder 1\Folder 2\Folder 3\Folder 4\
Folder 3\Folder 4\Folder 5\Folder 6\Folder 7
Folder 1\Folder 2\Folder 3\Folder 4\
Folder 3\Folder 4\Folder 5\\

I do not want the extra "\" in my output

Please can someone help me with a macro to generate the output without the extra "\"

Question by:ramkumar8516
    LVL 92

    Accepted Solution

    Hi Ram,

    Add this new function to your workbook's VBProject, in a regular module:

    Option Explicit

    Function ConcRange(Substrings As Range, Optional Delim As String = "", _
        Optional AsDisplayed As Boolean = False, Optional SkipBlanks As Boolean = False)
        ' Function by Patrick Matthews, Matt Vidas, and rberke

        ' Concatenates a range of cells, using an optional delimiter.  The concatenated
        ' strings may be either actual values (AsDisplayed=False) or displayed values.
        ' If NoBlanks=True, blanks cells or cells that evaluate to a zero-length string
        ' are skipped in the concatenation
        ' Substrings: the range of cells whose values/text you want to concatenate.  May be
        ' from a row, a column, or a "rectangular" range (1+ rows, 1+ columns)
        ' Delimiter: the optional separator you want inserted between each item to be
        ' concatenated.  By default, the function will use a zero-length string as the
        ' delimiter (which is what Excel's CONCATENATE function does), but you can specify
        ' your own character(s).  (The Delimiter can be more than one character)
        ' AsDisplayed: for numeric values (includes currency but not dates), this controls
        ' whether the real value of the cell is used for concatenation, or the formatted
        ' displayed value.  Note for how dates are handled: if AsDisplayed is FALSE or omitted,
        ' dates will show up using whatever format you have selected in your regional settings
        ' for displaying dates.  If AsDisplayed=TRUE, dates will use the formatted displayed
        ' value

        ' SkipBlanks: Indicates whether the function should ignore blank cells (or cells with
        ' nothing but spaces) in the Substrings range when it performs the concatenation.
        ' If NoBlanks=FALSE or is omitted, the function includes blank cells in the
        ' concatenation.  In the examples above, where NoBlanks=False, you will see "extra"
        ' delimiters in cases where the Substrings range has blank cells (or cells with only
        ' spaces)
        Dim CLL As Range
        For Each CLL In Substrings.Cells
            If Not (NoBlanks And Trim(CLL) = "") Then
                ConcRange = ConcRange & Delim & IIf(AsDisplayed, Trim(CLL.Text), Trim(CLL.Value))
            End If
        Next CLL

        ConcRange = Mid$(ConcRange, Len(Delim) + 1)
    End Function

    Now use it like this:


    That will concatenate the items in row 1, delimiting with a backslash, and ignore the cells with blanks.


    LVL 92

    Expert Comment

    by:Patrick Matthews

    The user-defined function I provided did exactly as the Asker requested.



    Author Comment

    Hi Patrick,

    Thank you for the help, I was on leave for some medical attention on my shoulder and that is the reason i did not access the answers.  Sorry for the late response.

    I appereciate your time and help,
    Thank you once again,

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
    The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    728 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

    17 Experts available now in Live!

    Get 1:1 Help Now