Searching for files with dir - Multiple hits

前端 未结 1 807
小蘑菇
小蘑菇 2021-01-25 17:25

I have a macro that iterate through folders and use the \"dir\"-function to find out if a file exist in the active folder, and puts the filename in a cell.

The problem i

相关标签:
1条回答
  • 2021-01-25 17:41

    How are you using DIR. The below code will give you all the files which start with Kommunesvar and are Excel Files.

    Option Explicit
    
    Sub Sample()
        Dim subfolder As String
        Dim sDir
    
        subfolder = "C:\Temp"
    
        sDir = Dir$(subfolder & "\Kommunesvar*.xls*", vbNormal)
    
        Do Until LenB(sDir) = 0
            Debug.Print subfolder & sDir
            sDir = Dir$
        Loop
    End Sub
    

    If you want to store all the files names in one variable then you can use this as well.

    Sub Sample()
        Dim subfolder As String, FileNames As String
        Dim sDir
    
        subfolder = "C:\Temp"
    
        sDir = Dir$(subfolder & "\Kommunesvar*.xls*", vbNormal)
    
        Do Until LenB(sDir) = 0
            If FileNames <> "" Then
                FileNames = FileNames & "; " & subfolder & sDir
            Else
                FileNames = subfolder & sDir
            End If
            sDir = Dir$
        Loop
    
        Debug.Print FileNames
    End Sub
    
    0 讨论(0)
提交回复
热议问题