2016-02-27 63 views
-1

如上所述,當循環目錄時,我得到一個對象所需的錯誤。有趣的是它被宣佈。當我開始循環瀏覽一個文件夾而不僅僅是一張工作表時,我纔開始出現這個問題。我試圖激活工作簿,但這似乎也沒有工作。我感動了一下,看看編譯器是否挑剔,什麼都沒有。也許一個新的眼睛會幫助我找出我的錯誤?由於當通過DIR進行循環時,要求的對象錯誤()

代碼:

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 
+2

提示:'Count'不是一個對象:'設置ws = ActiveWorkbook.Worksheets.Count'。看起來你改變它的使用後保留了舊的變量名。 –

+0

Ahhhh我明白了。如果我將「set」從那行中除去,那麼一切正常。......除了循環代碼不實際執行。我多玩一些,試着找出原因。 –

回答

0

採取從集合WS = ActiveWorkbook.Worksheets.Count

和解決所需要的對象的問題設置了。

ALSO

事實證明:

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

應該

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

我似乎總是錯過O_O

+0

不挑剔,但這不是對您的問題的回答,這是關於「Object Required」錯誤的答案。 –

+0

好吧,你可以說出你的原始評論作爲答案,並得到代表....至少我關心任何剩餘的問題,以防萬一別人關心。 –