检查具有负值的单元格

时间:2015-11-06 15:59:15

标签: vba

我想知道如何检查包含负值的单元格并应用一些更改。

我从这段代码开始,但是我在这句话中遇到了错误If (ActiveSheet.Cells(a, 37).Value)< 0

这是我的代码:

dim a as integer
Dim MaPlage As Range`

For a = 2 To ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row
    Set MaPlage = Range("A:AP").Rows(a)
    If (ActiveSheet.Cells(a, 37).Value) < 0  Then
        ActiveSheet.Cells(a, 31).Interior.Color = vbYellow           
        ActiveSheet.Cells(a, 14).Interior.Color = vbYellow`
    End If
Next a

1 个答案:

答案 0 :(得分:2)

Type Mismatch表示您正在使用文本值检查整数(< 0 - 语句的if部分)。您需要使用IsNumeric()函数检查测试值是否为数字:http://www.techonthenet.com/excel/formulas/isnumeric.php

您的代码将变为:

dim a as integer
Dim MaPlage As Range

For a = 2 To ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row
    Set MaPlage = Range("A:AP").Rows(a)
    If IsNumeric(ActiveSheet.Cells(a, 37).Value) and (ActiveSheet.Cells(a, 37).Value) < 0  Then
        ActiveSheet.Cells(a, 31).Interior.Color = vbYellow           
        ActiveSheet.Cells(a, 14).Interior.Color = vbYellow
    End If
Next a