我正在使用button命令,但使用elseif语句看不到这个代码有什么问题

这个代码应该做的是,如果你在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 Aceptable”的表格中有一个错字。 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的例子应该是确定的。