Results 1 to 27 of 27

How to make this worksheet action work with the CASE statement

Threaded View

  1. #1
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    How to make this worksheet action work with the CASE statement

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

    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 = "[email protected]"
                            .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)

    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 = "[email protected]"
                                .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?
    Last edited by dmcgov; 08-01-2017 at 01:32 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Trying to make this if statement work
    By Orginlock in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-16-2016, 03:58 AM
  2. [SOLVED] i have an if then statement that works, how to make it work as a with or case statement
    By dmcgov in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 06-03-2016, 03:34 PM
  3. How to make this SumIf statement work
    By Rheanna in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 10-29-2014, 01:22 PM
  4. [SOLVED] IF statement I cant make work
    By DrGnBld in forum Excel Formulas & Functions
    Replies: 17
    Last Post: 06-19-2014, 01:18 AM
  5. Anyone actually get Case statement to work ... ?
    By ForestFeeder in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-21-2006, 11:15 AM
  6. Anyone actually get Case statement to work ... ?
    By ForestFeeder in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-21-2006, 10:50 AM
  7. [SOLVED] Select case statement - it will not work and i dont understand why! :o(
    By Thomas in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-26-2005, 01:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1