VBA错误1004直到循环

时间:2017-09-01 08:06:30

标签: excel-vba loops vba excel

我对VBA很新,我在编程循环之前已经成功了,但是,这次我似乎无法找到使这段代码有效的解决方案。 目标是将数据从一张纸(分成400多个单独的表)分组到另一张纸上。

程序在没有外部循环的情况下工作,但是当围绕其他两个循环编写循环时,我不断得到错误“运行时错误'1004':应用程序定义的错误或对象定义错误。

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

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

好吧,不确定此代码如何帮助您获得所需的结果,但是因为在下面的代码中出现错误

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被分配了一个值,然后在循环中,您将另一个值分配给x并循环到x = HStop。在Do循环中,您还会增加Column的值。如果x不等于HStopColumn值达到16385,即比工作表的最后一列多一个,则在分配时会抛出运行时错误

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