[英]I'm Working with a button command, but can't see what's wrong with this code using elseif statements
What this code is supposed to do is that if you have a number in cell 2,2
and it's between a range of those if statements, it will give you a number in cell 3,3
. 该代码应该执行的操作是,如果您在
cell 2,2
有一个数字,并且它介于这些if语句的范围之间,那么它将在cell 3,3
给您一个数字。 The problem is that it only works on the first 3 statements. 问题在于它仅适用于前三个语句。 The fourth one doesn't work.
第四个不起作用。 It gives you number 1 instead of number 2.
它为您提供数字1而不是数字2。
*note: the program uses more statements but this is just to show that the problem is after the third statement, and sorry for my English. *注:程序使用更多语句,但这只是为了表明问题出在第三条语句之后,对不起我的英语。
Cell 3,6= 5
Cell 3,7= 10
'MEC
If sheet1.Cells(2, 2) = Empty Then
sheet3.Cells(3, 3) = ""
sheet4.Cells(3, 3) = 0
'MEC limited
ElseIf sheet2.Cells(2, 2) = 1 Then
sheet3.Cells(3, 3) = 0
'MEC aceptable
ElseIf sheet1.Cells(2, 2) > 1 < sheet.Cells(3, 6) Then
sheet3.Cells(3, 3) = 1
'MEC Good
ElseIf sheet1.Cells(2, 2) > 5 <= sheet2.Cells(3, 7) Then
sheet3.Cells(3, 3) = 2
End If
End Sub
You had a typo in your sheets for 'MEC Aceptable. 您的工作表中有一个“ MEC可接受”的错字。
sheet.Cells
. sheet.Cells
。 Judging by the context, it looks like it should be Sheet2.Cells
从上下文来看,它看起来应该是
Sheet2.Cells
ElseIf sheet1.Cells(2, 2) > 1 < sheet.Cells(3, 6) Then
In addition to that, try using Select Case
. 除此之外,请尝试使用
Select Case
。 You can use IF statements, but Select Case
gets handy when you have many ElseIf
s 您可以使用IF语句,但是当您有很多
ElseIf
时, Select Case
会派上用场
Select Case (Sheet1.Cells(2, 2).Value)
Case Is = ""
Sheet3.Cells(3, 3) = ""
Sheet4.Cells(3, 3) = 0
Case Is = 1
Sheet3.Cells(3, 3) = 0
Case Is <= Sheet2.Cells(3, 6) 'There was typo in your orig code.. Sheet2?
Sheet3.Cells(3, 3) = 1
Case Is <= Sheet2.Cells(3, 7)
Sheet3.Cells(3, 3) = 2
End Select
It would appear that you have a couple of languages confused here. 看来您在这里混用了几种语言。 The VBA method of writing the third and fourth comparisons is closer to,
编写第三和第四比较的VBA方法更接近
If Sheet1.Cells(2, 2) = Empty Then
Sheet3.Cells(3, 3) = ""
Sheet4.Cells(3, 3) = 0
'MEC limited
ElseIf Sheet2.Cells(2, 2) = 1 Then
Sheet3.Cells(3, 3) = 0
'MEC aceptable
ElseIf Sheet1.Cells(2, 2) > 1 And Sheet1.Cells(2, 2) < Sheet2.Cells(3, 6) Then
Sheet3.Cells(3, 3) = 1
'MEC Good
ElseIf Sheet1.Cells(2, 2) > 5 And Sheet1.Cells(2, 2) <= Sheet2.Cells(3, 7) Then
Sheet3.Cells(3, 3) = 2
End If
There is still some priority in these statements. 这些陈述中仍然有一些优先事项。 If B2 is 6 then it is greater than 1 and greater than 5. Depending on what is in F3 and G3, the fourth condition may never be reached.
如果B2为6,则它大于1且大于5。根据F3和G3中的内容,可能永远不会达到第四个条件。 Given your example of F3 being 5 it should be OK.
考虑到您的F3示例为5,应该可以。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.