+ Reply to Thread
Results 1 to 6 of 6

Concatenate a range not just individual cells

Hybrid View

  1. #1
    Forum Expert
    Join Date
    09-20-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    2,278

    Re: Concatenate a range not just individual cells

    Hi
    Below is my code with some remarks included:

    'Declare Variables
    Dim finalValue As String
    Dim c As Range, cell As Range, Rng As Range
    
    'Start loop of 'Unique ID' field.
        For Each c In Range("Table1[Unique Part ID]")
        'Set variable 'finalValue to empty.
        finalValue = ""
        'Set the range to be concatenated.
        Set Rng = Range(Cells(c.Row, 2), Cells(c.Row, c.Column - 1))
    
        'Start loop of each cell in above range.
        For Each cell In Rng
            'Concatente each non empty cell into the 'finalValue' string variable.
            finalValue = finalValue + IIf(CStr(cell.Value) <> "", CStr(cell.Value) & " ", "")
            
        Next cell
            'Place 'finalValue' in 'Unique ID' field.
            c.Value = Trim(finalValue)
    'Go to next cell in 'Unique ID' field.
        Next c
    Mike in post #5 has given you a more succinct piece of code as another alternative. You could also turn my example into a UDF (User Defined Function) and add this to the 'Unique ID' column as a formula:

    Function ConcatenateCells(sourceRange As Excel.Range) As String
        Dim finalValue As String
    
        Dim cell As Excel.Range
    
        For Each cell In sourceRange.Cells
            finalValue = finalValue + IIf(CStr(cell.Value) <> "", CStr(cell.Value) & " ", "")
        Next cell
    
        ConcatenateCells = Trim(finalValue)
    End Function
    Spoiled for choice but you did ask!
    Last edited by DBY; 09-10-2016 at 07:10 AM. Reason: Added Trim to finalValue

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. create range from individual cells
    By maxwell888 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-06-2016, 07:21 AM
  2. How to refernce individual cells instead of a range in a function?
    By PM1985 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 01-12-2015, 04:27 PM
  3. [SOLVED] UDF concatenate randomly chosen cells from range (Horizontal Range)
    By cheesefry in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-15-2014, 03:54 AM
  4. [SOLVED] Appling a range of data to a range of individual cells
    By dubliquid in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 05-08-2013, 07:36 AM
  5. Selecting all individual cells from within a range.
    By milkru1234 in forum Excel General
    Replies: 1
    Last Post: 11-13-2012, 03:03 AM
  6. Excel should lock individual cells or range only
    By centrality in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-30-2006, 03:10 PM
  7. Retrieve individual cells from a range changed between a range
    By baldomero in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-05-2005, 02:31 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1