• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

VBA Create Indented List of Files/Subfolders |VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / VBA Create Indented List of Files/Subfolders |VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosVBA Create Indented List of Files/S…
sp_PrintTopic sp_TopicIcon
VBA Create Indented List of Files/Subfolders
Avatar
will
Australia

Active Member
Members
Level 0
Forum Posts: 3
Member Since:
September 8, 2022
sp_UserOfflineSmall Offline
1
September 14, 2022 - 11:55 am
sp_Permalink sp_Print sp_EditHistory

Hello all, I hope you're all well.

After finding the fantasic vba macro by Philip Treacy Create Hyperlinked List of Files in Subfolders, I was inspired to take it a little further by making some of the changes found in the comments but also to group the results by subfolders. Which then led me to find another great vba macro on StackOverflow written by PeterT at Excel VBA List Files Grouped by Folder and have been trying to incorporate both Philip Treacy's & PeterT's marcos together. 
However, I've run into a little problem that I can't quite figure out how to solve.

For the hyperlinks, I'm not too sure how to get these hyperlinks when clicked, to open the file or subfolder, at the moment it only opens the "rootFolder"

Similarly, I don't really know how to display the file/subfolder name instead of the path for the hyperlink "TextToDisplay:="

If anyone has any ideas, any assistance would be greatly appreciated. I'm a little new to vba but am a fast learner (I think) haha  🙂
The code I have so far is attached as a workbook

Have also posted my question here https://stackoverflow.com/ques.....hyperlinks

sp_AnswersTopicSeeAnswer See Answer
Avatar
Velouria
London or thereabouts
Moderator
Members


Trusted Members

Moderators
Level 4
Forum Posts: 627
Member Since:
November 1, 2018
sp_UserOfflineSmall Offline
2
September 14, 2022 - 7:24 pm
sp_Permalink sp_Print

You'll need to add a loop to do the hyperlinks one at a time:

 

'--- copy the array to the worksheet
Const START_ROW As Long = 4
Dim pathRange As Range
Set pathRange = Sheet1.Range("A" & START_ROW).Resize(UBound(pathArray), 1)
pathRange = pathArray
Dim cell As Range
For Each cell In pathRange
cell.Worksheet.Hyperlinks.Add Anchor:=cell, Address:=cell.Value, TextToDisplay:=Mid$(cell.Value, InStrRev(cell.Value, Application.PathSeparator) + 1), ScreenTip:="Click To Open"
Next cell
pathRange.Font.Color = RGB(237, 125, 49)
pathRange.Font.Bold = True
pathRange.Font.Italic = True

sp_AnswersTopicAnswer
Answers Post
Avatar
will
Australia

Active Member
Members
Level 0
Forum Posts: 3
Member Since:
September 8, 2022
sp_UserOfflineSmall Offline
3
September 15, 2022 - 12:47 pm
sp_Permalink sp_Print

Wow thats fantasic, thanks! I wasnt too sure how to use a loop to fix it up but that works perfectly.

I've been playing around with the code a little bit and have been trying to change the font colours for the subfolders and files. So far I've only been able to get the

rootfolder: colour as orange, text as bold without an underline
first subfolder in rootfolder: colour as blue, text as bold without an underline
files in first subfolder in rootfolder; colour as blue, text as italic and underlined

You wouldn't know how I might be able to make all the subfolders inside the rootfolder use the colour black while having all the files blue? this is the format i would like to use

I'm guessing I have to use a loop somewhere here but am unsure how to use it....

For Each rowGroup In folderGroups
folderData = Split(folderGroups(rowGroup), ",")
theseRows = folderData(0)
level = folderData(1)
With pathRange.rows(theseRows)
.IndentLevel = level
If level < MAX_GROUP_LEVEL Then
.Group
pathRange.rows(theseRows).Font.Underline = True
pathRange.rows(theseRows).Font.Color = RGB(68, 114, 196)
pathRange.rows(theseRows).Font.Bold = False
pathRange.rows(theseRows).Font.Italic = True
ActiveSheet.Outline.ShowLevels RowLevels:=2, ColumnLevels:=2
End If
End With
pathRange.rows(level).Font.Bold = True
pathRange.rows(level).Font.Underline = False
pathRange.rows(level).Font.Italic = False
Next rowGroup
End Sub

