문제

그래서 내가 만들려고 목록을 엑셀 파일의 폴더에(파일 이름 및 경로)그리고 그 사용을 위한 루프를 복사하고 붙여넣는 지정된 워크시트에 나열된 모든 파일로 지정된 워크시트 excel 통합 문서에 포함된 매크로입니다.지금까지 모든 작동을 제외한다는 사실 같은 파일 복사 및 붙여는 대신 모든 파일이 있습니다.매크로는 루프에 대한 올바른 시간의 수,그러나 그것의 사용하지 않는 모든 excel 파일이 있습니다.

여기에는 코드:

첫 번째 부분에 대한 목록에서 파일을 폴더

Private Sub btn_LeaveReport()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer

'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the folder object
Set objFolder = objFSO.GetFolder("D:\Administration\Time Sheets")
i = 2
'loops through each file in the directory and prints their names and path
For Each objFile In objFolder.Files
    'print file name
    Cells(i + 1, 2) = objFile.Name
    'print file path
    Cells(i + 1, 3) = objFile.Path
    i = i + 1
Next objFile
End Sub

그리고 이것은 부분에 대한 반복

Private Sub btn_PullData()
'Declared Variables
Dim wbk As Workbook
Dim i As Integer
Dim StartAt As Integer
Dim EndAt As Integer
Dim CopyPath As String
Dim CopyPathRow As Integer
Dim iRow As Integer

'Ranges
StartAt = 1
EndAt = Val(ThisWorkbook.Worksheets("LeaveReport").Range("A1"))
CopyPathRow = 3
CopyPath = ThisWorkbook.Worksheets("LeaveReport").Range("C" & CopyPathRow)
iRow = 3

'Loop de loop
For i = StartAt To EndAt
    Application.ScreenUpdating = False
    Set wbk = Workbooks.Open(CopyPath)
    Sheets("TIMESHEET").Select
    Range("C12:S34").Select
    Selection.Copy

    ThisWorkbook.Activate
    Sheets("Pastebin").Select
    Range("a" & iRow).PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False

    iRow = iRow + 39
    CopyPathRow = CopyPathRow + 1
    wbk.Close True
Next i

Sheets("Pastebin").Select
Range("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Application.ScreenUpdating = True
MsgBox "Timesheet Data Imported"
End Sub

기반의 소스에 오류가,i.e같은 파일에 사용되는,나의 문제로 자리 잡고있는 부분이:

CopyPath=ThisWorkbook.워크시트("LeaveReport").범위("C"&CopyPathRow)

며"해야하는"업데이트에 대한 반복을 통해 이:

CopyPathRow=CopyPathRow+1

도움이 되었습니까?

해결책

CopyPath = ThisWorkbook.Worksheets("LeaveReport").Range("C" & CopyPathRow)

루프 안에서 해당 값의 CopyPath 은 결코 변경되지만,의 가치 CopyPathRow 입니다.

편집:저는 이 재귀 하나.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top