Вы можете сразу же сделать его короче, изменив
If <Value>=1 Then
Output = <Something>
Else
If <Value>=2 Then
Output = <Something else>
End If
End If
на
If <Value>=1 Then
Output = <Something>
ElseIf <Value>=2 Then
Output = <Something else>
End If
Если продвинуться дальше, вы можете использовать Select Case
для того же:
Select Case <Value>
Case 1:
Output = <Something>
Case 2:
Output = <Something else>
End Select
Или даже используйте Choose
Output = Choose(<value>, <something>, <something else>)
Вот пример кода во всех трех вариантах - другие (например, IIf
, или формула, которая преобразует 1/2/3 в -1/0/1) тоже доступны
'ElseIf
Private Sub CommandButton1_Click()
If Range("B15").Value = 6 Then
MsgBox "Maximum Beams Reached (7)", vbCritical
Exit Sub
End If
Range("B15").Value = Range("B15").Value + 1
Columns(colNum & "D").Insert Shift:=xlDown
Range("D3:D13").Borders(xlInsideHorizontal).LineStyle = xlContinuous
Range("D3:D13").Borders(xlEdgeRight).LineStyle = xlContinuous
If Range("B15").Value = 1 Then
Range("D15").Value = "Beam Summary"
Range("D16").Value = " Concrete Volume"
Range("D17").Value = " Rebar Length"
Range("D3").Value = "Beam " & Range("B15").Value + 1
ElseIf Range("B15").Value = 2 Then
Range("D3").Value = "Beam " & Range("B15").Value
Range("E3").Value = "Beam " & Range("B15").Value + 1
ElseIf Range("B15").Value = 3 Then
Range("D3").Value = "Beam " & Range("B15").Value - 1
Range("E3").Value = "Beam " & Range("B15").Value
Range("F3").Value = "Beam " & Range("B15").Value + 1
End If
End Sub
'Select Case
Private Sub CommandButton1_Click()
If Range("B15").Value = 6 Then
MsgBox "Maximum Beams Reached (7)", vbCritical
Exit Sub
End If
Range("B15").Value = Range("B15").Value + 1
Columns(colNum & "D").Insert Shift:=xlDown
Range("D3:D13").Borders(xlInsideHorizontal).LineStyle = xlContinuous
Range("D3:D13").Borders(xlEdgeRight).LineStyle = xlContinuous
Select Case Range("B15").Value
Case 1:
Range("D15").Value = "Beam Summary"
Range("D16").Value = " Concrete Volume"
Range("D17").Value = " Rebar Length"
Range("D3").Value = "Beam " & Range("B15").Value + 1
Case 2:
Range("D3").Value = "Beam " & Range("B15").Value
Range("E3").Value = "Beam " & Range("B15").Value + 1
Case 3
Range("D3").Value = "Beam " & Range("B15").Value - 1
Range("E3").Value = "Beam " & Range("B15").Value
Range("F3").Value = "Beam " & Range("B15").Value + 1
End Select
End Sub
'Choose
Private Sub CommandButton1_Click()
If Range("B15").Value = 6 Then
MsgBox "Maximum Beams Reached (7)", vbCritical
Exit Sub
End If
Range("B15").Value = Range("B15").Value + 1
Columns(colNum & "D").Insert Shift:=xlDown
Range("D3:D13").Borders(xlInsideHorizontal).LineStyle = xlContinuous
Range("D3:D13").Borders(xlEdgeRight).LineStyle = xlContinuous
If Range("B15").Value = 1 Then
Range("D15").Value = "Beam Summary"
Range("D16").Value = " Concrete Volume"
Range("D17").Value = " Rebar Length"
End If
If Range("B15").Value >=1 AND Range("B15").Value <=3 Then
Range("D3").Value = Choose(Range("B15").Value, "Beam " & Range("B15").Value + 1, "Beam " & Range("B15").Value, "Beam " & Range("B15").Value - 1)
Range("E3").Value = Choose(Range("B15").Value, Range("E3").Value, "Beam " & Range("B15").Value + 1, "Beam " & Range("B15").Value)
Range("F3").Value = Choose(Range("B15").Value, Range("E3").Value, Range("E3").Value, "Beam " & Range("B15").Value + 1)
End IF
End Sub