【问题标题】:Remove Goto statements删除 Goto 语句
【发布时间】:2019-06-21 09:45:22
【问题描述】:

我需要优化代码,我想删除 goto by 循环,但我对建议的循环和 VBA 语法有点麻烦!

    Dim a As Integer, b As Integer

a = 2

line1: 'Goto line
Workbooks("0-base de données.xlsx").Worksheets(1).Activate

If Cells(a, 1) <> "" Then
    b = a
 line2: 'Goto line
    If Cells(a, 18) = Cells(a + 1, 18) Then
        If Cells(a, 23) <> "" And Cells(a, 23) <= Date And Cells(a, 256) = 1 Then
            a = a + 1
            GoTo line2
        Else
    line3: 'Goto line
            If Cells(a, 18) = Cells(a + 1, 18) Then
                a = a + 1
                GoTo line3
            Else:
                a = a + 1
                GoTo line1
            End If
        End If
    Else
        If Cells(a, 23) <> "" And Cells(a, 23) <= Date And Cells(a, 256) = 1 Then

            Workbooks("0-base de données.xlsx").Worksheets(1).Range("1:2").Copy
            Workbooks("0-base de données1.xlsx").Worksheets(1).Activate
            D = 2
line5: 'Goto line
            If Cells(D, 1) <> "" Then
                D = D + 1
                GoTo line5
            End If
            Cells(D, 1).Select
            ActiveSheet.Paste
            Windows("0-base de données.xlsx").Activate
            Rows(b & ":" & a).Select
            Selection.Delete Shift:=xlUp
            a = b
            GoTo line1
        End If
        a = a + 1
        GoTo line1
    End If
End If

我已经阅读了很多帖子,但我没有找到任何匹配的代码:(

提前致谢

【问题讨论】:

  • 需要优化的工作代码属于Code Review,我建议你在那里发布你的问题。

标签: excel vba loops goto


【解决方案1】:

好吧,那太乱了!

不打算做所有事情,因为我真的觉得它很难阅读,但作为一个 sn-p:

D = 2
line5: 'Goto line
If Cells(D, 1) <> "" Then
    D = D + 1
    GoTo line5
End If

应该是

D = 2
Do Until Cells(D,1)<>""       
    D = D + 1
Loop

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多