[英]I'm Working with a button command, but can't see what's wrong with this code using elseif statements
該代碼應該執行的操作是,如果您在cell 2,2
有一個數字,並且它介於這些if語句的范圍之間,那么它將在cell 3,3
給您一個數字。 問題在於它僅適用於前三個語句。 第四個不起作用。 它為您提供數字1而不是數字2。
*注:程序使用更多語句,但這只是為了表明問題出在第三條語句之后,對不起我的英語。
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
您的工作表中有一個“ MEC可接受”的錯字。 sheet.Cells
。 從上下文來看,它看起來應該是Sheet2.Cells
ElseIf sheet1.Cells(2, 2) > 1 < sheet.Cells(3, 6) Then
除此之外,請嘗試使用Select Case
。 您可以使用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
看來您在這里混用了幾種語言。 編寫第三和第四比較的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
這些陳述中仍然有一些優先事項。 如果B2為6,則它大於1且大於5。根據F3和G3中的內容,可能永遠不會達到第四個條件。 考慮到您的F3示例為5,應該可以。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.