Excel VBA =显示一系列单元格中的值

时间:2016-04-19 19:59:02

标签: excel-vba vba excel

有人可以帮我吗?我还是比较新的,我无法让它发挥作用。我想:

显示电子邮件中一系列单元格中的数据。我在网上发现这个代码让我开始,但它是用于文本框:

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

一切正常,除了它只用于一个文本框,我在单元格中有列表和whatsits,我也需要发送。我尝试使用的代码是:

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

而不是:

.body = ActiveSheet.TextBoxes(1).Text

但这只是发送一封空白电子邮件。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

双转换范围并使用Join()方法:

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

这会将范围转换为单维数组,然后可以使用Join()方法将其与给定分隔符连接。

相关问题