+ Reply to Thread
Results 1 to 2 of 2

File name extraction w/hyperlinks no duplicates.

Hybrid View

  1. #1
    Registered User
    Join Date
    04-14-2013
    Location
    Ontario
    MS-Off Ver
    Excel 2010
    Posts
    1

    File name extraction w/hyperlinks no duplicates.

    Hey all, new to forum posting but here we go. I would like to be able to extract all file names from a given folder to a spread sheet that would also create hyperlinks to the files extracted. Also if possible when the script is run for a second time, lets say to update that it would only add new files to the spread sheet. I have tried a macro script file that allows you to select files and it extracts the file names but have no idea as to get it to hyperlink said files and not duplicate pre-existing files. I would appreciate any insight.

    Here is a copy of the macro I am currently using.

    Option Explicit

    Private Sub CommandButton1_Click()
    Dim rngSave As Range
    Dim lngCount As Long
    Dim strPathFile As String
    Dim strFname As String
    Dim intLastDiv As Integer
    Dim n As Integer

    On Error GoTo ErrHnd

    With ActiveSheet
    'find the last used cell in column A
    Set rngSave = .Range("A65534").End(xlUp)

    ' Open the file dialog
    With Application.FileDialog(msoFileDialogFilePicker)
    .AllowMultiSelect = True
    .Show
    'Copy all path/filenames into column A
    For lngCount = 1 To .SelectedItems.Count
    strPathFile = .SelectedItems(lngCount)
    'find last "/" in path/filename
    intLastDiv = 0
    For n = 1 To Len(strPathFile)
    If Mid(strPathFile, n, 1) = "\" Then
    intLastDiv = n
    End If
    Next n
    rngSave.Offset(lngCount - 1, 0) = strPathFile
    'split filename from path
    'save path in column A
    rngSave.Offset(lngCount - 1, 0).Value = Left(strPathFile, intLastDiv)
    'save filename in column B
    rngSave.Offset(lngCount - 1, 1).Value = Right(strPathFile, Len(strPathFile) - intLastDiv)
    Next lngCount
    End With
    'set column widths to fit
    .Range("A1:B" & Format(rngSave.Row + lngCount, "##0")).Columns.AutoFit
    End With
    Exit Sub

    'error handler
    ErrHnd:
    Err.Clear
    End Sub

  2. #2
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: File name extraction w/hyperlinks no duplicates.

    Hi,

    Option Explicit
    
    Private Sub CommandButton1_Click()
    Dim rngSave As Range
    Dim lngCount As Long
    Dim strPathFile As String
    Dim strFname As String
    Dim intLastDiv As Integer
    Dim n As Integer
    
    Dim xlRng As Range
    Dim strPath As String
    Dim strFileName As String
    Dim strFirstAddr As String
    Dim blnDuplicate As Boolean
    
    'On Error GoTo ErrHnd
    
        With ActiveSheet
            'find the last used cell in column A
            Set rngSave = .Range("A" & .Range("A65534").End(xlUp).Row + 1) 'otherwise you are overwriting the last row
            
            ' Open the file dialog
            With Application.FileDialog(msoFileDialogFilePicker)
                .AllowMultiSelect = True
                .Show
            End With
            
            'Copy all path/filenames into column A
            For lngCount = 1 To Application.FileDialog(msoFileDialogFilePicker).SelectedItems.Count
                
                strPathFile = Application.FileDialog(msoFileDialogFilePicker).SelectedItems(lngCount)
                intLastDiv = InStrRev(strPathFile, "\") 'faster
                strPath = Left(strPathFile, intLastDiv)
                strFileName = Right(strPathFile, Len(strPathFile) - intLastDiv)
                
                blnDuplicate = False
                Set xlRng = .Columns(2).Find(what:=strFileName, LookIn:=xlValues, lookat:=xlWhole)
                If Not xlRng Is Nothing Then
                    strFirstAddr = xlRng.Address
                    Do
                        If .Range("A" & xlRng.Row).Value = strPath Then
                            blnDuplicate = True
                            Exit Do
                        End If
                        
                        Set xlRng = .Columns(2).FindNext(After:=xlRng)
                    Loop Until xlRng Is Nothing Or xlRng.Address = strFirstAddr
                End If
    
                If Not blnDuplicate Then
                    'save path in column A
                    rngSave.Offset(lngCount - 1, 0).Value = strPath
                    'save filename in column B
    '                rngSave.Offset(lngCount - 1, 1).Value = strFileName
                    'hyperlink
                    .Hyperlinks.Add Anchor:=rngSave.Offset(lngCount - 1, 1), _
                        Address:=strPath & "\" & strFileName, _
                        TextToDisplay:=strFileName
                End If
                
            Next lngCount
     
            'set column widths to fit
            .Range("A1:B" & rngSave.Row + lngCount).Columns.AutoFit
        End With
        Exit Sub
    
    'error handler
    ErrHnd:
    
    Err.Clear
    End Sub
    Last edited by tehneXus; 04-14-2013 at 08:04 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1