로크 번호가 여러 개인 폴더가 있습니다 (.xls
).VBA : 여러 통합 문서의 특정 범위를 하나의 워크 시트로 복사
각 파일의 워크 시트 중 하나에서 특정 범위를 큰 워크 시트로 복사해야합니다.이 워크 시트는 향후 분석을위한 데이터 파일입니다.
매크로를 작성하려고했지만 오류가 계속 발생했습니다. ...의 I는 항상이 같은 작업에 사용하는 다음 사항을 고려
Sub ProcessFiles()
' declarations & definitions
Dim Pathname As String
Dim Filename As String
Dim sourceWB As Workbook
Dim targetWB As Workbook
targetWB = ActiveWorkbook
Pathname = ActiveWorkbook.Path & "\Files\"
Filename = Dir(Pathname & "*.xls")
' loop through all files in folder
Do While Filename <> ""
Set sourceWB = Workbooks.Open(Pathname & Filename)
' unlock worksheets
sourceWB.Sheets(4).Visible = True
sourceWB.Sheets(4).Unprotect Password:="Password"
sourceWB.Sheets(2).Unprotect Password:="Password"
' create new worksheet
sourceWB.Sheets.Add After:=8
' copy required cells to new sheets
sourceWB.Sheets(2).Range("A14:FM663").Copy Destination:=sourceWB.Sheets(9).Range("C2")
' fill columns for all rows
sourceWB.Sheets(9).Range("A2:A663").Value = sourceWB.Name
sourceWB.Sheets(9).Range("B2:B663").Value = Worksheets(4).Range("C13").Value
'move AuxSheet to taget workbook
sourceWB.Sheets(9).Move Before:=Workbooks(targetWB).Sheets(1)
'add to full data worksheet
targetWB.Sheets(1).Range("A2:FO651").Copy Destination:=sourceWB.Sheets(2).Rows("3:" & Worksheets("Sheet2").UsedRange.Rows.Count)
'close file and repeat
sourceWB.Close SaveChanges:=False
Filename = Dir()
Loop
' save result
targetWB.Save
End Sub
열린 통합 문서'sourceWB'의 'Sheets (4)'와'Sheets (2)'는 무엇입니까? 대답이 '예'라면,'sourceWB.Sheets (4)'에 의해 그것들을 한정하거나,'.Sheets (4)'와'.Sheets (2) '를 사용하여 처음부터'With sourceWB' 문을 추가해야합니다.) ' –
정확합니다. 한정자를 추가했지만 여전히 errot 91, 개체 변수가 설정되지 않았습니다. – BoTz
디버그 모드에서 실행할 때 어떤 줄이 있습니까? –