Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I add a list of files to a table column with a specific extension and name?

I need to add a list of files from a folder to a column in the table.
The folder is selected by the user. The list of files that should be displayed starts with "EAN" and ends with "notforprint.pdf, if there are no such files, then highlight the cell in red.
My program is in VBA, I work in excel.
The problem is that I do not know how to select from the list exactly the files that should be displayed start with "EAN" and end with "notforprint.pdf.

I wrote the following code:

Sub CommandButton1_Click()
    Dim V As String
    Dim BrowseFolder As String
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Chose folder"
        .Show
        On Error Resume Next
        Err.Clear
        V = .SelectedItems(1)
        If Err.Number <> 0 Then
            MsgBox "You didnt choose!"
            Exit Sub
        End If
    End With
    BrowseFolder = CStr(V)
    'ActiveWorkbook.Sheets.Add
    With Range("A1:E1")
        .Font.Bold = True
        .Font.Size = 12
    End With
    Range("A1").Value = "Name"
    'Range("B1").Value = "pwd"
    Range("B1").Value = "size"
    'Range("D1").Value = "date begin"
    'Range("E1").Value = "date edit"
    ListFilesInFolder BrowseFolder, True
End Sub
Private Sub ListFilesInFolder(ByVal SourceFolderName As String, ByVal IncludeSubfolders As Boolean)
    Dim FSO As Object
    Dim SourceFolder As Object
    Dim SubFolder As Object
    Dim FileItem As Object
    Dim r As Long
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set SourceFolder = FSO.getfolder(SourceFolderName)
    r = Range("A65536").End(xlUp).Row + 1 '
    '
    For Each FileItem In SourceFolder.Files
        'MsgBox FileItem
        'pdfFiles = Application.GetOpenFilename("PDF Files (*.pdf), *.pdf")
        'If pdfFiles <> False Then
        'MsgBox "-" & pdfFiles
        'Else
        'MsgBox "+" & pdfFiles
        'End If
        MsgBox FileItem.Name

        Cells(r, 1).Formula = FileItem.Name
        'Cells(r, 2).Formula = FileItem.Path
        Cells(r, 2).Formula = FileItem.Size
        'Cells(r, 4).Formula = FileItem.DateCreated
        'Cells(r, 5).Formula = FileItem.DateLastModified
        r = r + 1
        X = SourceFolder.Path
    Next FileItem
    '
    If IncludeSubfolders Then
        For Each SubFolder In SourceFolder.SubFolders
            ListFilesInFolder SubFolder.Path, True
        Next SubFolder
    End If
    Columns("A:B").AutoFit
    Set FileItem = Nothing
    Set SourceFolder = Nothing
    Set FSO = Nothing
End Sub

As you can see, it outputs the entire list of files that are displayed in a folder and subfolders.
Using the MsgBox method, I tried to output information for each file in order to somehow highlight them.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Without making any major changes to your code, you can add an IF to the Each loop:

For Each FileItem In SourceFolder.Files
    If FileItem.Name Like "EAN*notforprint.pdf" Then
        MsgBox FileItem.Name

        Cells(r, 1).Formula = FileItem.Name
        Cells(r, 2).Formula = FileItem.Size
        r = r + 1
    End If
Next FileItem
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading