简体   繁体   中英

How to clear cell when enter invalid data in Excel using VBA

I have an Excel sheet in which I am accepting value from the user when user enter a value a VBA will run which check the data is valid or not and if the data is not valid it will prompt a message saying invalid data. Here is my code:

    Private Sub Worksheet_Change(ByVal Target As Range)
 If Target.Address = "$D$12" Or Target.Address = "$D$13" Or Target.Address = "$D$14" Or Target.Address = "$D$15" Then
Call Room
End If
End Sub 

Room method is

Sub Room()
Dim lastRow As Long
If IsNumeric(Range("i17")) Then
If [I17] < 0 Then
 MsgBox "msg "
End If
End If
End Sub

In I17 cell I have a formula

=C6-(D12+(2*D13) + (2*D14) + (3*D15))

My problem is when wrong data is enter in any of the cells (D12, D13, D14, D15) then the cell should be clear automatically after showing prompt message.

How can this be done?

The first thing that you should do is clean up how you check what Target is. It could be multiple cells (Fill Down, paste a range, ...). This is accomplished by intersecting Target with the range you are interested in, and We'll store into a range variable, for later. If there is no overlap, then intersect will return an empty object, which we can test for with is Nothing .

The next thing to note is that odd things (infinite recursion) can happen if we allow the Worksheet_Change event to fire by changing a cell. To prevent this, we will turn off events before calling Room , and turn it back on after we're done.

Next we pass the range that has changed into room, so we can modify it from within that subroutine.

And, finally we modify the affected range after displaying the message. Note that I have used a command to literally clear the cell. Since you are performing calculations based on that data, you might prefer to set it to default value, like 0, using a.value = 0 instead.

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim a As Range

    Set a = Intersect(Target, Range("D12:D15"))

    If Not a Is Nothing Then
        Application.EnableEvents = False
        Room a
        Application.EnableEvents = True
    End If
End Sub

Sub Room(a As Range)
    Dim lastRow As Long
    If IsNumeric(Range("I17")) Then
        If Range("I17").Value < 0 Then
            MsgBox "msg "
            a.ClearContents
        End If
    End If
End Sub

As a side note, I have a used a bad variable name a , since I don't know what that range represents. You should pick something that describes to future maintainers what is going on.

use this

Private Sub Worksheet_Change(ByVal Target As Range)
Dim t As Range
Set t = Intersect(Target, [D12:D15])
Application.EnableEvents = 0
If Not t Is Nothing Then
     Call Room
     If [I17] < 0 Then Target.Value = ""
End If
Application.EnableEvents = 1
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM