VBA to insert a row/data when a new pdf has been added to a folder


New Member
Aug 8, 2013
Hello everybody!

I'm currently using the below code to add a file from a folder location and hyperlink it to an excel sheet.

Option Compare TextOption Explicit
Function Excludes(Ext As String) As Boolean
     'Function purpose:  To exclude listed file extensions from hyperlink listing
    Dim X, NumPos As Long
     'Enter/adjust file extensions to EXCLUDE from listing here:
    X = Array("exe", "bat", "dll", "zip", "xlsm")
    On Error Resume Next
    NumPos = Application.WorksheetFunction.Match(Ext, X, 0)
    If NumPos > 0 Then Excludes = True
    On Error GoTo 0
End Function
Sub HyperlinkFileList()
     'Macro purpose:  To create a hyperlinked list of all files in a user
     'specified directory, including file size and date last modified
     'NOTE:  The 'TextToDisplay' property (of the Hyperlink object) was added
     'in Excel 2000.  This code tests the Excel version and does not use the
     'Texttodisplay property if using XL 97.
    Dim fso As Object, _
    ShellApp As Object, _
    File As Object, _
    SubFolder As Object, _
    Directory As String, _
    Problem As Boolean, _
    ExcelVer As Integer
    Dim OpenPosition As Integer
    Dim ClosePosition As Integer
    Dim i As Integer
    Dim j As Integer
     'Turn off screen flashing
    Application.ScreenUpdating = False
     'Create objects to get a listing of all files in the directory
    Set fso = CreateObject("Scripting.FileSystemObject")
     'Prompt user to select a directory
        Problem = False
        Set ShellApp = CreateObject("Shell.Application"). _
        Browseforfolder(0, "Please choose a folder", 0, "c:\\") 'change this to specific job folder
        On Error Resume Next
         'Evaluate if directory is valid
        Directory = ShellApp.self.Path
        Set SubFolder = fso.GetFolder(Directory).Files
        If Err.Number <> 0 Then
            If MsgBox("You did not choose a valid directory!" & vbCrLf & _
            "Would you like to try again?", vbYesNoCancel, _
            "Directory Required") <> vbYes Then Exit Sub
            Problem = True
        End If
        On Error GoTo 0
    Loop Until Problem = False

     'Adds each file, details and hyperlinks to the list
    For Each File In SubFolder
        If Not Excludes(Right(File.Path, 3)) = True Then
            With Worksheets("MASTER")
                 'If Excel 2000 or greater, add hyperlink with file name
                 'displayed.  If earlier, add hyperlink with full path displayed
                If Val(Application.Version) > 8 Then 'Using XL2000+
                    .Hyperlinks.Add _
                    anchor:=Worksheets("MASTER").Range("A65536").End(xlUp).Offset(1, 0), _
                    Address:=File.Path, _
                    TextToDisplay:=Split(File.Name, "-")(0)
                Else 'Using XL97
                    .Hyperlinks.Add _
                    anchor:=Worksheets("MASTER").Range("A65536").End(xlUp).Offset(1, 0), _
                End If
                 'Add date last modified, and size in KB
                With .Range("A65536").End(xlUp)
                    .Offset(0, 4) = File.DateCreated    'file created
                    With .Offset(0, 2)
OpenPosition = InStr(File.Name, "-")
ClosePosition = InStr(File.Name, ".pdf")
On Error Resume Next
                        .Value = Mid(File.Name, OpenPosition + 1, ClosePosition - OpenPosition - 1) 'naming value for the description
                        With .Offset(0, -1)
                            .Value = Split(File.Name, " ")(0)

                        End With
                    End With
                End With
            End With
        End If
End Sub

In short, it adds files, then hyperlinks them to Column A, adds parts of file name in columns B, C, and E that can be used by a user to search for specific pdf's. From the file information, the user can manually add comments into Column I for future information. Everything works except when it comes to the commenting part; once another file is added to the folder, the comments to the pdf don't align. I'm hoping that there would be a way to insert a row and add the new files when they have been added, thus the rest of the information in Column I would align with the pdf.

I've looked for a week or so and tweaked code that I've found here and on different websites to see if I can get it to work, but have not been successful. I'm hoping that someone has a better idea, or can add to this code!

Thank you in advance!

Some videos you may like

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

Watch MrExcel Video

Forum statistics

Latest member
Mohan Kumar

This Week's Hot Topics