删除删除多个单元格时发生运行时错误13

时间:2017-12-26 22:34:57

标签: excel excel-vba vba

我是Excel中的VBA新手。我已经编写了下面的代码,该代码工作正常,但是当我尝试删除多个单元格时,会发生运行时错误13。我已经检查了其他搜索问题,但无法弄清楚。

我的代码是:

Private Sub Worksheet_Change(ByVal Target As Range)

'Peak Flow 120 and 180 Doctor Warning
If Intersect(Target, Range("C77:AD81")) = 180 Then
    MsgBox "''PEAK FLOW CRITICAL AT 180L/MIN''" & vbCrLf & "''PREDNISONE PROBABLY REQUIRED''" & vbCrLf & "''MAKE DOCTOR'S APPOINTMENTS ASAP''", vbInformation, "WARNING"
End If
If Intersect(Target, Range("C77:AD81")) = 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 Intersect(Target, Range("C77:AD81")) >= 450 Then
    MsgBox "''CHECK OR TEST PEAK FLOW METER''" & vbCrLf & "''IT MAY BE FAULTY AND GIVING FALSE HIGH's''", vbInformation, "WARNING"
End If
End Sub

1 个答案:

答案 0 :(得分:0)

尝试使用此代码:

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 120 and 180 Doctor 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
    Next r
End Sub