You are here:

Excel/Automatic link to folders

Advertisement


Question
QUESTION: Automatic link to folders
Is looking for a macro that lists all subfolders in a top folder and automatically make links to the folders.
Top Folder path = ActiveWorkbook path so that Excel spreadsheet can be copied to different locations without having to change the code.
The sheet of links used as an overview and quick access to the different folders

Grateful for help

ANSWER: Svein,


Here are several articles showing different approaches to recursively searching directories.  You can remove the code to list files and just use the directory results.

http://support.microsoft.com/kb/185476/EN-US/
How To Search Directories to Find or List Files

http://support.microsoft.com/kb/185601/EN-US/
HOW TO: Recursively Search Directories by Using FileSystemObject

http://support.microsoft.com/kb/186118/EN-US/
How To Use FileSystemObject with Visual Basic


if you only want the next level directories (the directories one level below the starting directory - the thisworkbook.path) you can use

Sub AddDirectoryNames()
Dim i As Long, spath As String
Dim Dirname As String, ndir As Long, dirnames() As String
spath = ThisWorkbook.Path
If Right(spath, 1) <> "\" Then spath = spath & "\"
ndir = 1
ReDim dirnames(1 To ndir)
     Dirname = Dir(spath, vbDirectory Or vbHidden Or vbArchive Or vbReadOnly _
Or vbSystem)  ' Even if hidden, and so on.
     Do While Len(Dirname) > 0
        ' Ignore the current and encompassing directories.
        If (Dirname <> ".") And (Dirname <> "..") Then
         ' Check for directory with bitwise comparison.
         If GetAttr(spath & Dirname) And vbDirectory Then
         dirnames(ndir) = Dirname
         ndir = ndir + 1
         ReDim Preserve dirnames(1 To ndir)
         End If
        End If
        Dirname = Dir()
     Loop
' now you have a list of top level directories in the array dirnames
For i = 1 To UBound(dirnames) - 1
 ActiveSheet.Hyperlinks.Add Anchor:=Cells(i, 1), Address:=dirnames(i), _
    TextToDisplay:=dirnames(i)
Next
End Sub

that was tested and worked for me.

--
Regards,
Tom Ogilvy

---------- FOLLOW-UP ----------

QUESTION: Your code worked perfectly
But if i want to include subfolderstto the list, how do i do that.
I've tried different, but I'm obviously not good enough.

Regard Svein

Answer
Svein,

this worked for me.

Insert a new module in your workbook, then insert this code.

run Master to get your list of hyperlinks.

Option Explicit
Public DirArray() As String
Public Dircount As Long

  
Sub Master()
Dim i As Long
ReDim DirArray(1 To 1)
Dircount = 1
FindFiles ThisWorkbook.path
For i = 1 To UBound(DirArray) - 1
ActiveSheet.Hyperlinks.Add Anchor:=Cells(i, 1), Address:=DirArray(i), _
   TextToDisplay:=DirArray(i)
Next

End Sub

Function FindFiles(spath As String)

Dim FileName As String   ' Walking filename variable.
Dim DirName As String    ' SubDirectory Name.
Dim dirNames() As String ' Buffer for directory name entries.
Dim nDir As Integer      ' Number of directories in this path.
Dim i As Integer         ' For-loop counter.

On Error GoTo sysFileERR



If Right(spath, 1) <> "\" Then spath = spath & "\"
     ' Search for subdirectories.
 nDir = 1
 ReDim dirNames(1 To nDir)
 DirName = Dir(spath, vbDirectory Or vbHidden _
        Or vbArchive Or vbReadOnly Or vbSystem)  ' Even if hidden, and so on.
 Do While Len(DirName) > 0
 ' Ignore the current and encompassing directories.
  If (DirName <> ".") And (DirName <> "..") Then
   ' Check for directory with bitwise comparison.
   If GetAttr(spath & DirName) And vbDirectory Then
      dirNames(nDir) = DirName
      DirArray(Dircount) = spath & DirName
      nDir = nDir + 1
      Dircount = Dircount + 1
      ReDim Preserve dirNames(1 To nDir)
      ReDim Preserve DirArray(1 To Dircount)
   End If
sysFileERRCont:
  End If
  DirName = Dir()  ' Get next subdirectory.
 Loop
Debug.Print spath, nDir
 ' If there are sub-directories..
 If nDir > 1 Then
    ' Recursively walk into them
   For i = 1 To nDir - 1
      FindFiles spath & dirNames(i) & "\"
   Next i
 End If

AbortFunction:
     Exit Function
sysFileERR:
If Right(DirName, 4) = ".sys" Then
  Resume sysFileERRCont ' Known issue with pagefile.sys
Else
  MsgBox "Error: " & Err.Number & " - " & Err.Description, , _
        "Unexpected Error"
  Resume AbortFunction
End If
End Function


--
Regards,
Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.