macro - open all files in a folder

前端 未结 3 1371
小鲜肉
小鲜肉 2020-12-08 11:24

I want to open all files in a specified folder and have the following code

Sub OpenFiles()
Dim MyFolder As String
Dim MyFile As String
MyFolder = \"\\\\ILAFI         


        
相关标签:
3条回答
  • 2020-12-08 11:35

    You have to add this line just before loop

        MyFile = Dir
    Loop
    
    0 讨论(0)
  • 2020-12-08 11:38

    Try the below code:

    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
    
    0 讨论(0)
  • 2020-12-08 11:46

    You can use Len(StrFile) > 0 in loop check statement !

    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
    
    0 讨论(0)
提交回复
热议问题