简体   繁体   中英

VBA error 1004 in do until loop

I'm pretty new to VBA, I've been succesfull before in programming loops, however, this time I can't seem to find the solution to make this code work. The goal is to assemble data from one sheet (divided over 400+ seperate tables) to another sheet.

The program works without the outer loop, but when coding the loop around the other two loops I constantly get the error "run-time error '1004': Application-defined or object-defined error.

Dim x As String
Dim HStop As String
Dim y As String
Dim VStop As String
Dim z As Integer
Dim Row As Integer
Dim Column As Integer
Dim Counter As Integer

Sub Reform()

'1 maal te doen:

Row = 10
Column = 1
Counter = 6

'begin sub
Do

HStop = Worksheets("Find").Cells(2, 3).Value
VStop = Worksheets("Find").Cells(3, 3).Value

x = 1
y = 1

Do

Column = Column + 1
x = Worksheets("retour").Cells(Row, Column).Value


Loop Until x = HStop

'Checkpoint
Worksheets("Find").Cells(2, 12).Value = x

Do

Row = Row + 1
y = Worksheets("retour").Cells(Row + 7, 2).Value
Worksheets("Find").Cells(Counter, 12).Value = Worksheets("retour").Cells(Row, Column).Value
Counter = Counter + 1

Loop Until y = VStop

'Checkpoint
Worksheets("Find").Cells(3, 14).Value = y
Worksheets("Find").Cells(2, 14).Value = Row
Worksheets("Find").Cells(3, 14).Value = Column

z = z + 1

Loop Until z = 10

End Sub

Can someone help me?

Well, not sure how this code will help you to get desired result, however the error occurs because in the following peice of code

Do
    HStop = Worksheets("Find").Cells(2, 3).Value
    VStop = Worksheets("Find").Cells(3, 3).Value
    x = 1
    y = 1
    Do
        Column = Column + 1
        x = Worksheets("retour").Cells(Row, Column).Value
    Loop Until x = HStop
    'Checkpoint

HStop is assigned a value, then in the loop you are assigning another value to x and looping till x = HStop . In Do loop you are also incrementing the value of Column . In case, x is not equal to HStop and Column value reaches 16385 ie one more than the last column of the sheet, it throws runtime error while assigning

x = Worksheets("retour").Cells(Row, 16385).Value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM