VBA Macro to Email Last column D Data as Body

tlaltmey

New Member
Joined
Nov 10, 2021
Messages
20
Office Version
  1. 365
  2. 2019
Hi All,

I'm attempting to make a quick macro that emails data found in column D of Sheet "Scorecard" with the last set of data. My current issue is that column D actually is full of a formula that has its data determined as a percentage based off of columns C and B, so it is unable to find the actual data I'm looking for.

VBA Code:
Sub Submit()

Application.DisplayAlerts = False

Dim path As String
Dim filename1 As String
Dim emailApplication As Object
Dim emailItem As Object

 ChDrive "F"
 ChDir "Filepath"
 filename1 = "Filename"
 ActiveWorkbook.SaveAs Filename:=path & filename1, FileFormat:=52
    
    Set emailApplication = CreateObject("Outlook.Application")
    Set emailItem = emailApplication.CreateItem(0)
                
                    emailItem.to = "DummyEmail@Email.com"
                    emailItem.Subject = "First Pass Yield this Week"

                    emailItem.Body = "First Pass Yield" & ThisWorkbook.Sheets("Scorecard").Range("D" & Rows.Count).End(xlUp).Value

                    emailItem.Attachments.Add ActiveWorkbook.FullName

                    emailItem.Send
                    
                    Set emailItem = Nothing
                    Set emailApplication = Nothing
                    
                    Application.ScreenUpdating = False
                    Application.DisplayAlerts = False
                    Application.AutomationSecurity = msoAutomationSecurityForceDisable
                    Application.AskToUpdateLinks = False
                    
                    Application.DisplayAlerts = True
                    
                    ActiveWorkbook.Close True

End Sub

The formula found in Column D is the following:

=IFERROR((C2-B2)/C2,"")

The issue is that this formula is implemented throughout all of column D but currently the only cell that has actual data is D2, which will then be D3 next week.

IE: Can I send data from column D based on the last row of data from Column C, as that column will be manually implemented, if this makes sense.
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
is the body supposed to be 1 cell, or lots of data in many cells?
if 1 cell then
.Body = "message" & range("D1").value
 
Upvote 0
is the body supposed to be 1 cell, or lots of data in many cells?
if 1 cell then
.Body = "message" & range("D1").value

The cell is meant to be just one cell but the cell meant to be called out is the last cell in column D that has actual, non error data. D1 will only work once, as next week the data will be D2, then D3, then D4, etc.

Essentially I want to use column C as my "last row" data to call out but I want to call out column D in that row determined by C.
 
Upvote 0
VBA Code:
Sub Submit()

Application.DisplayAlerts = False

Dim path As String
Dim filename1 As String
Dim emailApplication As Object
Dim emailItem As Object
Dim lastRow As Long
Dim mainWB As Workbook
Dim wsDest As Worksheet
Dim wsCopy As Worksheet

Set mainWB = ActiveWorkbook
Set wsCopy = mainWB.Worksheets("Sheet1")

lastRow = Cells(Rows.Count, 3).End(xlUp).Row

 ChDrive "F"
 ChDir "Path1"
 filename1 = "name"
 ActiveWorkbook.SaveAs Filename:=path & filename1, FileFormat:=52
   
    Set emailApplication = CreateObject("Outlook.Application")
    Set emailItem = emailApplication.CreateItem(0)
               
                    emailItem.to = "email@email.com"
                    emailItem.Subject = "subject"

                    emailItem.Body = "Text " & wsCopy.Range("D" & lastRow).Value

                    emailItem.Attachments.Add ActiveWorkbook.FullName

                    emailItem.Send
                   
                    Set emailItem = Nothing
                    Set emailApplication = Nothing
                   
                    Application.ScreenUpdating = False
                    Application.DisplayAlerts = False
                    Application.AutomationSecurity = msoAutomationSecurityForceDisable
                    Application.AskToUpdateLinks = False
                   
                    Application.DisplayAlerts = True
                   
                    ActiveWorkbook.Close True
                   
End Sub

Solved
 
Upvote 0
Solution

Forum statistics

Threads
1,214,950
Messages
6,122,436
Members
449,083
Latest member
Ava19

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top