2015-07-02 54 views
0

我收到一个对象定义的错误,下面的代码。 任何想法我可能做错了什么?感谢Excel VBA对象定义错误

Sub Loop_Test2() 

    Dim i As Integer 
    Dim j As Integer 
    Dim CountAll As Integer 
    Dim CountXL As Integer 

    ActiveSheet.Range("A1").Activate 

    CountAll = ActiveSheet.Range("A35") 
    MsgBox CountAll 

    For j = 1 To CountAll 
     i = 1  

这是错误发生:

 CountXL = Cells(i, j).Value 

续:

 MsgBox CountXL 

     For i = 1 To CountXL + 2 
     Cells(i + 2, j) = "Row " & i & " Col " & j 
     Next i 
    Next j 

End Sub 

我认为这是一个不正确的分配。我不熟悉正确的语法。

Error Details: "Run time error 1004. Application defined or object defined error

+0

请发布错误。 – FormigaNinja

+0

嗨。我做了一些测试后修改了这篇文章。看起来错误在“CountXL”分配中。 – Nadz

+0

我认为这是因为我没有初始化“我”。任何想法如何正确地做到这一点?另外我认为我为“CountXL”赋值的方式没有正确完成 – Nadz

回答

0

之前编辑你的问题,你是忘了最初你i。所以刚才设置的值i

将来,您可以在Sub的顶部使用Option Explicit以确保在使用它之前声明该变量。

因此对于您的情况,只需要设置i=1,并且请声明所有变量为long而不是整数。你可以参考here找出使用long而不是整数的原因。

相关问题