简体   繁体   English

如果单元格的值超过另一个单元格的值,则显示消息框弹出窗口的VBA代码

[英]VBA code to show Message Box popup if the value of a cell exceeds the value of another

I have a scenario in Excel which requires some vba code. 我在Excel中有一个场景,需要一些vba代码。 I'm a relative newbie and have reached a dead end in trying to find a solution. 我是一个相对新手,并试图找到一个解决方案达到了死胡同。

Say for instance, a user inputs a numerical value in cell A1. 比如说,用户在单元格A1中输入数值。

They must then also enter values in cells A5,A6,A7 and A8. 然后,他们还必须在单元格A5,A6,A7和A8中输入值。

The total of this sum is displayed in cell A9, using a generic excel SUM function. 使用通用Excel Excel函数在单元格A9中显示此总和的总和。

None of the cells A5:A8 can be left blank, although a zero ('0') input is acceptable. 尽管零('0')输入是可接受的,但是单元格A5:A8都不能留空。

The value of A9 can be less than, equal to, but not exceed the value in A1. A9的值可以小于,等于但不超过A1中的值。

If A9 exceeds A1, an error message must pop up to alert them that this is the case. 如果A9超过A1,则必须弹出一条错误消息,提醒他们是这种情况。

Alpha characters cannot be input. 无法输入字母字符。 An error message pops up to alert them if they are. 弹出错误消息以提醒他们。

Numbers input must range between 0 and 9,999,999. 数字输入必须介于0到9,999,999之间。 An error message pops up to alert them if they don't. 如果没有,则会弹出一条错误消息以提醒他们。

I was given a piece of vba code (below) which I use for a similar purpose which works really well. 我得到了一个vba代码(下面),我用它来达到类似的目的,效果非常好。 I cannot however, figure out how to incorporate the code which will identify and return an error message if the value in A9 exceeds A1.This what I tried to do, but I know it's wrong! 但是,我无法弄清楚如果A9中的值超过A1,如何合并将识别并返回错误消息的代码。这是我尝试做的,但我知道这是错的! The code is as follows: 代码如下:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
If Not Intersect(Target, Range("A9")) Is Nothing Then
    Application.EnableEvents = False
    For Each c In Intersect(Target, Range("A9"))
        If IsEmpty(c) Then
            Application.EnableEvents = True
            Exit Sub
        End If
        If Not VarType(c.Value2) = vbDouble Or c.Value < 0 Or c.Value > 9999999 Then
            MsgBox "Entry in cell " & c.Address(0, 0) & " must be a number from 0 and 9,999,999"
            Application.Undo
        ElseIf WorksheetFunction.Count(Range("A9")) = 1 And _
            WorksheetFunction.Sum(Range("A9")) > Range("A1").Value Then
            MsgBox "The sum of A99 cannot exceed A1 when all entries are completed"
            Application.Undo
        End If

If anyone could help me with this, it would be very much appreciated!! 如果有人能帮助我,我将非常感谢!

Cal 卡尔

It seems to me that your Worksheet_Change event macro should be dealing with changes to A1 and A5:A8, not A9. 在我看来,你的Worksheet_Change事件宏应该处理A1和A5的变化:A8,而不是A9。 If the values in A5:A8 meet criteria, you can then check their total against A1. 如果A5:A8中的值符合条件,则可以检查它们的总数与A1。

After adjusting the Intersect method(s) , I've used a Select Case statement to organize the various logic conditions. 在调整了Intersect方法之后,我使用了Select Case语句来组织各种逻辑条件。

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("A1, A5:A8")) Is Nothing Then
        On Error GoTo bm_Safe_exit
        Application.EnableEvents = False
        Dim c As Range
        For Each c In Intersect(Target, Range("A1, A5:A8"))
            If Application.Count(Range("A1, A5:A8")) < 5 Then
                Range("A9") = vbNullString
            Else
                Range("A9").Formula = "=SUM(A5:A8)"
                Select Case Range("A9").Value2
                    Case Is > Range("A1").Value2
                        MsgBox "The sum of A9 cannot exceed A1 when all entries are completed"
                        Range("A9", c).ClearContents
                        GoTo bm_Safe_exit
                    Case Is < 0, Is >= 10 ^ 7
                        MsgBox "Entry in cell " & c.Address(0, 0) & " must be a number from 0 and 9,999,999"
                        Range("A9", c).ClearContents
                        GoTo bm_Safe_exit
                    Case Else
                        'do nothing - A9 is oh-key-doh-key
                End Select
            End If
        Next c
    End If
bm_Safe_exit:
    Application.EnableEvents = True
End Sub

This should meet your needs: 这应该符合您的需求:

Private Sub Worksheet_Change(ByVal Target As Range)
   Dim rLook As Range, v1 As Variant, v5 As Variant
   Dim v6 As Variant, v7 As Variant, v8 As Variant
   Dim bad As Boolean
   Set rLook = Range("A1, A5:A8")

   If Intersect(Target, rLook) Is Nothing Then Exit Sub
   v1 = Range("A1").Value
   v5 = Range("A5").Value
   v6 = Range("A6").Value
   v7 = Range("A7").Value
   v8 = Range("A8").Value
   If v1 = "" Or v5 = "" Or v6 = "" Or v7 = "" Or v8 = "" Then Exit Sub

   bad = False
   If Not IsNumeric(v1) Then bad = True
   If Not IsNumeric(v5) Then bad = True
   If Not IsNumeric(v6) Then bad = True
   If Not IsNumeric(v7) Then bad = True
   If Not IsNumeric(v8) Then bad = True
   If bad Then
      MsgBox "non-numeric data"
      Exit Sub
   End If

   If v1 < 0 Or v1 > 9999999 Then bad = True
   If v5 < 0 Or v1 > 9999999 Then bad = True
   If v6 < 0 Or v1 > 9999999 Then bad = True
   If v7 < 0 Or v1 > 9999999 Then bad = True
   If v8 < 0 Or v1 > 9999999 Then bad = True
   If bad Then
      MsgBox "data out of bounds"
      Exit Sub
   End If

   If Range("A9").Value > v1 Then
      MsgBox "sum exceeds the value in A1"
   End If

End Sub

The macro is triggered when the user completes inputs to A1 and A5 through A8 . 当用户完成对A1A5A8的输入时,将触发宏。

I assume that the sum formula is already in cell A9 我假设总和公式已经在单元格A9中

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

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