Jack Gillson
Jack Gillson

Reputation: 1

Excel VBA Filename Search Return Full Path

Sub Sample()
Dim sh As Worksheet
Dim rng As Range
Dim i As Long, Lrow As Long
Dim fPath As String, sPath As String

With Application.FileDialog(msoFileDialogFolderPicker)
    .Show
    fPath = .SelectedItems(1)
End With

If Right(fPath, 1) <> "\" Then
    fPath = fPath & "\"
End If

Set sh = ThisWorkbook.Sheets("Sheet1")

With sh
    Lrow = .Range("A" & .Rows.Count).End(xlUp).Row

    For i = 2 To Lrow
        '~~> Check for partial match
        sPath = fPath & "*" & .Range("A" & i).Value & "*.*"

        If Len(Trim(Dir(sPath))) > 0 Then
            .Range("B" & i).Value = Dir(sPath)
        End If
    Next i
End With

Hi I have used this code above to return a file name when it matches a cell in a sheet. I am wondering on how to return the full pathname not just the filename? What would I need to change to this code?

Upvotes: 0

Views: 101

Answers (1)

BigBen
BigBen

Reputation: 49998

You already have fPath; just concatenate:

.Range("B" & i).Value = fpath & Dir(sPath)

Upvotes: 1

Related Questions