简体   繁体   English

使用VBA在Excel中输入无效数据时如何清除单元格

[英]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. 我有一个Excel工作表,当用户输入一个值时,我将在其中接受用户的值,VBA将运行,以检查数据是否有效,如果数据无效,则会提示消息,指出数据无效。 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 在I17单元格中,我有一个公式

=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. 我的问题是,在任何单元格(D12,D13,D14,D15)中输入错误数据时,显示提示消息后应自动清除该单元格。

How can this be done? 如何才能做到这一点?

The first thing that you should do is clean up how you check what Target is. 您应该做的第一件事是清理如何检查Target是什么。 It could be multiple cells (Fill Down, paste a range, ...). 它可能是多个单元格(Fill Down,粘贴一个范围,...)。 This is accomplished by intersecting Target with the range you are interested in, and We'll store into a range variable, for later. 这是通过将Target与您感兴趣的范围相交来完成的,我们将存储到范围变量中,以备后用。 If there is no overlap, then intersect will return an empty object, which we can test for with is Nothing . 如果没有重叠,则相交将返回一个空对象,我们可以使用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. 接下来要注意的是,如果我们允许通过更改单元格触发Worksheet_Change事件来触发奇怪的事情(无限递归)。 To prevent this, we will turn off events before calling Room , and turn it back on after we're done. 为避免这种情况,我们将在调用Room之前关闭事件,并在完成后将其重新打开。

Next we pass the range that has changed into room, so we can modify it from within that subroutine. 接下来,我们传递已更改为room的范围,因此我们可以在该子例程中对其进行修改。

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. 由于您是基于该数据执行计算的,因此您可能更喜欢使用a.value = 0将其设置为默认值(例如0)。

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. 附带说明一下,我使用了一个错误的变量名a ,因为我不知道该范围代表什么。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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