programing

매크로 - 폴더의 모든 파일 열기

lovejava 2023. 6. 18. 10:01

매크로 - 폴더의 모든 파일 열기

지정된 폴더의 모든 파일을 열고 다음 코드를 사용합니다.

Sub OpenFiles()
Dim MyFolder As String
Dim MyFile As String
MyFolder = "\\ILAFILESERVER\Public\Documents\Renewable Energy\FiTs\1 Planning
           Department\Marks Tracker\Quality Control Reports"
MyFile = Dir(MyFolder & "\*.xlsx")
Do While MyFile <> ""
Workbooks.Open Filename:=MyFolder & "\" & MyFile
Loop
End Sub

문제는 폴더의 첫 번째 파일을 계속해서 열려고 하고 계속 진행되지 않는다는 것입니다.누가 도와줄 수 있나요, 저는 VBA의 초보자라서 정말 도움이 필요합니다..xlsx 형식의 보고서를 30개 정도 열려고 합니다.미리 감사 드려요.

이 라인을 직전에 추가해야 합니다.loop

    MyFile = Dir
Loop

사용할 수 있습니다.Len(StrFile) > 0루프 확인 문에서!

Sub openMyfile()

    Dim Source As String
    Dim StrFile As String

    'do not forget last backslash in source directory.
    Source = "E:\Planning\03\"
    StrFile = Dir(Source)

    Do While Len(StrFile) > 0                        
        Workbooks.Open Filename:=Source & StrFile
        StrFile = Dir()
    Loop
End Sub

다음 코드를 사용해 보십시오.

Sub opendfiles()

Dim myfile As Variant
Dim counter As Integer
Dim path As String

myfolder = "D:\temp\"
ChDir myfolder
myfile = Application.GetOpenFilename(, , , , True)
counter = 1
If IsNumeric(myfile) = True Then
    MsgBox "No files selected"
End If
While counter <= UBound(myfile)
    path = myfile(counter)
    Workbooks.Open path
    counter = counter + 1
Wend

End Sub

언급URL : https://stackoverflow.com/questions/11152870/macro-open-all-files-in-a-folder