VBA Calculate Event Issue

Vigash

New Member
Joined
Aug 26, 2022
Messages
21
Office Version
  1. 2013
Platform
  1. Windows
Hi,

I am trying to call macro If i run reorder mail macro it's working fine as I expected. But when I tried to call the same macro using worksheet calculate it executed macro in a different manner. It triggers mail for all the values irrespective of IF condition.

Kindly someone helps me on this.. Thanks in advance.

VBA Code:
Private Sub Worksheet_Calculate()
  Call Module2.Reorder_mail
End Sub


Sub Reorder_mail()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Replenishment")
Dim i,j  As Integer
Dim OA As Object
Dim msg As Object

Set OA = CreateObject("outlook.application")

For i = 6 To 64

    For j = 5 To 17
      If Cells(i, j).Value > 0 Then
  
           Set msg = OA.CreateItem(0)
           msg.To = "[EMAIL]XXXX@gmail.com[/EMAIL]"
           'msg.CC = sh.Range("B" & i).Value
           msg.Subject = "Reorder for" & " " & sh.Range("B" & i).Value & "_" & sh.Cells(5, j).Value & "_" & _
                        sh.Cells(3, j).Value
           msg.Body = "Hi Team," & vbNewLine & vbNewLine & _
                "This is to inform you that " & "Item no : " & sh.Cells(5, j) & " " & "is going to be out of stock soon." & vbNewLine & vbNewLine & _
                "So Kindly order the below mentioned items for the following location." & vbNewLine & vbNewLine & _
                "Hub Name            : " & sh.Range("B" & i).Value & vbNewLine & _
                "Item no                  : " & sh.Cells(5, j).Value & vbNewLine & _
                "Item Description : " & sh.Cells(3, j).Value & vbNewLine & _
                "Order Qty              : " & sh.Cells(i, j).Value & vbNewLine & vbNewLine & _
                "Kindly Check." & vbNewLine & vbNewLine & _
                "Regards," & vbNewLine & _
                "Auto Generated Mail"

           msg.Display
               Application.Wait (Now + TimeValue("0:00:01"))
               Application.SendKeys "%s"
       
           
 
      End If
      Next j
Next i


End Sub
 
Last edited by a moderator:

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
If Cells(i, j).Value > 0 Then refers to cells on whatever the active sheet is at the time the code runs. You should specify which sheet you want it to look at.
 
Upvote 0
Solution
Hi Rory,

I have changed the code to if sh.cells(i,j) > 0 .. It's working fine thank you so much......
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,746
Members
448,989
Latest member
mariah3

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