Combine VBA declaration and calculation in One

FGaxha

Board Regular
Joined
Jan 10, 2023
Messages
221
Office Version
  1. 365
Platform
  1. Windows
I have VBA codes: I'm looking to Call macro only when tow condition are meet: D5<0 and Closing Workbook, then SendEmail. It is working but Im receiving one email when condition D5 <0 and other email when I close workbook: Please help
Sheet2:
Private Sub Worksheet_Calculate()
If IsNumeric(Range("D5")) Then
If Range("D5").Value < 0 Then
'MsgBox "Range D5 has reached is limit of <=0", vbInformation
Call SendEmail

End If
End If

End Sub

Workbook:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Save
'MsgBox "This Workbook is saved."
Call SendEmail
End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
In order to know if the mail has already been sent and not send it again when you close the book, we need an available cell.
In the example I put the cell Z5, change that cell for a cell that you have available.

Try this:

VBA Code:
Private Sub Worksheet_Calculate()
  If IsNumeric(Range("D5")) Then
    If Range("D5").Value < 0 Then
      'MsgBox "Range D5 has reached is limit of <=0", vbInformation
      Call SendEmail
      
      'In an available cell
      Range("Z5").Value = "sent"
    End If
  End If
End Sub

VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
  ThisWorkbook.Save
  'MsgBox "This Workbook is saved."
  If Range("D5").Value < 0 And Range("Z5").Value = "" Then
    Call SendEmail
  End If
End Sub

In the same panel where you have the BeforeClose event add the following code:

VBA Code:
Private Sub Workbook_Open()
  'In an available cell
  Range("Z5").Value = ""
End Sub
 
Upvote 1
Sir, I appreciate your help:

I applied in the workbook your Code:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Save
'MsgBox "This Workbook is saved."
If Range("D5").Value < 0 And Range("Z5").Value = "" Then
Call SendEmail
End If
End Sub
Private Sub Workbook_Open()
'In an available cell
Range("Z5").Value = ""
End Sub
 
Upvote 0
Sir is not working:

If i include this code on worksheet2 it send me twice email: when value D5 <0 and one when close workbook.

Private Sub Worksheet_Calculate()
If IsNumeric(Range("D5")) Then
If Range("D5").Value < 0 Then
'MsgBox "Range D5 has reached is limit of <=0", vbInformation
Call SendEmail

'In an available cell
Range("Z5").Value = "sent"
End If
End If
End Sub
 
Upvote 0
You must refer to the sheet, I don't know the name of your sheet, but I'm going to assume it's "Sheet2", so I'll give you the code but you must adjust the name of your sheet:

Rich (BB code):
Private Sub Worksheet_Calculate()
  If IsNumeric(Sheets("Sheet2").Range("D5")) Then
    If Sheets("Sheet2").Range("D5").Value < 0 Then
      'MsgBox "Range D5 has reached is limit of <=0", vbInformation
      Call SendEmail
      
      'In an available cell
      Sheets("Sheet2").Range("Z5").Value = "sent"
    End If
  End If
End Sub
Rich (BB code):
Private Sub Workbook_BeforeClose(Cancel As Boolean)
  ThisWorkbook.Save
  'MsgBox "This Workbook is saved."
  If Sheets("Sheet2").Range("D5").Value < 0 And Sheets("Sheet2").Range("Z5").Value = "" Then
    Call SendEmail
  End If
End Sub

Private Sub Workbook_Open()
  'In an available cell
  Sheets("Sheet2").Range("Z5").Value = ""
End Sub
 
Upvote 1

Forum statistics

Threads
1,214,586
Messages
6,120,402
Members
448,958
Latest member
Hat4Life

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