如果还是在VBA中多个语句

我想用14列重新分配一个Excel文件到正确的列(12.000行)。

为此,我必须使用一些If和Or语句将数字放在一个matrix中。 但显然我没有从中得到正确的东西。

它使我所有的单元格为零,而具有值的单元格应该保持这个值。

我哪里错了?

For i = 1 To LastRow If Cells(i, 8).Value2 = "" Then Cells(i, 8).Value2 = 0 If Cells(i, 1).Value2 < 437 And Cells(i, 5).Value2 = "aa" _ Or Cells(i, 5).Value2 = "bb" _ Or Cells(i, 5).Value2 = "cc" _ Or Cells(i, 5).Value2 = "dd" _ Or Cells(i, 5).Value2 = "ee" _ Or Cells(i, 5).Value2 = "ff" _ Or Cells(i, 5).Value2 = "gg" _ And Cells(i, 7).Value2 = "" _ Then Cells(i, 7).Value2 = 0 Next i 

因此,如果单元格包含aa或bb或cc或dd或ee或ff或gg并且是empthy,则单元格应该变为0,否则应该保持相同的值。 之后,它应该进入matrix

 Then Matrixgetallen(i, 2) = CDbl(Cells(i, 7).Value2) 

但是我没有设法在相同的陈述。

如果有6种这样的If语句,那么可能If Then Else不起作用。

很难优化不看你的完整的代码,但这部分:

  1. 由于VBA 不短路,所以将AND断成两个IF
  2. 而不是一个长序列的OR ,对数组进行单次testing(数字结果表示find确切的string)

 i = 1 Dim strIN strIN = Array("aaf", "bb", "cc", "dd", "ee", "ff", "gg") If Cells(i, 1).Value2 < 437 Then If Len(Cells(i, 5)) = 0 Then Cells(i, 7).Value2 = 0 Else If IsNumeric(Application.Match(Cells(i, 5), strIN, 0)) Then Cells(i, 7).Value2 = 0 End If End If 

谢谢你的提示阿克塞尔。 我以前从来没有见过这个,但是它的效果很棒! 这个诀窍:

  If Cells(i, 1).Value2 < 437 And (Cells(i, 5).Value2 = "aa" _ Or Cells(i, 5).Value2 = "bb" _ Or Cells(i, 5).Value2 = "cc" _ Or Cells(i, 5).Value2 = "dd" _ Or Cells(i, 5).Value2 = "ee" _ Or Cells(i, 5).Value2 = "ff" _ Or Cells(i, 5).Value2 = "gg") _ And Cells(i, 7).Value2 = "" _ Then Cells(i, 7).Value2 = 0 

但我也很喜欢Brett的答案。 这是一个很好的方法来处理它。