+ Reply to Thread
Results 1 to 10 of 10

Alternative to concatenate

Hybrid View

  1. #1
    Registered User
    Join Date
    06-06-2008
    Posts
    34

    Alternative to concatenate

    Hi,

    I'm looking to take a long list of e-mail addresses, probably 100 orso, that will be in Cell A2 downwards, one by one. In non-VB I would've used the concatenate formula to combine them all with a "; " in between, so I could then copy-paste in Outlook.

    I know this isn't available in VB, so I was wondering if there's an obvious way to do this? My current longwinded solution is:

    LValue = Worksheets("Results").Range("A2") & "; " & Worksheets("Results").Range("A2")
    
    Worksheets("Results").Range("A102") = LValue
    Last edited by Toddneyx; 12-12-2010 at 06:20 AM.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Alternative to concatenate

    Not sure what you mean by isn't available in VB, but how about:

    Sub ConcatenatEmailAddresses()
        Dim steMailList As String, x As Long
    
        For x = 1 To 100
            steMailList = steMailList & ";" & Range("A2").Cells(x, 1)
        Next x
        Range("A102") = Right(steMailList, Len(steMailList) - 1)
    End Sub
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

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

    Re: Alternative to concatenate

    Hello Toddneyx,

    This code will find the last cell in column "A", and then add a semi-colon to each cell in the range.
      Dim Cell As Range
      Dim SendTo As String
      
        Set Rng = Range("A2", Cells(Rows.Count, "A")).End(xlUp)
        
        For Each Cell In Rng
          SendTo = SendTo & ";"
        Next Cell
    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!)

  4. #4
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Alternative to concatenate

    recipients=Join([transpose(A2:A102)], ";")
    or

    With createObject("outlook.application").CreateItem(0)
      .Subject ="example"
      .To=Join([transpose(A2:A102)], ";")
      .send
    end with
    Last edited by snb; 12-06-2010 at 05:53 PM.



  5. #5
    Registered User
    Join Date
    06-06-2008
    Posts
    34

    Re: Alternative to concatenate

    Quote Originally Posted by snb View Post
    recipients=Join([transpose(A2:A102)], ";")
    or

    With createObject("outlook.application").CreateItem(0)
      .Subject ="example"
      .To=Join([transpose(A2:A102)], ";")
      .send
    end with
    And if my data was in a specific worksheet, would the following work?

    Dim Loco As Range
    
    Loco = Worksheets("Results").Range("A2:A5")
    
    With CreateObject("outlook.application").CreateItem(0)
      .Subject = ""
      .To = Join([transpose(Loco)], ";")
    
    End With
    I get the error Object variable or With block variable not set

  6. #6
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Alternative to concatenate

    sheets("Results").Range("A2:A5").Name="Loco"
    
    With CreateObject("outlook.application").CreateItem(0)
      .Subject = ""
      .To = Join([transpose(Loco)], ";")
    End With
    I hope you realise it's an unnecessary step, as I illustrated in my previous post.

  7. #7
    Registered User
    Join Date
    06-06-2008
    Posts
    34

    Re: Alternative to concatenate

    Sorry I must've missed your point in the previous post, which step is unnecessary?

  8. #8
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Alternative to concatenate

    The defining of a namedrange.

    This will do:

      .To=Join([transpose(A2:A102)], ";")
    and if you want to make that more robust:

      .To=Join(filter([transpose(A2:A1000)],"@"),";")

  9. #9
    Registered User
    Join Date
    06-06-2008
    Posts
    34

    Re: Alternative to concatenate

    Ah ok, wasn't sure how to write the reference to another worksheet within that

  10. #10
    Registered User
    Join Date
    06-06-2008
    Posts
    34

    Re: Alternative to concatenate

    I've managed to get it working, partially - now it's just a matter of getting these two functions working under one button.

    Private Sub PasteData()
    
    Worksheets("Results").Range("A2:Z900").Clear
    
    Worksheets("Data").Range("D2:D220").SpecialCells(xlCellTypeVisible).Copy _
    Destination:=Worksheets("Results").Range("A2")
    
        Dim dbresults As Range
        Set dbresults = Worksheets("Results").Range("A1:A200")
        Set dbresults = dbresults.Rows("2:" & dbresults.Rows.Count)
        Dim r As Range
        
        For Each r In dbresults.Rows
           
        If r.Cells(, EMAIL_COL) = "" _
        Then
        ElseIf r.Cells(2, EMAIL_COL) = "" _
        Then
        Else
        r.Cells(, EMAIL_COL) = r.Cells(, EMAIL_COL) & "; "
        End If
       
        Next r
        End
        
    End Sub
     
    Private Sub CopyEmail()
    
        Dim myvalue As String
        Dim x As Long
      
        myvalue = ""
        For x = 4 To 7
        myvalue = myvalue & Worksheets("Results").Cells(x, "A")
        Next x
        
        Worksheets("Results").Cells(102, "A").Value = myvalue
        
        Dim DataObj As New MSForms.DataObject
        Dim S As String
        S = myvalue
        DataObj.SetText S
        DataObj.PutInClipboard
    
    End Sub
    
    Private Sub Commandbutton1_Click()
    
    CopyEmail
    PasteData
    
    End Sub

    Is my sub PasteData() not "ending" correctly? Because if I assign these two subs to two different buttons, then it works 100%. But if I assign both subs to one button, or both functions under one sub - it fails.
    Last edited by Toddneyx; 12-08-2010 at 10:54 AM. Reason: Fixed If statement coding

+ 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