通过DIR循环时所需的对象错误()

时间:2016-02-27 23:45:47

标签: excel vba loops

如上所述,循环浏览目录时,我收到了一个对象必需的错误。有趣的是它被宣布。当我开始循环浏览文件夹而不仅仅是工作表时,我才开始遇到这个问题。我尝试激活工作簿,但这似乎也没有用。我动了一下,看看编译器是否挑剔,什么都没有。也许一双新鲜的眼睛会帮助我查明我的错误?感谢

代码:

 Sub MultiDimArray()

 Dim Z As Long
 Dim A1 As Long
 Dim ws As Integer
 Dim wbk As Workbook
 Dim Filename As String
 Dim Path As String
 Dim myArray(9, 5) As String

 Path = "C:\Users\Ashleysaurus\Desktop\doug\test\TheFolder"
 Filename = Dir(Path & "*.xlsx")

 Do While Len(Filename) > 0
     Set wbk = Workbooks.Open(Path & Filename)
     wbk.Activate
     Set ws = ActiveWorkbook.Worksheets.Count
     For i = 1 To ws
         For x = LBound(myArray, 1) To UBound(myArray, 1)
             For y = LBound(myArray, 2) To UBound(myArray, 2)
                     myArray(x, y) = "Position " & "x=" & x & ", y=" & y & ", z=" & Z & ", A1=" & A1
                     ActiveWorkbook.Worksheets(i).Cells(x + 1, y + 1).Value = myArray(x, y)
             Next y
         Next x
         Z = Z + 1
     Next i
     wbk.Close True
     x = 0
     y = 0
     Z = 0
     A1 = A1 + 1
     Filename = Dir
 Loop
 End Sub

1 个答案:

答案 0 :(得分:0)

从Set ws = ActiveWorkbook.Worksheets.Count

中取出Set

并解决了所需的对象问题。

ALSO

原来:

 Path = "C:\Users\Ashleysaurus\Desktop\doug\test\TheFolder"

应该是

 Path = "C:\Users\Ashleysaurus\Desktop\doug\test\TheFolder" & "\"

我似乎总是想念那个O_o