搜索当前文件夹

时间:2016-01-20 01:21:53

标签: excel vba excel-vba

此Excel VBA代码创建指向A列中项目列表的超链接,以及来自FIXED位置的相应文件夹名称。例如:

 Room101 is hyperlinked to C:\Files\Pictures\Room101

 Room102 is hyperlinked to C:\Files\Pictures\Room102

 Room103 is hyperlinked to C:\Files\Pictures\Room103

我一直在尝试重新设计文件夹目标,使其更具动态性,特别是搜索文件夹应该是Excel文件的当前文件夹。请参阅下面第7行的代码:< / p>

Option Explicit
Dim lngRow

Public Sub Aufruf()
    Dim lngTMP As Long
    lngTMP = ActiveSheet.Cells(Cells(Rows.Count, "A").End(xlUp).Row, 1).Row
    For lngRow = 1 To lngTMP
        searchDir "M:\Pictures" 'fixed folder location
    Next lngRow
End Sub

Private Sub searchDir(strDir)
    Dim objSubDir As Object
    Dim strName As String
    Dim objFSO As Object
    Dim objDir As Object
    On Error Resume Next
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objDir = objFSO.getfolder(strDir)
    For Each objSubDir In objDir.subfolders
        strName = StrReverse(Split(StrReverse(objSubDir.Path), "\")(0))
        If strName = Cells(lngRow, 1).Text Then
            ActiveSheet.Hyperlinks.Add Anchor:=Cells(lngRow, 1), Address:= _
                objSubDir.Path & "\", TextToDisplay:=Cells(lngRow, 1).Text
                Exit Sub
        End If
        searchDir objSubDir
    Next
    Set objFSO = Nothing
    Set objDir = Nothing
End Sub

您会注意到文件路径已固定为&#34; M:\ Pictures&#34;。

如何搜索保存Excel文档的文件夹?我试图使用以下变体:

path = ActiveWorkbook.Path

0 个答案:

没有答案