Mitch_Bird
New Member
- Joined
- Jan 4, 2015
- Messages
- 4
Hi All,</SPAN>
(VBA learner
</SPAN></SPAN> )</SPAN>
I am working on a VBA Code for emailing data from an excel spreadsheet. The below code will grab the data in a range of cells (A3:H50) and send email to whoever I have in cell H1 with subject in cell J1 – I have tested this code and it works fine but I do need help in making some alterations. </SPAN>
Instead of sending all data in the sheet, can I please get help with a code to grab last row of data with the headings and send that with a generic message above that data? The headings are from A3 to H3 and will never change but as there are multiple rows of data added in each day I will only require to send last row. Every time a new row of data is entered I will need and email sent, example - data entered in A35:H35 email to send with headings and Data for A35:H35 only, next row A36:H36 and repeat</SPAN>
Thanks in advance for your help.
Mitch
(VBA learner

I am working on a VBA Code for emailing data from an excel spreadsheet. The below code will grab the data in a range of cells (A3:H50) and send email to whoever I have in cell H1 with subject in cell J1 – I have tested this code and it works fine but I do need help in making some alterations. </SPAN>
Instead of sending all data in the sheet, can I please get help with a code to grab last row of data with the headings and send that with a generic message above that data? The headings are from A3 to H3 and will never change but as there are multiple rows of data added in each day I will only require to send last row. Every time a new row of data is entered I will need and email sent, example - data entered in A35:H35 email to send with headings and Data for A35:H35 only, next row A36:H36 and repeat</SPAN>
Code:
Sub Send_Email2()
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Dim rngAttach As Range
'Set range for file attachment, cell should contain the path and file name
Sheets("Sheet1").Select
With ActiveSheet
Set rngAttach = .Range("H1")
End With
Set rng = Nothing
' Only send the visible cells in the selection.
Set rng = Sheets("Sheet1").Range("A3:H50").SpecialCells(xlCellTypeVisible)
If rng Is Nothing Then
MsgBox "The selection is not a range or the sheet is protected. " & _
vbNewLine & "Please correct and try again.", vbOKOnly
Exit Sub
End If
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = ThisWorkbook.Sheets("Sheet1").Range("H1").Value
'CC = ThisWorkbook.Sheets("Email").Range("").Value
'.BCC = ThisWorkbook.Sheets("Email").Range("").Value
.Subject = ThisWorkbook.Sheets("Sheet1").Range("J1").Value
'.Display
.HTMLBody = RangetoHTML(rng)
' In place of the following statement, you can use ".Display" to
' display the e-mail message.
'strFileName = Dir(rngAttach.Value)
'.Attachments.Add Replace(rngAttach.Value, "*.*", "") & strFileName
'.Attachments.Add rngAttach.Value
.send
End With
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Function RangetoHTML(rng As Range)
' By Ron de Bruin.
Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook
TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
'Copy the range and create a new workbook to past the data in
rng.Copy
Set TempWB = Workbooks.Add(1)
With TempWB.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
.Cells(1).PasteSpecial xlPasteValues, , False, False
.Cells(1).PasteSpecial xlPasteFormats, , False, False
.Cells(1).Select
Application.CutCopyMode = False
On Error Resume Next
.DrawingObjects.Visible = True
.DrawingObjects.Delete
On Error GoTo 0
End With
'Publish the sheet to a htm file
With TempWB.PublishObjects.Add( _
SourceType:=xlSourceRange, _
Filename:=TempFile, _
Sheet:=TempWB.Sheets(1).Name, _
Source:=TempWB.Sheets(1).UsedRange.Address, _
HtmlType:=xlHtmlStatic)
.Publish (True)
End With
'Read all data from the htm file into RangetoHTML
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
RangetoHTML = ts.ReadAll
ts.Close
RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
"align=left x:publishsource=")
'Close TempWB
TempWB.Close savechanges:=False
'Delete the htm file we used in this function
Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function
Thanks in advance for your help.
Mitch