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

时间:2016-01-15 13:40:25

标签: excel-vba vba excel

我在Excel中有一个需要一些vba代码的场景。我是一个相对新手,并试图找到一个解决方案达到了死胡同。

例如,假设用户在单元格A1中输入数值。

然后他们还必须在单元格A5,A6,A7和A8中输入值。

此总和的总和显示在单元格A9中,使用通用Excel Excel函数。

虽然可以接受零('0')输入,但A5 + A8的单元格都不能留空。

A9的值可以小于,等于,但不超过A1中的值。

如果A9超过A1,则必须弹出一条错误消息,提醒他们确实是这种情况。

无法输入字母字符。弹出错误消息以提醒他们。

数字输入必须介于0到9,999,999之间。如果没有,则会弹出一条错误消息提醒他们。

我获得了一段vba代码(下面),我将其用于类似的目的,效果非常好。但是,我无法弄清楚如果A9中的值超过A1,如何合并将识别并返回错误消息的代码。这是我尝试做的,但我知道这是错的!代码如下:

    WITH CTE
AS
(SELECT        
    RTRIM(BLDGCODE) AS BLDGCODE, 
    RTRIM(FLOORCODE) AS FLOORCODE, 
    SUM(CASE WHEN rtrim(spacetype) LIKE '%fs' THEN 1 ELSE 0 END) AS FLR_SHARED,          
    SUM(CASE WHEN rtrim(spacetype) LIKE '%bs' THEN 1 ELSE 0 END) AS BLDG_SHARED, 
    SUM(CASE WHEN rtrim(spacetype) LIKE '%as' AND rtrim(spacetype) NOT IN ('U-TLS-AS', 'U-PARK-AS') THEN 1 ELSE 0 END) AS FLR_ASSIGNABLE,
    SUM(CASE WHEN rtrim(spacetype) = 'U-TLS-AS' THEN 1 ELSE 0 END) AS TENANT
    FROM FMA0
    WHERE
    bldgcode in ('us0385', 'us0566')
    GROUP BY BLDGCODE, FLOORCODE
)
SELECT CTE.*, SUM(FLR_ASSIGNABLE) AS BLDG_ASSIGNABLE FROM CTE
where (FLR_SHARED > 0 AND FLR_ASSIGNABLE = 0) or
      (BLDG_SHARED > 0 AND SUM(FLR_ASSIGNABLE) = 0)

如果有人能帮助我,我将非常感激!!

校准

2 个答案:

答案 0 :(得分:3)

在我看来,你的Worksheet_Change事件宏应该处理A1和A5的变化:A8,而不是A9。如果A5:A8中的值符合条件,则可以检查它们的总数与A1。

调整Intersect method(s)后,我使用Select Case statement来组织各种逻辑条件。

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

答案 1 :(得分:2)

这应该符合您的需求:

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

当用户完成对 A1 A5 的输入时,会触发宏strong> A8 。

我假设总和公式已经在单元格 A9