(VBA) IF = TRUE 中的布尔条件;尽管如此,还是跳过了胆量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13477471/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 14:28:14  来源:igfitidea点击:

(VBA) Boolean conditions in IF = TRUE; skips guts despite of this

excelvbaboolean

提问by KHH

I've got the following IF-conditions set up:

我已经设置了以下 IF 条件:

    If Range("D" & x) + Range("D" & z) = 0 = True And (Range("G" & x) = Range("G" & z)) = True And (IsEmpty(Range("A" & x)) = False) = True And (IsEmpty(Range("C" & x)) = False) = True Then

          [guts]

    End if

I've inserted all of the (condition) = Truein order to be able to evaluate each individual condition while debugging, and ALL of them return TRUE = TRUE. And yet it doesn't execute the guts of the boolean. It just doesn't make sense to me, but hopefully one of you can see what is wrong with it. I created a test sheet to run the macro on, containing only a few rows of data that I know should return TRUE on all of the conditions, but it doesn't work. I have a feeling that it is a painfully obvious error.

我已经插入了所有(condition) = True以便能够在调试时评估每个单独的条件,并且所有这些都返回 TRUE = TRUE。然而它并没有执行布尔值的胆量。这对我来说没有意义,但希望你们中的一个人能看到它有什么问题。我创建了一个测试表来运行宏,只包含几行数据,我知道在所有条件下都应该返回 TRUE,但它不起作用。我有一种感觉,这是一个令人痛苦的明显错误。

You can download a sample including some data here: http://www.mediafire.com/view/?rm299bttfsp1v01

您可以在此处下载包含一些数据的示例:http: //www.mediafire.com/view/?rm299bttfsp1v01

采纳答案by Jook

Reformatted this and made a pastable example of it:

重新格式化并制作了一个可粘贴的示例:

 Public Sub demo()
  Dim x, Z As Long

  x = 1
  Z = 1

  If Range("D" & x) + Range("D" & Z) = 0 = True And _
  (Range("G" & x) = Range("G" & Z)) = True And _
  (IsEmpty(Range("A" & x)) = False) = True And _
  (IsEmpty(Range("C" & x)) = False) = True Then

          Debug.Print "[guts]"

    End If

End Sub

It works for me, if A1=12 and C1=12 (e.g.)

它对我有用,如果 A1=12 和 C1=12(例如)

But this is how I would write this:

但这就是我写这个的方式:

Public Sub demo()
  Dim x, Z As Long

  x = 1
  Z = 1

  'to confirm
  Debug.Print Range("D" & x) + Range("D" & Z) = 0
  Debug.Print Range("G" & x) = Range("G" & Z)
  Debug.Print Not IsEmpty(Range("A" & x))
  Debug.Print Not IsEmpty(Range("C" & x))

  If Range("D" & x) + Range("D" & Z) = 0 And _
     Range("G" & x) = Range("G" & Z) And _
     Not IsEmpty(Range("A" & x)) And _
     Not IsEmpty(Range("C" & x)) Then

          Debug.Print "[guts]"
  End If

End Sub

Edit

编辑

You can test this easily, using this code:

您可以使用以下代码轻松测试:

  Debug.Print Range("D" & x) + Range("D" & Z) = 0 And _
              Range("G" & x) = Range("G" & Z) And _
              Not IsEmpty(Range("A" & x)) And _
              Not IsEmpty(Range("C" & x))

Paste if after For Z = 8 To 16.

如果之后粘贴 For Z = 8 To 16

It does not resolve to true, that is why you are not getting the guts ;)

它没有解析为真的,这就是为什么你没有勇气;)

回答by MNOPups

Could it be that you are getting hung up by the order of operations? It looks like you may be missing a set of parenthesis around the first condition...

会不会是你被操作顺序挂了?看起来您可能在第一个条件周围缺少一组括号...

If (Range("D" & x) + Range("D" & z) = 0) = True And (Range("G" & x) = Range("G" & z)) = True And (IsEmpty(Range("A" & x)) = False) = True And (IsEmpty(Range("C" & x)) = False) = True Then

      [guts]

End if