Running subroutines based on formula value

Aidan1000

New Member
Joined
Jun 27, 2018
Messages
1
Hi, apologies Im a little new to this but I have a spreadsheet that is intended to automatically send an email based on the result of a formula. The code I have in ThisWorksheet for sending the email is:

Code:
Sub Test1()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim cell As Range

    Application.ScreenUpdating = False
    Set OutApp = CreateObject("Outlook.Application")

    On Error GoTo cleanup
    For Each cell In Columns("AH").Cells.SpecialCells(xlCellTypeConstants)
        If cell.Value Like "?*@?*.?*" And _
           LCase(Cells(cell.Row, "AG").Value) = "yes" _
           And LCase(Cells(cell.Row, "AJ").Value) <> "yes" Then

            Set OutMail = OutApp.CreateItem(0)

            On Error Resume Next
            With OutMail
                .To = cell.Value
                .Subject = "Reminder"
                .Body = "Dear " & Cells(cell.Row, "J").Value _
                      & vbNewLine & vbNewLine & _
                        "Action. " & Cells(cell.Row, "AF").Value

                .Send
            End With
            On Error GoTo 0
            Cells(cell.Row, "AJ").Value = "yes"
            Cells(cell.Row, "AK").Value = Date
            Set OutMail = Nothing
        End If
    Next cell

 cleanup:
    Set OutApp = Nothing
    Application.ScreenUpdating = True
 End Sub

 Private Sub Worksheet_Calculate()
   Worksheet_Change Range("AG:AG")
 End Sub

And in the sheet monitoring the value I have put this code:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("AG1:AG100")) Is Nothing Then
    Call ThisWorkbook.Test1
    End If
 End Sub
Any help appreciated how I can get this to work automatically. Thanks
 
Last edited by a moderator:

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Cross posted https://www.excelforum.com/excel-pr...35822-running-subroutines-from-userforms.html

While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules).
This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0
Welcome to the forum. :)

First off, I can't see any reason for the Test1 routine to be in the ThisWorkbook module. It really belongs in a normal module.

But anyway, you can simply add:

Code:
Thisworkbook.Test1

to whatever code you want to use to send the email - e.g. your Save and Close button code.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

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