Email From VBA

Joejnr

New Member
Joined
Dec 6, 2019
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi, I don't normally use VBA so I apologise for my lack of knowledge!

I have a spreadsheet that has a header row, then each person has their own row with their own data below it. I need to run a macro to send these people an email with only their line of data.

So far I have managed to create the macro very simply, a sample of the code is below so that it takes the email address from Column J and creates an email and moves on to the next row.

The issue I have is that when it puts the "Time" in (I have 3 times to add to the email) it converts it from 01:40:38 to a number like 4.023453-2.

I have looked at every page on the internet to find an easy work around and can't come up with one! Does anyone know how I can format the time to stay as hh:mm:ss as it is the most important thing!

Any help is really appreciated as I am losing what little hair I have left!

Sub test2()

Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range



Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
For Each cell In Worksheets("Sheet1").Columns("J").Cells
Set OutMail = OutApp.CreateItem(0)
If cell.Value Like "?*@?*.?*" Then 'try with less conditions first
With OutMail
.To = Cells(cell.Row, "J").Value
.Subject = "Test Email"
.Body = "Date: " & Cells(cell.Row, "A").Value & vbNewLine & _
"Name: " & Cells(cell.Row, "B").Value & vbNewLine & _
"Time: " & Cells(cell.Row, "C").Value & vbNewLine & _
"Time2: " & Cells(cell.Row, "D").Value & vbNewLine & _
"Time3: " & Cells(cell.Row, "E").Value & vbNewLine & _

.display

End With
Cells(cell.Row, "M").Value = "sent"
Set OutMail = Nothing
End If
Next cell

Application.ScreenUpdating = True

End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Not a pro with VBA either but maybe like this:

VBA Code:
Format(Cells(cell.Row, "C").Value, "hh:mm:ss")
 
Upvote 0
Not a pro with VBA either but maybe like this:

VBA Code:
Format(Cells(cell.Row, "C").Value, "hh:mm:ss")

I tried that but it seems to break the code, I will give it another try. Really struggling with finding fix online, and I know I can't be the first person to have this issue!
 
Upvote 0
Hi, Joejnr
Welcome to the Forum.
Instead of value, try using the text property:
"Time: " & Cells(cell.Row, "C").Text & vbNewLine & _
 
Upvote 0
You're welcome, glad to help, & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,214,520
Messages
6,120,007
Members
448,935
Latest member
ijat

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