vba 检查负值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13162156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
vba check for negative value
提问by Trung Tran
I wrote a simple VBA code to check if the value of a cell is negative and if it is negative, highlight it red. For some reason, I keep getting "run-time mismatch". My code is
我编写了一个简单的 VBA 代码来检查单元格的值是否为负,如果为负,则将其突出显示为红色。出于某种原因,我不断收到“运行时不匹配”。我的代码是
For x = 2 To 100
Set val3 = Worksheets("Summary").Cells(x, 9)
If val3.Value < 0 Then
Worksheets("Summary").Cells(x, 9).FontColorIndex = 3
End If
Next x
Column 9 (the column I am checking) is filled with dollar values. Thank you in advance for your help.
第 9 列(我正在检查的列)填充了美元值。预先感谢您的帮助。
回答by timbur
In your code, you're simply missing a dot.
在您的代码中,您只是缺少一个点。
FontColorIndex = 3
字体颜色索引 = 3
should be:
应该:
Font.ColorIndex = 3
字体颜色索引 = 3
回答by bennyhardjono
Thanks for your post, I needed to change the negative numbers to zeros after a certain process - BUT I want to check them before I use them so changing the font color is useful, so this code works well for the whole sheet range until (245,50)
感谢您的帖子,我需要在某个过程后将负数更改为零 - 但我想在使用它们之前检查它们,因此更改字体颜色很有用,因此此代码适用于整个工作表范围,直到 (245 ,50)
Sub negnumbers()
For x = 1 To 245
For y = 1 To 50
Set val3 = Worksheets("target sheet").Cells(x, y)
If val3.Value < 0 Then
Worksheets("target sheet").Cells(x, y).Font.ColorIndex = 3
Worksheets("target sheet").Cells(x, y) = 0
End If
Next y
Next x
End Sub
回答by elrado
Public Sub test()
For x = 2 To 100
Set val3 = Worksheets("Sheet1").Cells(x, 9)
If val3.Value < 0 Then
Worksheets("Sheet1").Cells(x, 9).Font.Color = RGB(99, 179, 73)
End If
Next x
End Sub
Code above works in excel 2007
上面的代码适用于 excel 2007