VBA:如果Base或2 * Base则发出警报

时间:2017-10-08 03:17:19

标签: vba excel-vba excel

我已经在Excel先生发布了这个Qn,但没有一个解决方案有效。

https://www.mrexcel.com/forum/excel-questions/1026003-vba-alert-if-base-2-base.html?posted=1#post4924266

我在这里重新制作了Qn:

使用Excel 2010。 如果单元格值不等于Base或2 * Base,我希望Excel提醒我,但当单元格不是Base时,它可能是2 * Base,反之亦然;因此Excel总是告诉我这是错误的。

我应该如何修改以下代码?

Sub SubTotal_test()
    Dim k, i, j, minim, countleft, base, tmp_row, Last_Row, rw As Integer

    base = InputBox("State the number of items")
    Last_Row = ActiveWorkbook.ActiveSheet.Cells(Rows.Count, 2).End(xlUp).Row

    For i = Last_Row + 1 To 1 Step -1

        If (ActiveWorkbook.ActiveSheet.Cells(i, 3) = "L" And ActiveWorkbook.ActiveSheet.Cells(i, 11) <> base) Then
            If (ActiveWorkbook.ActiveSheet.Cells(i, 3) = "L" And ActiveWorkbook.ActiveSheet.Cells(i, 11) <> (2 * base)) Then
                MsgBox "There is an error with the SubTotal. Please change manually."
                Exit For
            End If
        Else
            'MsgBox "SubTotal OK"
            ActiveWorkbook.ActiveSheet.Cells(i, 11).Select
        End If
    Next i

End Sub

1 个答案:

答案 0 :(得分:3)

只是测试细胞是否不等于碱基* 2:

Sub SubTotal_test()
    Dim k, i, j, minim, countleft, base, tmp_row, Last_Row, rw As Integer

    base = InputBox("State the number of items")
    Last_Row = ActiveWorkbook.ActiveSheet.Cells(Rows.Count, 2).End(xlUp).Row

    For i = Last_Row + 1 To 1 Step -1
        If ActiveWorkbook.ActiveSheet.Cells(i, 3) = "L" And _
           ActiveWorkbook.ActiveSheet.Cells(i, 11) <> base And _
           ActiveWorkbook.ActiveSheet.Cells(i, 11) <> 2 * base Then
            MsgBox "There is an error with the SubTotal. Please change manually."
            ActiveWorkbook.ActiveSheet.Cells(i, 11).Select
            Exit For
        End If
    Next i

End Sub

我相信你遇到了麻烦,因为你正在解释你的要求&#34;如果单元格值不等于Base或2 * Base&#34; as&#34;如果单元格值不等于base或单元格值不等于2 * base&#34;,但你真的是指&#34;如果单元格值不是(等于base或等于2 *基)&#34;可以表达,通过应用&#34; not&#34;到括号的内部,如&#34;如果单元格值是(不等于基数)和(不等于2 *基数)&#34;。

另请注意您的陈述

Dim k, i, j, minim, countleft, base, tmp_row, Last_Row, rw As Integer

等同于语句

Dim k As Variant, i As Variant, j As Variant, minim As Variant, countleft As Variant, base As Variant, tmp_row As Variant, Last_Row As Variant, rw As Integer

但您可能打算使用

Dim k As Integer, i As Integer, j As Integer, minim As Integer, countleft As Integer, base As Integer, tmp_row As Integer, Last_Row As Integer, rw As Integer