【问题标题】:VBA if statement to be shortenedVBA if 语句被缩短
【发布时间】:2013-07-11 07:24:12
【问题描述】:

如何缩短我的 if 语句?有没有其他方法可以缩短??

F = Strt
E = Strt + A1
I = Strt + A1 + A2
A = Strt + A2 + A1 + A3
b = Strt + A2 + A1 + A3 + A4
c = Strt + A1 + A2 + A3 + A4 + A5
D = Strt + A1 + A2 + A3 + A4 + A5 + A6
G = Strt + A1 + A2 + A3 + A4 + A5 + A6 + A7
H = Strt + A1 + A2 + A3 + A4 + A5 + A6 + A7 + A8
J = Strt + A1 + A2 + A3 + A4 + A5 + A6 + A7 + A8 + A9


    If F >= Row Then
    asd = 0
    ElseIf E >= Row Then
    asd = 1
    ElseIf I >= Row Then
    asd = 2
    ElseIf A >= Row Then
    asd = 3
    ElseIf b >= Row Then
    asd = 4
    ElseIf c >= Row Then
    asd = 5
    ElseIf D >= Row Then
    asd = 6
    ElseIf G >= Row Then
    asd = 7
    ElseIf H >= Row Then
    asd = 8
    ElseIf J >= Row Then
    asd = 9
    Else
    End If

还有我的变量..我想知道我是否可以缩短它.. 非常感谢,如果回答了

【问题讨论】:

    标签: vba loops if-statement


    【解决方案1】:

    自从我完成 vba 以来已经有好几年了,但这里是我将如何解决它的伪代码:

    arr = [Strt+A1, A2, A3, A4, A5, A6, A7, A8, A9]
    for i = 0; i < arr.length; i++ // assume 0-based array
        if i > 0
            arr[i] = arr[i-1]+arr[i] // sum
        if Row <= arr[i]
            asd = i
            break
    

    这应该完全符合您的代码正在做的事情,只是更简单。基本上,您将 A1-A9 相加(从 Strt 开始),并返回 Strt

    我发现这段代码更具可读性,而且它肯定更短,以后扩展更容易。

    【讨论】:

    • 先生.. 代码正在执行我需要的东西.. 很抱歉我忘了包括我的函数行.. 这是行函数 asd(A1 As Integer, A2 As Integer, A3 As Integer , A4 As Integer, A5 As Integer, A6 As Integer, Strt As Integer, Row As Integer) 现在有什么办法吗?我在 excel 中使用 vba .. 包含 =asd($E$4,$I$4,$M$4,$Q$4,$U$4,$Y$4,$AC$4 的单元格中使用的变量 A1...A6 ,AL3).. 还有其他方法吗?我很抱歉
    • @AllenTheGreat - 我的伪代码不正确或更简单怎么办?您需要做的就是将我的伪代码更改为语法正确,并且您有一个 7 行函数与一个 33 行函数。
    • 好吧,如果出现一些错误,我会尝试更新你...谢谢顺便说一句:)
    【解决方案2】:

    未测试代码,但如果需要,只需稍作修改即可工作

    For i = 0 To 9
        If Str >= Row then
            asd = i 
            Exit for
        End If
        Str = Str + INDIRECT("A" + i + 1) 'assuming A1, A2 etc are cell addresses .. 
    Next i
    

    【讨论】:

    • 先生.. 代码正在执行我需要的东西.. 很抱歉我忘了包括我的函数行.. 这是行函数 asd(A1 As Integer, A2 As Integer, A3 As Integer , A4 As Integer, A5 As Integer, A6 As Integer, Strt As Integer, Row As Integer) 现在有什么办法吗?我在 excel 中使用 vba .. 包含 =asd($E$4,$I$4,$M$4,$Q$4,$U$4,$Y$4,$AC$4 的单元格中使用的变量 A1...A6 ,AL3).. 还有其他方法吗?我的道歉
    【解决方案3】:
    Option Explicit
    Function ASD(byval F as Range, byval E as Range, byval I as Range, _
    byval A as Range, byval Row as Range) As Integer
    
    Dim var
    Dim ctr As Integer
    Dim data As Variant
    
    ctr = 0
    data = Array(F.Value, E.Value, I.Value, A.Value)
    For Each var In data
        If var >= Row.Value Then
           ASD = ctr
           Exit Function
        End If
    
        ctr = ctr + 1
    Next
    ASD = -1
    End Function
    

    【讨论】:

    • 先生.. 代码正在执行我需要的东西.. 很抱歉我忘了包含我的函数行.. 这是行函数 asd(A1 As Integer, A2 As Integer, A3 As Integer , A4 As Integer, A5 As Integer, A6 As Integer, Strt As Integer, Row As Integer) 现在有什么办法吗?我在 excel 中使用 vba .. 包含 =asd($E$4,$I$4,$M$4,$Q$4,$U$4,$Y$4,$AC$4 的单元格中使用的变量 A1...A6 ,AL3).. 还有其他方法吗?我的道歉
    • @AllenTheGreat:看看修改后的版本是否有帮助。
    猜你喜欢
    • 1970-01-01
    • 2012-07-05
    • 1970-01-01
    • 1970-01-01
    • 2018-08-19
    • 1970-01-01
    • 2016-03-26
    • 1970-01-01
    • 2012-04-06
    相关资源
    最近更新 更多