VBA code to send email when multiple items fall below minimum values

raspinwall

New Member
Joined
Dec 30, 2020
Messages
1
Office Version
  1. 365
Platform
  1. Windows
  2. Web
Hello,

I would like to send a single email that contains all items in a spreadsheet that have fallen below minimum quantity values.
In the body of the email, I need to include each low items Manufacturer, Part # and Description which are each individual cells.

E-mail body:

Hello,

The following items have fallen below minimum quantities:

Row 2, Widget Co, ABC123, Widget Standard
Row 3 Widget Co, DEF456, Widget Pro

Note, in the code, there are different minimum values for different ranges.

Spreadsheet looks like this:

QuantityMfgrPart#Description
0Widget CoABC123Widget Standard
0Widget CoDEF456Widget Pro

Code I have so far looks like this:

VBA Code:
'Need to be in the sheet code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Call notify
End Sub

Sub notify()
    Dim rng As Range
    For Each rng In Range("B6:B11,B13:B17")
       If (rng.Value < 4) Then
           Call mymacro(rng.Address)
       End If
    Next rng
    For Each rng In Range("B99:B116")
       If (rng.Value < 1) Then
           Call mymacro(rng.Address)
       End If
    Next rng
End Sub



Private Sub mymacro(theValue As String)
    Dim xOutApp As Object
    Dim xOutMail As Object
    Dim xMailBody As String
    Set xOutApp = CreateObject("Outlook.Application")
    Set xOutMail = xOutApp.CreateItem(0)
    xMailBody = "Hello," & vbNewLine & vbNewLine & _
              "The following inventory items have fallen below minimum stock quantities: " & theValue
    On Error Resume Next
    With xOutMail
        .To = "user@test.com"
        .CC = ""
        .BCC = ""
        .Subject = "Low warehouse stock alert"
        .Body = xMailBody
        .Display   'or use .Send
    End With
    On Error GoTo 0
    Set xOutMail = Nothing
    Set xOutApp = Nothing
End Sub

Thanks for all of the help with this!!!!

Russ
 

Some videos you may like

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.

Watch MrExcel Video

Forum statistics

Threads
1,123,265
Messages
5,600,603
Members
414,393
Latest member
Vignesh Mechz

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
Top