2014-09-11 2 views
0

죄송합니다. 코드 태그가 작동하지 않으면 언젠가는 스택 오버플로에 있지 않았습니다. 어쨌든 여기에 제가 가지고있는 문제가 있습니다. 나는 어떤 일이 일어나고 있는지 잘 알고 있지만 도움을받을 수는 있습니다. 아래의 코드는 기본적으로 컴퓨터의 다른 통합 문서에 파일 경로를 저장하는 Excel 통합 문서를 열어 배열로 배치합니다. 배열은 내가 사용하는 디버그 메시지 상자에 의해 데이터를 적절히 저장하고 있지만 "Set tempBook = filePathArray (i, 1)"행에 도착하면 "424 Object Required"오류가 발생합니다. 이제 Set tempBook 행에서 찾고있는 배열의 항목이 파일 경로를보고 있다는 사실을 알고 있습니다. 필자의 이론은 배열에 저장된 정보가 Set tempBook이 문자열을 파일 경로로 인식하지 못하게하는 특별한 형식을 가질 수 있다는 것입니다. 죄송합니다. 이것이 정말 간단하다면, VBA로 작업 한 것은 이번이 처음이고 매크로는 Java와 C#을 많이 선호합니다. 어쨌든 남자들과 여자들은 큰 도움을 청했습니다. 또한 보안을 위해 게시하기 전에 filePathBook에 대한 파일 경로 정보를 변경합니다.배열 구조에서 파일 경로를 가져 오는 VBA-Excel

가 요약하자면 :

가 내 바탕 화면에 통합 문서 파일 경로 정보를 검색하고 배열로 저장하고있어, 해당 통합 문서의 각 셀은

내가 '통합 문서에 전체 파일 경로를 들고 그런 다음 루프를 실행하면서 루프를 실행하는 루프를 실행하고 각 파일 경로를 개별적으로 가져오고 데이터를 가져온 다음 배열의 다음 파일 경로로 다시 수행합니다.

내가 얻는 오류는 배열에서 첫 번째 파일 경로를 가져 와서 통합 문서로 설정된 변수에 배치하려고하면 "424 개체 필요"오류가 발생합니다. .

보고있는 위치가 적절한 파일 경로 정보를 포함하고 있으므로 디버깅 메시지 상자가 있습니다. 그러나 배열 서식이 문제의 원인 일 수 있다고 생각합니다.

이 문제를 완화하는 데 도움을 주시면 큰 도움이 될 것입니다. 여기

Sub get_data_from_file() 

Dim actBook As Workbook   'active workbook 
Dim filePathBook As Workbook 'filepath workbook 

Dim pasteCounter As Integer  'paste counting variable in loop 
Dim counter As Integer   'loop counter 


'This sets the workbook the macro is in to be the active workbook to paste too 
Set actBook = ActiveWorkbook 

'Turn off screen update to speed up macro and make it not seem like the screen flashes 
Application.ScreenUpdating = False 

'set the filePathBook to point to the workbook storing the file paths for other books 
Set filePathBook = Workbooks.Open("C:directory info\filePathBook") 



Dim filePathArray As Variant 'declare array 

'retrieve data from range cells and store in array, these are the file paths for other books 
filePathArray = filePathBook.Sheets("Sheet1").Range("a1:a2").Value 

'Save and close filePathBook 
filePathBook.Save 
filePathBook.Close 

pasteCounter = 1 'initialize paste counter variable, it's used to move cell paste locations 

'declare another workbook to use as the temporary variable in the loop to open and retrieve info from each workbook in the array 
Dim tempBook As Workbook 

'Looping structure to look at array and perform functions. 
For i = 1 To UBound(filePathArray) 


    MsgBox filePathArray(i, 1)      'Debugging purposes: files are being stored properly 


    Set tempBook = filePathArray(i, 1)    'get first workbook filepath and store in tempBook 
    tempBook.Sheets("Sheet1").Range("a1:a4").Copy 'Copy cells a1:a4 from workbook 

    actBook.Sheets("Sheet1").Activate    'Activate current book, this ensures it is always active in each run of the loop 
    ActiveSheet.Cell(a, pasteCounter).Select  'Select proper cell to paste values down from 

    Selection.PasteSpecial Paste:=xlPasteValues  'Paste Values 

    pasteCounter = pasteCounter + 4  'increment paste counter to select cells below pasted cells each iteration 

    'save and close tempBook 
    tempBook.Save 
    tempBook.Close 
Next i 

'Turn screen updating back on 
Application.ScreenUpdating = True 

End Sub 

답변

1
Set tempBook = filePathArray(i, 1) 

통합 문서 객체

Set tempBook = Workbooks.Open(filePathArray(i, 1)) 

에 변형/문자열을 할당하려는 당신이

을 필요 가능성이 높습니다
관련 문제