簡體   English   中英

Excel VBA文件名搜索返回完整路徑

[英]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

嗨,我已經使用上面的代碼在匹配工作表中的單元格時返回文件名。 我想知道如何返回完整路徑名而不僅僅是文件名? 我需要對此代碼進行哪些更改?

你已經有了fPath 只是連接:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM