Tech Support Guy banner

Excel 2016 and VBA to create automated business checklist

1468 Views 13 Replies 2 Participants Last post by  OBP
Hello,

I've been online all day trying to find more info on creating an excel spreadsheet with VBA scripts to send an email when there is an upcoming due date. I'm trying to learn about macros and VBA scripts, but I've never learned how to code and this stuff looks very dense.

In this spreadsheet that I've attached, I've laid out a few generic examples. I'm creating a to-do list and I want this spreadsheet(hopefully running on a task scheduler) to remind me by e-mail when there is something upcoming that has not been checked off yet. I added the check box in an attempt to make an =if formula but I'm not sure if that's helpful.

Is there any way to run this program on a scheduler everyday and have it email me when dates are upcoming if they have not been completed yet?

Thanks

Attachments

Status
Not open for further replies.
1 - 14 of 14 Posts
Yes to both your questions.
There are many examples of emailing at due dates on this Forum, it is probably one of the most asked questions.
At the top left of the Business Applications Forum there is a "Search" box where you can enter "email from Excel".
There are 9 pages of threads, most of which are Excel ones.
Yes to both your questions.
There are many examples of emailing at due dates on this Forum, it is probably one of the most asked questions.
At the top left of the Business Applications Forum there is a "Search" box where you can enter "email from Excel".
There are 9 pages of threads, most of which are Excel ones.
Thanks!
Yes to both your questions.
There are many examples of emailing at due dates on this Forum, it is probably one of the most asked questions.
At the top left of the Business Applications Forum there is a "Search" box where you can enter "email from Excel".
There are 9 pages of threads, most of which are Excel ones.
do you know any good sources for learning VBA coding?
You can google most things, but for me you can't beat a good book.
I would look at buying one on ebay for an older version of Excel as they are cheaper but will give you a good idea of the basics.
Are you struggling with this?
As I am working on the basically the same thing for another poster.
Well I am seeing a lot of the same questions on this forum, but I don't know what each command means and what task it performs so I was hoping to learn so I can edit it myself. I am finding some good sources online though so I think I might just keep practicing.
Ask me and I will reply tomorow.
Chet, this is the code on the macro started by Ctrl + q that I used for the post here
https://forums.techguy.org/threads/using-exel-cell-values-to-send-automatic-emails.1197123/

I have annotated the code to tell you what each part is doing, perhaps that will help you understand what is going on.

Code:
Sub Macro1()
'
' Macro1 Macro
' email.
'
' Keyboard Shortcut: Ctrl+q
'
    Dim objOutlook  As New Outlook.Application, ccstring As String  ' The Dim (Dimension) statement tells the Editor what to expect when you assign something to a "Variable".
    Dim objMessage  As MailItem, spacestring As Integer    ' A variable can hold a program or object name like outlook mail message
    Dim subject As String, body As String, EmailAddress As String, tempemailaddress As String ' or a string which is text
    Dim lastrow As Long, count As Integer, Wksh As Worksheet, count2 As Long, found As Integer ' or an integer, long or double which hold mathematical values
On Error GoTo errorcatch ' if an error occurs in the code GoTo the line  called errorcatch and display the error type and stop the code
    body = "Please complete your task for the CMT number mentioned above." & Chr(13) ' chr(13) is the Asci code for a carriage Return
    body = body & "This task is now past-due and at risk of cancelation. " & Chr(13)
    body = body & "Please let me know if there is something I can do to assist to get this task completed by end of day. "

Set Wksh = Sheets("SAPBW_DOWNLOAD") ' the word Set tells the Editor you are setting an Object to the variable
Wksh.Select ' select the worksheet now called Wksh
lastrow = Wksh.Cells(Rows.count, "A").End(xlUp).Row ' find the last row by coming up the worksheet until it finds a filled cell in Column A
Application.ScreenUpdating = False ' turn off the screen updating to make the code run faster and less distracting to the user
For count = 125 To lastrow ' This the start of a Loop using For and Next, there are other types like Do/Until and While/Wend, count is the counter being used for this loop which will look at the rows of the worksheet
    If Cells(count, 14) > 4 Then ' using the Cells(row, column) reference system test if the cell value is greater than 4
        EmailAddress = "" ' if the value is >4 then set the emailaddress to nothing in readines to add items to it
        tempemailaddress = Cells(count, 12) ' set the temp email address to a Cell contents
        If Left(tempemailaddress, 1) = " " Then tempemailaddress = Right(Cells(count, 12), Len(Cells(count, 12)) - 1) ' test for a space at the start of the email address and remove it
        spacestring = InStr(1, tempemailaddress, " ") ' using the instring functio test if there are spaces in the email address indicating more than one email address
        If spacestring = 0 Then ' if there is NO space assign the contents to the EmailAddress on the next line
            EmailAddress = tempemailaddress
        Else ' or if there isn't one do the following
            found = 1 ' set the space found indicator to 1
            For count2 = 2 To Len(tempemailaddress) ' start a second loop to go through the characters in the cell looking for spaces
                If Mid(tempemailaddress, count2, 1) = " " Then ' test for space
                    EmailAddress = EmailAddress & ";" & Mid(tempemailaddress, found, count2 - found) ' a space is found so add the part before space to the EmailAddress with a ";"
                    found = count2 ' reset the space found indicator to where the space was found
                    'MsgBox EmailAddress & " - " & found & " = " & count2 ' this is not currently used but provides the user with a message box to show the current EmailAddress
                End If ' the end of the if/the/else test
            Next count2 ' the incremtor of count2
                EmailAddress = Right(EmailAddress, Len(EmailAddress) - 1) ' remove any spare ; from the end of the EmailAddress
        End If ' the end of the test for spaces
        subject = Cells(count, 5) ' set the email subject to a cell content
        ccstring = Cells(count, 2) ' set the email CC to a cell content
        'MsgBox EmailAddress
        'GoTo skip
                Set objMessage = objOutlook.CreateItem(olMailItem) ' start Outlook and set up a new email
                With objMessage ' the word with means use code associated with previous Set command
                    .To = EmailAddress
                    .CC = ccstring
                    .subject = subject
                    .body = body
                    .Display
                    '.Send
                End With
        Set objOutlook = Nothing 'reset Outlook
        Set objMessage = Nothing ' and the email
    End If ' the end of the first if question looking at Cell(count, 14)
