Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50199

How to make this worksheet action work with the CASE statement

$
0
0
i have the following routine that works, now i would like to expand it's capabilities to utilize the CASE statement

Code:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim AWorksheet As Worksheet
    Dim KeyCells As Range, Sendrng As Range, rng As Range
    Dim subject As String, NewString As String, SelectedRow As String, ApprovedReq As String
   
    Application.DisplayAlerts = False
   
    ' The variable KeyCells contains the cells that will
    ' cause an alert when they are changed.
    Set KeyCells = Range("P15:P94")
   
    If Not Application.Intersect(KeyCells, Range(Target.Address)) _
          Is Nothing Then

        ' Display a message when one of the designated cells has been changed to "Approved".
        If Range(Target.Address) = "Approved" Then
            'MsgBox "Cell " & Target.Address & " has changed. Email Dan"
            SelectedRow = Right(WorksheetFunction.Substitute(Target.Address, "$", ""), 2)
            NewString = "B" & SelectedRow & ":N" & SelectedRow
            ApprovedReq = "O" & SelectedRow
         
            On Error GoTo StopMacro
           
            With Application
            .ScreenUpdating = False
            .EnableEvents = False
            End With
           
            'Note: if you use one cell it will send the whole worksheet

            Set Sendrng = Worksheets("Summary").Range(NewString)
            subject = "Purchase Requisition " & Range(ApprovedReq) & " has been Approved"
           
            Set AWorksheet = ActiveSheet
           
            With Sendrng
           
                .Parent.Select
               
                Set rng = ActiveCell
               
                .Select
               
                ' Create the mail and send it
                ActiveWorkbook.EnvelopeVisible = True
                With .Parent.MailEnvelope
               
                    ' Set the optional introduction field thats adds
                    ' some header text to the email body.
                    .Introduction = "This requisition has been Approved"
               
                    With .Item
                        .To = "danmcg@somewhere.com"
                        .CC = ""
                        .BCC = ""
                        .subject = subject
                        .Send
                    End With
               
                End With
               
                'select the original ActiveCell
                rng.Select
            End With
           
            'Activate the sheet that was active before you run the macro
            AWorksheet.Select

StopMacro:
            With Application
            .ScreenUpdating = True
            .EnableEvents = True
            End With
            ActiveWorkbook.EnvelopeVisible = False

        End If
    End If
    Application.DisplayAlerts = True
End Sub

here is what i tried and is not working (but the "Approved" string works, just not the other cases)

Code:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim AWorksheet As Worksheet
    Dim KeyCells As Range, Sendrng As Range, rng As Range
    Dim subject As String, NewString As String, SelectedRow As String, ApprovedReq As String
   
    Application.DisplayAlerts = False
   
    ' The variable KeyCells contains the cells that will
    ' cause an alert when they are changed.
    Set KeyCells = Range("P15:P94")
   
    If Not Application.Intersect(KeyCells, Range(Target.Address)) _
          Is Nothing Then

        ' Display a message when one of the designated cells has been changed to "Approved".
        'If Range(Target.Address) = "Approved" Then
        Select Case Target.Address
            Case Is = "Approved"
                'MsgBox "Cell " & Target.Address & " has changed. Email Dan"
                SelectedRow = Right(WorksheetFunction.Substitute(Target.Address, "$", ""), 2)
                NewString = "B" & SelectedRow & ":N" & SelectedRow
                ApprovedReq = "O" & SelectedRow
             
                'On Error GoTo StopMacro
               
                With Application
                    .ScreenUpdating = False
                    .EnableEvents = False
                End With
               
                'Note: if you use one cell it will send the whole worksheet
   
                Set Sendrng = Worksheets("Summary").Range(NewString)
                subject = "Purchase Requisition " & Range(ApprovedReq) & " has been Approved"
               
                Set AWorksheet = ActiveSheet
               
                With Sendrng
               
                    .Parent.Select
                   
                    Set rng = ActiveCell
                   
                    .Select
                   
                    ' Create the mail and send it
                    ActiveWorkbook.EnvelopeVisible = True
                    With .Parent.MailEnvelope
                   
                        ' Set the optional introduction field thats adds
                        ' some header text to the email body.
                        .Introduction = "This requisition has been Approved"
                   
                        With .Item
                            .To = "danmcg@somewhere.com"
                            .CC = ""
                            .BCC = ""
                            .subject = subject
                            .Send
                        End With
                   
                    End With
                   
                    'select the original ActiveCell
                    rng.Select
                End With
               
                'Activate the sheet that was active before you run the macro
                AWorksheet.Select
   

            Case Is = "Written"
                MsgBox "Req Form Written"
            Case Is = "Hold"
                MsgBox "Req Form on Hold"
            Case Is = "No"
                MsgBox "Req Form not Written"
            Case Else
                MsgBox "CaseElse"
        'End If
StopMacro:
                With Application
                MsgBox "At StopMacro"
                .ScreenUpdating = True
                .EnableEvents = True
                End With
                ActiveWorkbook.EnvelopeVisible = False
        End Select
    End If
    Application.DisplayAlerts = True
End Sub

thoughts?

Viewing all articles
Browse latest Browse all 50199

Trending Articles