+ Reply to Thread
Results 1 to 11 of 11

Concatenating unique cells (vertically), leaving "; " between results (for emails)

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-29-2011
    Location
    Bristol, England
    MS-Off Ver
    Excel 2003
    Posts
    471

    Concatenating unique cells (vertically), leaving "; " between results (for emails)

    Hi all
    I'm creating a macro to compile some emails but taking the necessary addresses information from the spreadsheet.

    I have some email addresses in lets say column H (row 8 downwards). Lets say as per example below.

    ----------------- ------------col H
    Row 8--------------- empty cell-----------
    Row 9--------------- empty cell-----------
    Row 10------- John.smith1@hotmail.com
    Row 11------- John.smith1@hotmail.com
    Row 12------- John.smith1@hotmail.com
    Row 13--------------- empty cell-----------
    Row 14------- John.smith14@hotmail.com
    Row 15------- John.smith18@hotmail.com
    Row 16------- John.smith24@hotmail.com
    Row 17--------------- empty cell-----------
    Row 18------- John.smith30@hotmail.com

    Now, I'm after a macro or formula that would look the information from H8 (inclusive) downwards as far as per last cell used in column A. Now it should take all the unique email addresses and concatenate them together and use "; " (read semicolon + 1 space) and populate this into cell H3.

    In our example the correct result in H3 would be like this:
    John.smith1@hotmail.com; John.smith14@hotmail.com; John.smith18@hotmail.com; John.smith24@hotmail.com; John.smith30@hotmail.com;

    But not like this:
    John.smith1@hotmail.com; John.smith1@hotmail.com; John.smith1@hotmail.com; John.smith14@hotmail.com; John.smith18@hotmail.com; John.smith24@hotmail.com; John.smith30@hotmail.com;


    I have attached and example spreadsheet. It has sheet "Before", "After" and ""But not like this"

    My intention is use some email compiling macros, that would take the email addresses from H3.

    Does anyone know how to achieve this sort of concatenation?
    Any help is greatly appreciated.
    Cheers
    Attached Files Attached Files
    Last edited by rain4u; 09-21-2011 at 08:36 PM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Concatenating unique cells (vertically), leaving "; " between results (for emails

    Hello rain4u,

    The macro below setups up both the recipients and CC email columns "H3" and "I3" with the unique addresses separated by a semi-colon and a space. It has been added to the attached workbook.

    ' Thread:  http://www.excelforum.com/excel-programming/793315-concatenating-unique-cells-vertically-leaving-between-results-for-emails.html
    ' Poster:  rain4u
    ' Written: September 21, 2011
    ' Author:  Leith Ross
    
    Sub CreateEmailLists()
    
      Dim Dict As Object
      Dim ListCell As Range
      Dim R As Long
      Dim Recipients As String
      Dim Rng As Range
      Dim RngEnd As Range
      Dim StartRng As Range
      Dim Wks As Worksheet
      
      
          Set Wks = Worksheets("Before")
          
          Set ListCell = Wks.Range("H3")
          Set StartRng = Wks.Range("H8")
          
              GoSub CreateList
              
          Set ListCell = Wks.Range("I3")
          Set StartRng = Wks.Range("I8")
          
              GoSub CreateList
          
    Exit Sub
    
    
    CreateList:
    
          Set RngEnd = Wks.Cells(Rows.Count, StartRng.Column).End(xlUp)
          If RngEnd.Row < StartRng.Row Then Exit Sub Else Set Rng = Wks.Range(StartRng, RngEnd)
          
            Set Dict = CreateObject("Scripting.Dictionary")
            Dict.CompareMode = vbTextCompare
            
            For R = 1 To Rng.Rows.Count
                If StartRng.Item(R, 1) <> "" Then
                   If Not Dict.Exists(Rng.Item(R, 1).Value) Then
                      Dict.Add Rng.Item(R, 1).Value, R
                      Recipients = Recipients & Rng.Item(R, 1) & "; "
                   End If
                End If
            Next R
            
          ListCell = Left(Recipients, Len(Recipients) - 2)
          
    Return
    
    End Sub
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Contributor
    Join Date
    01-29-2011
    Location
    Bristol, England
    MS-Off Ver
    Excel 2003
    Posts
    471

    Re: Concatenating unique cells (vertically), leaving "; " between results (for emails

    Brilliant stuff! Exactly what I was after. Much appreciated!
    Cheers

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Concatenating unique cells (vertically), leaving "; " between results (for emails

    Hello Rain4u,

    Glad you like it. That will be 25 cents to the egress.

  5. #5
    Forum Contributor
    Join Date
    01-29-2011
    Location
    Bristol, England
    MS-Off Ver
    Excel 2003
    Posts
    471

    Re: Concatenating unique cells (vertically), leaving "; " between results (for emails

    Hehe. I think I rushed with the praises. Essentially it works with small bug. Namely in I3 the the following should not be there as its information from H column:
    John.smith1@hotmail.com; John.smith14@hotmail.com; John.smith18@hotmail.com; John.smith24@hotmail.com; John.smith30@hotmail.com; John.smith41@hotmail.com; John.smith47@hotmail.com; John.smith67@hotmail.com; John.smith71@hotmail.com;


    Any ideas?

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Concatenating unique cells (vertically), leaving "; " between results (for emails

    Hello rain4u,

    Looks like I owe a refund. WTF is going on? I will look this over to find the cause. This should not be happening.

  7. #7
    Forum Contributor
    Join Date
    01-29-2011
    Location
    Bristol, England
    MS-Off Ver
    Excel 2003
    Posts
    471

    Re: Concatenating unique cells (vertically), leaving "; " between results (for emails

    Thanks for helping me out.
    PS! I charge interest with refunds. Muahahahahaha a

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Concatenating unique cells (vertically), leaving "; " between results (for emails

    Hello Rain4u,

    What a beginner's mistake! I did not clear the Recipients string before the next call.

    Here is the corrected code...
    ' Thread:  http://www.excelforum.com/excel-programming/793315-concatenating-unique-cells-vertically-leaving-between-results-for-emails.html
    ' Poster:  rain4u
    ' Written: September 21, 2011
    ' Author:  Leith Ross
    
    Sub CreateEmailLists()
    
      Dim Dict As Object
      Dim ListCell As Range
      Dim R As Long
      Dim Recipients As String
      Dim Rng As Range
      Dim RngEnd As Range
      Dim StartRng As Range
      Dim Wks As Worksheet
      
      
          Set Wks = Worksheets("Before")
          
          Set ListCell = Wks.Range("H3")
          Set StartRng = Wks.Range("H8")
          
              GoSub CreateList
              
          Set ListCell = Wks.Range("I3")
          Set StartRng = Wks.Range("I8")
          
              GoSub CreateList
          
    Exit Sub
    
    
    CreateList:
          
          Set RngEnd = Wks.Cells(Rows.Count, StartRng.Column).End(xlUp)
          If RngEnd.Row < StartRng.Row Then Exit Sub Else Set Rng = Wks.Range(StartRng, RngEnd)
          
            Set Dict = CreateObject("Scripting.Dictionary")
            Dict.CompareMode = vbTextCompare
          
            Addx = Rng.Address
            N = Dict.Count
            
            For R = 1 To Rng.Rows.Count
                If Rng.Item(R, 1) <> "" Then
                   If Not Dict.Exists(Rng.Item(R, 1).Value) Then
                      Dict.Add Rng.Item(R, 1).Value, R
                      Recipients = Recipients & Rng.Item(R, 1) & "; "
                   End If
                End If
            Next R
            
          ListCell = Left(Recipients, Len(Recipients) - 2)
          Recipients = ""
          
    Return
    
    End Sub

  9. #9
    Forum Contributor
    Join Date
    01-29-2011
    Location
    Bristol, England
    MS-Off Ver
    Excel 2003
    Posts
    471

    Re: Concatenating unique cells (vertically), leaving "; " between results (for emails

    Thank you for the tip!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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