How to combine similar Private Sub Worksheets?

Rita9091

New Member
Joined
Jan 11, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello all,

I am trying to figure out how to combine two very similar Private Sub Worksheets. Ideally....they would run one after the other, and create different emails (one to staff and one to client). What it does is look at my worksheet, and if the value in the formula cell is > 0...it launches an email macro. The second, is identical, except it checks a different cell, and launches a different email macro. I also need to add something in that if the cell queried is "0" then do nothing...or don't send anything, but I'm not sure how to integrate that.

Here is the first code:
VBA Code:
Private Sub Worksheet_Calculate()
    Dim FormulaRange As Range
    Dim NotSentMsg As String
    Dim MyMsg As String
    Dim SentMsg As String
    Dim MyLimit As Double

    NotSentMsg = "Not Sent"
    SentMsg = "Sent"

    'Above the MyLimit value it will run the macro
    MyLimit = 0

    'Set the range with the Formula that you want to check
    Set FormulaRange = Me.Range("Q101")

    On Error GoTo EndMacro:
    For Each FormulaCell In FormulaRange.Cells
        With FormulaCell
            If IsNumeric(.Value) = False Then
                MyMsg = "Not numeric"
            Else
                If .Value > MyLimit Then
                    MyMsg = SentMsg
                    If .Offset(0, 1).Value = NotSentMsg Then
                        Call JC_Mail
                    End If
                Else
                    MyMsg = NotSentMsg
                End If
            End If
            Application.EnableEvents = False
            .Offset(0, 1).Value = MyMsg
            Application.EnableEvents = True
        End With
    Next FormulaCell

ExitMacro:
    Exit Sub

EndMacro:
    Application.EnableEvents = True

    MsgBox "Some Error occurred." _
         & vbLf & Err.Number _
         & vbLf & Err.Description

End Sub


And here is the second.

VBA Code:
Private Sub Worksheet_Calculate()
    Dim FormulaRange As Range
    Dim NotSentMsg As String
    Dim MyMsg As String
    Dim SentMsg As String
    Dim MyLimit As Double

    NotSentMsg = "Not Sent"
    SentMsg = "Sent"

    'Above the MyLimit value it will run the macro
    MyLimit = 0

    'Set the range with the Formula that you want to check
    Set FormulaRange = Me.Range("R101")

    On Error GoTo EndMacro:
    For Each FormulaCell In FormulaRange.Cells
        With FormulaCell
            If IsNumeric(.Value) = False Then
                MyMsg = "Not numeric"
            Else
                If .Value > MyLimit Then
                    MyMsg = SentMsg
                    If .Offset(0, 1).Value = NotSentMsg Then
                        Call JC_Mail2
                    End If
                Else
                    MyMsg = NotSentMsg
                End If
            End If
            Application.EnableEvents = False
            .Offset(0, 1).Value = MyMsg
            Application.EnableEvents = True
        End With
    Next FormulaCell

ExitMacro:
    Exit Sub

EndMacro:
    Application.EnableEvents = True

    MsgBox "Some Error occurred." _
         & vbLf & Err.Number _
         & vbLf & Err.Description

End Sub

Can anyone help,
Thank you
RC
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Try This:
VBA Code:
Private Sub Worksheet_Calculate()
Dim FormulaRange1 As Range
Dim FormulaRange2 As Range
Dim NotSentMsg As String
Dim MyMsg As String
Dim SentMsg As String
Dim MyLimit As Double
NotSentMsg = "Not Sent"
SentMsg = "Sent"

'Above the MyLimit value it will run the macro
MyLimit = 0

'Set the range with the Formula that you want to check
Set FormulaRange1 = Me.Range("Q101")
Set FormulaRange2 = Me.Range("R101")

On Error GoTo EndMacro:
For Each FormulaCell In FormulaRange1.Cells
    With FormulaCell
        If IsNumeric(.Value) = False Then
            MyMsg = "Not numeric"
        Else
            If .Value > MyLimit Then
                MyMsg = SentMsg
                If .Offset(0, 1).Value = NotSentMsg Then
                    Call JC_Mail
                End If
            Else
                MyMsg = NotSentMsg
            End If
        End If
        Application.EnableEvents = False
        .Offset(0, 1).Value = MyMsg
        Application.EnableEvents = True
    End With
Next FormulaCell

For Each FormulaCell In FormulaRange2.Cells
    With FormulaCell
        If IsNumeric(.Value) = False Then
            MyMsg = "Not numeric"
        Else
            If .Value > MyLimit Then
                MyMsg = SentMsg
                If .Offset(0, 1).Value = NotSentMsg Then
                    Call JC_Mail2
                End If
            Else
                MyMsg = NotSentMsg
            End If
        End If
        Application.EnableEvents = False
        .Offset(0, 1).Value = MyMsg
        Application.EnableEvents = True
    End With
Next FormulaCell
ExitMacro:
Exit Sub

EndMacro:
Application.EnableEvents = True
MsgBox "Some Error occurred." _
     & vbLf & Err.Number _
     & vbLf & Err.Description
End Sub
 
Upvote 0
Hi Rita,

as the ranges to compare are directly connected to each other I changed the offset to put the mesaage to to be one row below instead overwriting an existing formula:

VBA Code:
Private Sub Worksheet_Calculate()
  Dim FormulaRange As Range
  Dim FormulaCell As Range
  Dim NotSentMsg As String
  Dim MyMsg As String
  Dim SentMsg As String
  Dim MyLimit As Double

  NotSentMsg = "Not Sent"
  SentMsg = "Sent"

  'Above the MyLimit value it will run the macro
  MyLimit = 0

  'Set the range with the Formula that you want to check
  Set FormulaRange = Range("Q101,R101")

  On Error GoTo EndMacro:
  For Each FormulaCell In FormulaRange.Cells
    With FormulaCell
      If IsNumeric(.Value) = False Then
        MyMsg = "Not numeric"
      Else
        If .Value > MyLimit Then
          MyMsg = SentMsg
          If .Offset(1, 0).Value <> NotSentMsg Then
            If .Address = "$Q$101" Then
              Call JC_Mail
            ElseIf .Address = "$R$101" Then
              Call JC_Mail2
            End If
          End If
        Else
          MyMsg = NotSentMsg  'I'd preferred to use "" instead: why not sent when value doesn't fit criteria
        End If
      End If
      Application.EnableEvents = False
      .Offset(1, 0).Value = MyMsg
      Application.EnableEvents = True
    End With
  Next FormulaCell

ExitMacro:
  Exit Sub

EndMacro:
  Application.EnableEvents = True

  MsgBox "Some Error occurred." _
       & vbLf & Err.Number _
       & vbLf & Err.Description

End Sub

Ciao,
Holger
 
Upvote 0
Solution
Thank you both very much! Holger, I went with your code as your solution to move the notification text was very helpful.
 
Upvote 0

Forum statistics

Threads
1,215,551
Messages
6,125,478
Members
449,233
Latest member
Deardevil

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