简体   繁体   中英

Excel VBA = Display values in a range of cells

Can someone help me with this? I'm still fairly new with it and I can't get it to work. I am trying to:

Display the data in a range of cells in an email. I found this code online to get me started, but it is for text boxes:

Sub Sample()
   'Setting up the Excel variables.
   Dim olApp As Object
   Dim olMailItm As Object
   Dim iCounter As Integer
   Dim Dest As Variant
   Dim SDest As String

   'Create the Outlook application and the empty email.
   Set olApp = CreateObject("Outlook.Application")
   Set olMailItm = olApp.CreateItem(0)

   'Using the email, add multiple recipients, using a list of addresses in column A.
   With olMailItm
       SDest = ""
       For iCounter = 1 To WorksheetFunction.CountA(Columns(1))
           If SDest = "" Then
               SDest = Cells(iCounter, 1).Value
           Else
           SDest = SDest & ";" & Cells(iCounter, 1).Value
           End If
       Next iCounter

    'Do additional formatting on the BCC and Subject lines, add the body text from the spreadsheet, and send.
       .BCC = SDest
       .Subject = "FYI"
       .Body = ActiveSheet.TextBoxes(1).Text
       .Send
   End With

   'Clean up the Outlook application.
   Set olMailItm = Nothing
   Set olApp = Nothing
End Sub

Everything works, except it is only for one text box, and I have lists and whatsits in cells that I need to send too. The code that I'm trying to use is:

.body = Activesheet.range("B1:E1").Value 

instead of:

.body = ActiveSheet.TextBoxes(1).Text

but that just sends a blank email. Any advice?

Double transpose the range and use the Join() method:

.Body = Join$(WorksheetFunction.Transpose(WorksheetFunction.Transpose(Range("B1:E1").Value)), vbTab)

This will transpose the range into a single dimension array, which can then be joined with a given delimiter using the Join() method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM