删除Goto语句

时间:2019-06-21 09:45:22

标签: excel vba loops goto

我需要优化代码,我想删除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

我读了很多文章,但没有找到任何匹配的代码:(

预先感谢

1 个答案:

答案 0 :(得分:1)

好,那太乱了!

因为我真的很难读,但不能摘录全部内容,只是摘录:

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