缩短if if语句

时间:2017-03-18 01:59:44

标签: vba if-statement

如何以更短的方式编写此代码?

这只是我代码的一部分。它很长,我的系统运行有点慢。我希望每次用户在N2范围内添加数字值时运行:P&最后一行。

Sub TEST()

lastrow = Range("A10000").End(xlUp).Row

For i = 2 To lastrow

'---1
If Cells(i, "P").Value >= 1 And Cells(i, "P").Value <= 5 And Cells(i, "I").Value >= Cells(i, "K").Value And Range("P" & i).Value * Range("K" & i).Value <= Cells(i, "I").Value Then
Cells(i, "P").Value = Range("P" & i).Value * Range("K" & i).Value

'----2
ElseIf ((Cells(i, "P").Value >= Cells(i, "I").Value) Or (Range("I" & i).Value - Range("P" & i).Value < Range("K" & i).Value)) And Cells(i, "O").Value >= 1 And Cells(i, "O").Value <= 5 And Cells(i, "H").Value >= Cells(i, "K").Value And Range("O" & i).Value * Range("K" & i).Value <= Cells(i, "H").Value Then
Cells(i, "O").Value = Range("O" & i).Value * Range("K" & i).Value
'---3
ElseIf ((Cells(i, "P").Value >= Cells(i, "I").Value) Or (Range("I" & i).Value - Range("P" & i).Value < Range("K" & i).Value)) And Cells(i, "N").Value >= 1 And Cells(i, "N").Value <= 4 And Cells(i, "H").Value >= Cells(i, "K").Value And Range("N" & i).Value * Range("K" & i).Value <= Cells(i, "H").Value Then
Cells(i, "O").Value = Range("N" & i).Value * Range("K" & i).Value
Range("N" & i) = vbNullString

'----4
ElseIf ((Cells(i, "P").Value < Cells(i, "I").Value) Or (Range("I" & i).Value - Range("P" & i).Value >= Range("K" & i).Value)) And Cells(i, "O").Value >= 1 And Cells(i, "O").Value <= 5 And Cells(i, "I").Value >= Cells(i, "K").Value And Range("O" & i).Value * Range("K" & i).Value <= Cells(i, "I").Value Then
Cells(i, "P").Value = Range("O" & i).Value * Range("K" & i).Value
Range("O" & i) = vbNullString
'---5
ElseIf ((Cells(i, "P").Value < Cells(i, "I").Value) Or (Range("I" & i).Value - Range("P" & i).Value >= Range("K" & i).Value)) And Cells(i, "N").Value >= 1 And Cells(i, "N").Value <= 4 And Cells(i, "I").Value >= Cells(i, "K").Value And Range("N" & i).Value * Range("K" & i).Value <= Cells(i, "I").Value Then
Cells(i, "P").Value = Range("N" & i).Value * Range("K" & i).Value
Range("N" & i) = vbNullString

'---6
ElseIf ((Range("I" & i).Value - Range("P" & i).Value < Range("K" & i).Value) Or (Range("H" & i).Value - Range("O" & i).Value < Range("K" & i).Value)) And Cells(i, "N").Value >= 1 And Cells(i, "N").Value <= 4 And (Cells(i, "H").Value >= Cells(i, "K").Value Or Cells(i, "I").Value >= Cells(i, "K").Value) Then
Range("N" & i) = vbNullString
MsgBox "You already"


'---6
ElseIf Cells(i, "I").Value = vbNullString And Cells(i, "H").Value = vbNullString And Cells(i, "N").Value >= 1 And Cells(i, "N").Value <= 4 Then
Range("N" & i) = vbNullString
MsgBox "There is no "

2 个答案:

答案 0 :(得分:4)

为返回Boolean的测试提取函数。例如,第一次If测试(为了清晰起见,添加了行继续)......

If Cells(i, "P").Value >= 1 And _ 
   Cells(i, "P").Value <= 5 And _
   Cells(i, "I").Value >= Cells(i, "K").Value And _
   Range("P" & i).Value * Range("K" & i).Value <= Cells(i, "I").Value Then

...可以写成以下函数:

Private Function WhateverTheTestIsFor(rowNumber As Long) As Boolean
    Dim columnI As Variant
    Dim columnP As Variant

    columnP = Cells(rowNumber, "P").Value
    columnI = Cells(rowNumber, "I").Value
    columnK = Cells(rowNumber, "K").Value
    Select Case True
        Case columnP < 1
        Case columnP > 5
        Case columnI < columnK
        Case columnP * columnK > columnI
        Case Else
            WhateverTheTestIsFor = True
    End Select
End Function

然后你的第一个If条款变为:

If WhateverTheTestIsFor(i) Then

请注意,如评论中所述,您应该只读取工作表中的值一次。代码还存在其他问题 - 您需要限定对CellsRange的引用,并且选择要在两个之间使用的约定。混合Cells(i, "K")Range("K" & i)令人困惑。

答案 1 :(得分:1)

您是否尝试过对此进行数据验证? 您可以创建多个以复杂方式重叠的规则,而无需编写复杂的代码。

在Excel中

,选择所需的范围PN:PN, 然后在数据选项卡中选择数据验证。 您将看到一个向导,可以帮助您添加规则层,这些规则可以在输入特定值时警告用户,甚至可以防止输入不可接受的值。

http://www.excel-easy.com/basics/data-validation.html