Avatar
Velouria
London or thereabouts
Moderator
Members


Trusted Members

Moderators
Level 4
Forum Posts: 627
Member Since:
November 1, 2018
sp_UserOfflineSmall Offline
4
September 16, 2022 - 12:16 am
sp_Permalink sp_Print

Try this version:

 

''--found at https://stackoverflow.com/ques.....4#73669234
Public Sub ShowFilePaths()
Dim rootFolder As String
rootFolder = SelectFolder
If rootFolder = vbNullString Then Exit Sub
'--- quick fixup if needed
rootFolder = rootFolder & IIf(Right$(rootFolder, 1) = "\", vbNullString, "\")

Dim pathArray As Variant
pathArray = GetAllFiles(rootFolder)
Dim folderGroups As Object
Set folderGroups = BuildFolderDictionary(rootFolder, pathArray)

'--- when debugging, this block just clears the worksheet to make it
' easier to rerun and test the code
On Error Resume Next
With Sheet1
.UsedRange.ClearOutline
.UsedRange.Clear
.Outline.SummaryRow = xlAbove
End With
Err.Clear
On Error GoTo 0

'--- copy the array to the worksheet
Const START_ROW As Long = 4
Dim pathRange As Range
Set pathRange = Sheet1.Range("A" & START_ROW).Resize(UBound(pathArray), 1)
pathRange = pathArray
Dim cell As Range
For Each cell In pathRange.Cells
cell.Worksheet.Hyperlinks.Add Anchor:=cell, Address:=cell.Value, _
TextToDisplay:=Mid$(cell.Value, InStrRev(cell.Value, Application.PathSeparator) + 1), _
ScreenTip:="Click To Open"
Next cell
With pathRange
' most items will be files, so format all cells with format for files first
With .Font
.Color = RGB(68, 114, 196)
.Bold = False
.Italic = True
End With
End With
'------ now apply the indention levels to each line on the sheet
' and group the same rows
Const MAX_GROUP_LEVEL As Long = 8
Dim rowGroup As Variant
Dim level As Long
Dim folderData As Variant
Dim theseRows As String
For Each rowGroup In folderGroups
folderData = Split(folderGroups(rowGroup), ",")
theseRows = folderData(0)
level = folderData(1)
With pathRange.rows(theseRows)
.IndentLevel = level
If level < MAX_GROUP_LEVEL Then
.Group
ActiveSheet.Outline.ShowLevels RowLevels:=1
End If
' format the subfolders - cell above the top of the group
With .Cells(0).Font
.Color = vbBlack
.Bold = True
.Italic = False
End With
End With
Next rowGroup

' format root folder
With pathRange.Cells(1).Font
.Color = RGB(237, 125, 49)
.Bold = True
.Italic = False
End With

End Sub

Avatar
will
Australia

Active Member
Members
Level 0
Forum Posts: 3
Member Since:
September 8, 2022
sp_UserOfflineSmall Offline
5
September 16, 2022 - 11:25 am
sp_Permalink sp_Print sp_EditHistory

wow All working now. the wonders of vba and some people's knowledge never cease to amaze me, thank you very much for adding comments in the vba, they helped me to understand the applied macro process. Thanks again for all your assistance!

Avatar
Velouria
London or thereabouts
Moderator
Members


Trusted Members

Moderators
Level 4
Forum Posts: 627
Member Since:
November 1, 2018
sp_UserOfflineSmall Offline
6
September 16, 2022 - 7:10 pm
sp_Permalink sp_Print

You're welcome. 🙂

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Catalin Bombea, Kim Knox, Nada Perovic, Othman AL MUTAIRI
Guest(s) 9
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 871
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
Jessica Stewart: 205
A.Maurizio: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
Othman AL MUTAIRI
Misael Gutierrez Sr.
Attif Ihsan
Kieran Fee
Murat Hasanoglu
Brett Dryland
Saeed Aldousari
Bhuwan Devkota
Kathryn Patton
Maria Conatser
Forum Stats:
Groups: 3
Forums: 24
Topics: 6222
Posts: 27293

 

Member Stats:
Guest Posters: 49
Members: 31916
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.