将两个Private Sub Worksheet_Change(ByVal Target As Range)组合在一起

时间:2017-12-27 22:59:24

标签: excel vba

我需要合并两个Private Sub Worksheet_Change(ByVal Target As Range) 我是Excel VBA代码的新手,我该怎么做?代码如下。

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range, r As Range, rv As Long
    Set rng = Intersect(Target, Range("C77:AD81"))
    If rng Is Nothing Then Exit Sub
    For Each r In rng
        rv = r.Value
        'Peak Flow Doctor Warning and Weight Gain Warning
        If rv = 180 Then
            MsgBox "''PEAK FLOW CRITICAL AT 180L/MIN''" & vbCrLf & "''PREDNISONE PROBABLY REQUIRED''" & vbCrLf & "''MAKE DOCTOR'S APPOINTMENTS ASAP''", vbInformation, "WARNING"
        End If
        If rv = 120 Then
            MsgBox "''PEAK FLOW CRITICAL AT 120L/MIN''" & vbCrLf & "''MAKE URGENT DOCTOR'S APPOINTMENTS''" & vbCrLf & "''OR GO TO A&E IMMEDIATELY''", vbInformation, "CRITICAL WARNING"
        End If
        If rv >= 450 Then
            MsgBox "''CHECK OR TEST PEAK FLOW METER''" & vbCrLf & "''IT MAY BE FAULTY AND GIVING FALSE HIGH's''", vbInformation, "WARNING"
        End If


Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range, r As Range, rv As Long
    Set rng = Intersect(Target, Range("C93:AD93"))
    If rng Is Nothing Then Exit Sub
    For Each r In rng
        'Weight Gain Warning
        rv = r.Value
        If rv = 90 Then
            MsgBox "''LIKELY TO EXACERBATE COPD SYMPTOMS''" & vbCrLf & "''CHRONIC ASTHMA OR EMPHYSEMA PROBABLE''", vbCritical, "WARNING"
        End If
        If rv = 95 Then
            MsgBox "''IF SWELLING IN ANKLES PROBABLE FLUID RETENTION''" & vbCrLf & "''POSSIBILITY OF HEART FAILURE IF UNATTENDED''", vbCritical, "CRITICAL WARNING"
        End If
      Next r
End Sub

1 个答案:

答案 0 :(得分:1)

以下代码是您当前的2个Worksheet_Change事件的合并。

您也可以使用Select Case来清理代码。

代码

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rng As Range, r As Range, rv As Long

    If Not Intersect(Target, Range("C77:AD81")) Is Nothing Then
        Set rng = Intersect(Target, Range("C77:AD81"))
        For Each r In rng
            'Peak Flow Doctor Warning and Weight Gain Warning
            Select Case r.Value
                Case 180
                    MsgBox "''PEAK FLOW CRITICAL AT 180L/MIN''" & vbCrLf & "''PREDNISONE PROBABLY REQUIRED''" & vbCrLf & "''MAKE DOCTOR'S APPOINTMENTS ASAP''", vbInformation, "WARNING"    
                Case 120
                    MsgBox "''PEAK FLOW CRITICAL AT 120L/MIN''" & vbCrLf & "''MAKE URGENT DOCTOR'S APPOINTMENTS''" & vbCrLf & "''OR GO TO A&E IMMEDIATELY''", vbInformation, "CRITICAL WARNING"
                Case Is >= 450
                    MsgBox "''CHECK OR TEST PEAK FLOW METER''" & vbCrLf & "''IT MAY BE FAULTY AND GIVING FALSE HIGH's''", vbInformation, "WARNING"
            End Select
        Next r
    End If

    If Not Intersect(Target, Range("C93:AD93")) Is Nothing Then
        Set rng = Intersect(Target, Range("C93:AD93"))
        For Each r In rng
            'Weight Gain Warning
            Select Case r.Value
                Case 90
                    MsgBox "''LIKELY TO EXACERBATE COPD SYMPTOMS''" & vbCrLf & "''CHRONIC ASTHMA OR EMPHYSEMA PROBABLE''", vbCritical, "WARNING"
                Case 95
                    MsgBox "''IF SWELLING IN ANKLES PROBABLE FLUID RETENTION''" & vbCrLf & "''POSSIBILITY OF HEART FAILURE IF UNATTENDED''", vbCritical, "CRITICAL WARNING"
            End Select
        Next r
    End If

End Sub