可是試試用select case statement
function udf_myCompare(myNum as double) as variant
select case myNum
case is <= -4000
udf_myCompare = 1
case is <= -2000
udf_myCompare = 2
case is <= -1000
udf_myCompare = 3
case is >= 4000
udf_myCompare = 4
case is >= 2000
udf_myCompare = 5
case is >= 1000
udf_myCompare = 6
case else
udf_myCompare = "'000"
end select
end function
在U2
寫下=udf_myCompare(i2)
即可。
Sub Macro1()
If Range("I2") <= -4000 Then
Range("U2") = "1"
ElseIf Range("I2") <= -2000 Then
Range("U2") = "2"
ElseIf Range("I2") <= -1000 Then
Range("U2") = "3"
ElseIf Range("I2") >= 4000 Then
Range("U2") = "4"
ElseIf Range("I2") >= 2000 Then
Range("U2") = "5"
ElseIf Range("I2") >= 1000 Then
Range("U2") = "6"
Else
Range("U2") = "'000"
End If
End Sub