On Error goto <label> (incorrectly works)

孤街醉人 提交于 2019-12-12 04:35:49

问题


When “run-time error 1004” occurs I want the program to skip a part of the code and work further . With the first variable it works normally , but with the second “run-time error 1004” again pops up.

    Sub связатьцифирь14()
    Dim book1 As Workbook
    Dim book2 As Workbook
    Dim r As Range
    Dim firstAddress As String
    Dim n, m, e, t As Long
    Dim A()
    Dim B()
    Dim D()
    Dim F()
    Dim G As String
    Dim H As String
    Dim intBe As Integer
    Dim varValue1 As Variant
    Dim varValue2 As Variant




    'сезон (прошлый,допрошлый,додопрошлый)
    A = Array("прошлый", "допрошлый", "додопрошлый")
    'страна
    D = Array("Англия", "Испания", "Италия", "Германия", "Голландия", "Франция", "Португалия", "Россия", "Украина")
   'место (1,2,3,4,5,6)
    B = Array("1", "2", "3", "4", "5", "6")
    'номер условия
    F = Array("6", "7", "8", "16", "17", "21", "22", "23", "25", "26", "29", "30", "31", "54", "55", "56", "57", "58", "59")
   'Общ п/пр
    G = "AP100"
    'Общ
    H = "AO100"

    Set book1 = Workbooks.Open("E:\Super M\Проект ставки\Решение\цифирь.xlsx")

    'переходим в активную страницу откуда надо скопировать данные'
    'массив условие
     For t = 0 To 18
    'массив страна
     For m = 0 To 8
    'массив сезон
    For n = 0 To 2
    'массив место
    For e = 0 To 5


   'пропускаем ошибку
   On Error GoTo Label1
   Set book2 = Workbooks.Open("E:\Super M\Проект ставки\Поиск решения\Усов 4\БАЗА ДАННЫХ\" & A(n) & " сезон\" & D(m) & "\" & B(e) & "-ое место\" & F(t) & ".xlsx")

   intBe = CInt(B(e)) + 5
   varValue1 = book2.Worksheets("" & F(t) & "").Range("" & G & "").Value
   varValue2 = book2.Worksheets("" & F(t) & "").Range("" & H & "").Value
   'ищемс
   With book1.Worksheets("Лист1").Range("A1:CV808")

    Set r = .Find(What:="" & F(t) & "", LookAt:=xlWhole, LookIn:=xlValues)
    If Not r Is Nothing Then
        firstAddress = r.Address
        Do
         'условие для страны
         'прошлый сезон
         If r.Offset(3, 0).Text = D(m) And A(n) = "прошлый" Then
         r.Offset(intBe, 5).Value = varValue1
         r.Offset(intBe, 6).Value = varValue2
         End If
         'допрошлый сезон
        If r.Offset(3, 0).Text = D(m) And A(n) = "допрошлый" Then
        r.Offset(intBe, 3).Value = varValue1
        r.Offset(intBe, 4).Value = varValue2
        End If
         'додопрошлый сезон
         If r.Offset(3, 0).Text = D(m) And A(n) = "додопрошлый" Then
        r.Offset(intBe, 1).Value = varValue1
        r.Offset(intBe, 2).Value = varValue2
        End If

        Set r = .FindNext(r)

        Loop While Not r Is Nothing And r.Address <> firstAddress
    End If
    End With
    book2.Close
    Next e
   book1.Save
   Label2:
   Next n
   Next m
   Next t
   book1.Save
   book1.Close
   Exit Sub
   Label1:
   On Error Resume Next
   GoTo Label2
   End Sub  

回答1:


You are not exiting from your error-handler correctly - you are using a GoTo instead of a Resume. Because you were effectively still within the error-handling code when you encountered the second error, the error was not able to be trapped.

Therefore, change

GoTo Label2

to

Resume Label2


来源:https://stackoverflow.com/questions/44364013/on-error-goto-label-incorrectly-works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!