VBA Auto mail problem

criper

New Member
Joined
Jan 12, 2017
Messages
12
Hi,

I have a mail macro that automatically sends email based on a cell value in Excel.
So what this is supposed to do is auto mail when cell C2 is larger than 0. This is working as it should.

The problem here is that the values are gathered with formulas, "=COUNTIF(Utleveranser.Data!B:B, 1)". So the data in cell C2 isn't inserted manually.
When the data is inserted manually, the automail works. But if the value gets inserted through the formula, the mail function doesn't work.

Any ideas? :confused:

Code:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Not Application.Intersect(Range("C2"), Target) Is Nothing Then
If IsNumeric(Target.Value) And Target.Value > 0 Then
Call Indiska
End If
End If

Public Sub Indiska()
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

strbody = "Indiska"

On Error Resume Next
With OutMail
.To = ""
.CC = ""
.BCC = ""
.Subject = "Subject"
.Body = strbody
.Send
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hi.

There's Something I don't get. You have a formula in C2 which does your calculation, but you want the Worksheet_Change module to trigger when C2 is changed ?

The Worksheet_Change module will only trigger when the user manually modifies a cell in the worksheet

Anyways try to add :

Code:
Target.Worksheet.Calculate
before

Code:
Call Indiska
 
Last edited:
Upvote 0
The Worksheet_Change module will only trigger when the user manually modifies a cell in the worksheet

I would recommend placing this in the "Utleveranser.Data" worksheet module :


Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Target.Column <> 2 Or Target.Columns.Count > 1 Then Exit Sub
    
    If WorksheetFunction.CountIf(Target.Worksheet.Range("B:B"), 1) > 0 Then Call Indiska
    
End Sub
 
Upvote 0
Hi louisH,

What I want is that the automail works although the data is inserted with a formula, not manually. Is that possible?

This is because I have a database connected to Excel, and the automail function needs to work based on values 0 or 1, which represents entries or no entries in the database.

Example:

I have some outgoing transactions for a customer stored in the database, then the value in C2 will be "1", and an automail is mailed to one of my employees.
When the transaction is done, it is no longer stored in the database, then the value will become "0".
 
Upvote 0
Hi again louisH,

It got solved using Worksheet_Calculate().

But not multiple mail gets sent, I'll try to figure out why.

Thanks for your help!
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
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