0

I have the following piece of code:

Range("C13:Y21").Select
For Each cell2 In Selection
      If cell2 = "-0.0568" Then
            x = 1000
      End If`
Next cell2

I want to discover the value -0.0568, but the issue I have is that when the value comes up, Visual Basic sees it as being equal to 0. I obviously want Visual Basic to see the value for what it actually is and not rounded. Could someone spot the mistake I'm making?

4

2 回答 2

1

您的问题来自数据类型。仅此代码即可在我的计算机上完美运行。我怀疑您已将 cell2 声明为不适合的数据类型。

我更喜欢这种方式:

Sub Test()


Range("C13:C21").Select
For Each cell2 In Selection
      If cell2.Value = -0.0568 Then
            Debug.Print "YES"
      End If
Next cell2


End Sub
于 2013-07-03T15:59:40.623 回答
0

如果单击值为 -0.0568 的单元格,公式栏中是否会显示任何额外的小数?

您可以考虑同时检查值和文本,以解决此问题:

  If cell2.Value = -0.0568 Or cell2.Text = "-0.0568" Then

其中.Text是单元格中显示的值。

为避免在将数字与小数进行比较时出现任何问题,您可以考虑使用Round()

  If Round(cell2.Value, 4) = -0.0568 Then
于 2013-07-03T16:26:28.060 回答