函数永远不会执行ElseIf语句

Ste*_*ev0 1 excel vba excel-vba

在excel上使用一些VBA,我编写了一个用户定义的函数来根据行号进行一些算术运算.它似乎乍一看,然后我意识到无论它总是执行什么,好像第一个If声明是真的,并且从未考虑过ElseIf.我究竟做错了什么?

Function redProfit(ByVal myRange As Range) As Long
    Dim rangerow As Range, baseprofit As Long
    Application.Volatile
    baseprofit = 3500000
    With myRange
        If (.Row = 3 Or 11) Then
            redProfit = baseprofit * 0.1
        ElseIf (.Row = 4 Or 10) Then
            redProfit = baseprofit * 0.08
        ElseIf (.Row = 5 Or 9) Then
            redProfit = baseprofit * 0.07
        ElseIf (.Row = 6 Or 8) Then
            redProfit = baseprofit * 0.06
        ElseIf .Row = 7 Then
            redProfit = baseprofit * 0.05

        End If
    End With

End Function
Run Code Online (Sandbox Code Playgroud)

Ron*_*eld 5

除了其他人编写的内容之外,这似乎是一个Select Case结构可能更可取的实例:

Function redProfit(ByVal myRange As Range) As Long
Dim rangerow As Range, baseprofit As Long
Application.Volatile
baseprofit = 3500000

Select Case myRange.Row
    Case 3, 11
        redProfit = baseprofit * 0.1
    Case 4, 10
        redProfit = baseprofit * 0.08
    Case 5, 9
        redProfit = baseprofit * 0.07
    Case 6, 8
        redProfit = baseprofit * 0.06
    Case 7
        redProfit = baseprofit * 0.05
End Select
End Function
Run Code Online (Sandbox Code Playgroud)