2017-07-24 42 views
2

我想從我的Excel電子表格中發送一封電子郵件,其中包含從列A到列G的最後一行範圍的正文。從Excel發送Outlook電子郵件將最後一行範圍文本放在正文消息中

我試圖添加我的最後一行範圍到下面的代碼沒有成功。

你能幫忙嗎?

Sub Mail_LastRowRange_Outlook() 

Dim OutApp As Object 
Dim OutMail As Object 
Dim strbody As String 

Set OutApp = CreateObject("Outlook.Application") 
Set OutMail = OutApp.CreateItem(0) 

strbody = "Thanks for your help" & '**I Would like to insert_ 
      my last row range from column A to G in here** 

On Error Resume Next 
With OutMail 
    .To = "" 
    .CC = "" 
    .BCC = "" 
    .Subject = "Looking for a solution" 
    .Body = strbody 
    .Attachments.Add ("C:\test.txt") 
    .Send 
End With 
On Error GoTo 0 

Set OutMail = Nothing 
Set OutApp = Nothing 
End Sub 

回答

1
Sub test1() 
    Dim lastRow As Long 
    'get last row from column A of worksheet with 'codename' Sheet1 
    lastRow = getLastRow(Sheet1, 1) 

    Dim r As Range 
    Set r = Sheet1.Range("A" & lastRow & ":G" & lastRow) 

    Dim str As String 
    'Join the cell texts with a space - lazy coding... 
    str = Join(Application.Transpose(Application.Transpose(r.Value2)), " ") 

    MsgBox str 
End Sub 


Public Function getLastRow(ws As Worksheet, Optional col As Long) As Long 
    Dim arr As Variant 

    If col > 0 Then 
     arr = Intersect(ws.UsedRange, ws.Columns(col)).Value2 
    Else 
     arr = ws.UsedRange.Value2 
    End If 

    Dim i As Long, j As Long 
    For i = UBound(arr) To 1 Step -1 
     For j = UBound(arr, 2) To 1 Step -1 
       If Len(arr(i, j)) > 0 Then 
        getLastRow = i + ws.UsedRange.Row - 1 
        Exit Function 
       End If 
     Next j 
    Next i 
End Function 

上面的函數是最強大的功能來獲得的最後一行工作表/列中的實際數據值。一切是脆弱的,包括Range.Find("*", , , , , xlPrevious)這是易受activeCell在過濾的ListObject

下面的功能易受像過濾行有兩件事情,有數據最後一排,等等等等

Public Function getLastRow2(ws As Worksheet, col As Long) As Long 
    getLastRow2 = ws.Cells(ws.Rows.Count, col).End(xlUp).Row 
End Function 
+0

嗨MacroMarc,請如何將Sub test1()放在正文郵件中? – Tom

+0

非常感謝您的支持。它現在可以正常工作。在A欄中,我收到了以電子郵件形式出現的日期數字。我會盡力整理出來。再次感謝 – Tom

0

這應該工作,只要有在A列中沒有空白單元格從單元格A1到最後一行A列中的數據替換,你也行「strbody = ....」與所有這些。

Cells(1,1).Select 
Selection.End(xlDown).Select 

Dim s As String 
s = ActiveCell.Value  'A 
ActiveCell.Offset(0, 1).Select 
s = s & ActiveCell.Value 'B 
ActiveCell.Offset(0, 1).Select 
s = s & ActiveCell.Value 'C 
ActiveCell.Offset(0, 1).Select 
s = s & ActiveCell.Value 'D 
ActiveCell.Offset(0, 1).Select 
s = s & ActiveCell.Value 'E 
ActiveCell.Offset(0, 1).Select 
s = s & ActiveCell.Value 'F 
ActiveCell.Offset(0, 1).Select 
s = s & ActiveCell.Value 'G 

strbody = "Thanks for your help" & s 
+0

非常感謝。有時無論如何,我的電子表格中都有一些空單元格。你認爲能否爲我的案子找到解決方案? – Tom

+0

不需要選擇單元格。只需掛鉤範圍對象並使用它。對於最後一行,最好從底部開始,使用xlUp,如:Cells(sheet1.rows.count,1).End(xlUp)'。然後通過偏移量獲得到G列的範圍 – MacroMarc

+0

從您知道不會爲空的列開始。或者,放入一個虛擬列,然後鍵入以找到結尾。 – user2529170

0

確定具體的行,並創建一個範圍對象,並遍歷它,例如:

Dim rng As range 
    Dim item As range 
    Dim row As Long 

    Set rng = range(Cells(row, 1), Cells(row, 7)) 

    For Each item In rng 
     strBody = strBody & " " & item 
    Next item 
+0

對不起我的問題。特定的行是否與lastrow相對應? – Tom

+0

是的,但你必須找到最後一行。您可以在上面的答案中使用MacroMarc的建議方法,並將結果存儲在新的範圍對象中,並通過range.row查詢行值。 – user8355222

+0

非常感謝您回覆 – Tom

相關問題