skip:
   
Next count ' increment the first counter which will move testing down to the next row until it reaches "Lastrow".
Application.ScreenUpdating = True
MsgBox "emails have been sent." ' tell the user that the emails should have been sent assuming nothing goes wrong with outlook etc
Exit Sub ' stop the code and return to the worksheet
errorcatch: ' the line where you want the GoTo to go to to declare the error
MsgBox Err.Description

End Sub
See less See more
Okay thanks so much for helping out. I'm gonna look through this and see if I can understand and use this. Thanks again!
I've been going over this and also reading a book on VBA and Excel but the book isn't very in depth. I've edited the code you linked here to try to fit my spreadsheet but I get an error when I try to run it.

Sub Macro1()

'

' Macro1 Macro

' email.

'

' Keyboard Shortcut: Ctrl+q

'

Dim objOutlook As New Outlook.Application, ccstring As String

Dim objMessage As MailItem, spacestring As Integer

Dim subject As String, body As String, EmailAddress As String, tempemailaddress As String

Dim lastrow As Long, count As Integer, Wksh As Worksheet, count2 As Long, found As Integer

On Error GoTo errorcatch

body = "Action is required in this loan checklist" & Chr(13)


Set Wksh = Sheets("SAPBW_DOWNLOAD")

Wksh.Select

lastrow = Wksh.Cells(Rows.count, "A").End(xlUp).Row

Application.ScreenUpdating = False

For count = 11 To lastrow

If Cells(count, 7) > 7 Then

EmailAddress = ""

tempemailaddress = Cells(2, 6)

If Left(tempemailaddress, 1) = " " Then tempemailaddress = Right(Cells(count, 12), Len(Cells(count, 12)) - 1)

spacestring = InStr(1, tempemailaddress, " ")

If spacestring = 0 Then

EmailAddress = tempemailaddress

Else found = 1

For count2 = 2 To Len(tempemailaddress)

If Mid(tempemailaddress, count2, 1) = " " Then

EmailAddress = EmailAddress & ";" & Mid(tempemailaddress, found, count2 - found)

found = count2

'MsgBox EmailAddress & " - " & found & " = " & count2

End If

Next count2

EmailAddress = Right(EmailAddress, Len(EmailAddress) - 1)

End If

subject = Cells(count, 5)

ccstring = Cells(count, 2)

'MsgBox EmailAddress

'GoTo skip

Set objMessage = objOutlook.CreateItem(olMailItem)

With objMessage

.To = EmailAddress

.CC = ccstring

.subject = subject

.body = body

.Display



End With

Set objOutlook = Nothing

Set objMessage = Nothing

End If

skip:



Next count

Application.ScreenUpdating = True

MsgBox "emails have been sent."

Exit Sub

errorcatch:

MsgBox Err.Description


End Sub

Attachments

See less See more
Is there more than 1 Project?
If so will there be a sheet per project, or will the projects be on the same sheet?

I have to code working now when the Workbook is opened.

But there is a slight problem, if you use the Task Scheduler to open and Close the Workbook you can never change anything on it.
To overcome that problem you would either need to leave the workbook open or need another workbook that this one checks and which you would set a cell value that would either run the Macro or not.

ps it has sent you a couple of emails.
I have gone ahead with the separate Workbook route and it works OK.
See attached Workbooks.
The one called MacroTest controls whether the macro in the Loan workbook runs or not.

Attachments

Thank you so much for your help! This is awesome and I owe you. Maybe I could send you some beer money for your troubles.
Chet, that is not necessary, this is my retirement hobby.
Glad to have helped.
1 - 14 of 14 Posts
Status
Not open for further replies.
Top