+ Reply to Thread
Results 1 to 4 of 4

Display folder name in Excel - VBA Macro

Hybrid View

  1. #1
    Registered User
    Join Date
    03-28-2014
    Location
    Chennai, India
    MS-Off Ver
    Excel 2007
    Posts
    3

    Display folder name in Excel - VBA Macro

    Hi, I need a macro which displays the folder and subfolder name from a specific path in the column A of the excel sheet in the same heirarchy. Also whenever a sub folder name is displayed in column A, I need its main folder name to be displayed in the same row Column B i.e offset(0,1). I have a macro, but it displays the folder name in the 1st cell of column B, I need it to increment to the subsequent cells based on subfolders.


    Sub Step1()
    Dim startRange As Range

    Sheet1.Cells.Clear
    Set startRange = Sheet1.Range("A5")
    'Parent Directory - Change this to whichever directory you want to use
    ListFoldersAndInfo "C:\desktop\", startRange
    End Sub


    Sub ListFoldersAndInfo(foldername As String, Destination As Range)
    Dim FSO As Object
    Dim Folder As Object
    Dim R As Long
    Dim subfolder As Object
    Dim Wks As Worksheet
    Dim x As Long
    Dim y As Long


    Set FSO = CreateObject("Scripting.FileSystemObject")

    Set Folder = FSO.GetFolder(foldername)
    Destination = Folder.Name
    'R = Activecell.Row
    'c = Activecell.Column
    Set Destination = Destination.Offset(1, 0)

    For Each subfolder In Folder.SubFolders

    x = Activecell.Row
    y = Activecell.Column

    Cells(x, y + 1) = Folder.Name
    'Set Destination = Destination.Offset(0, 1)


    ListFoldersAndInfo Folder.Path & "\" & subfolder.Name, Destination

    Next subfolder



    Set FSO = Nothing


    End Sub

    I am a beginner in vba...so an explanation along with codes will be really helpful for me. Thanks in advance

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Display folder name in Excel - VBA Macro

    Set Destination = Destination.Offset(1, 0) is where you are going wrong (I think) you are using the folder name as a range, so you'll need to get the folder name, find it on the page, and then offset from where its found.
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

  3. #3
    Registered User
    Join Date
    03-28-2014
    Location
    Chennai, India
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Display folder name in Excel - VBA Macro

    Quote Originally Posted by nathansav View Post
    Set Destination = Destination.Offset(1, 0) is where you are going wrong (I think) you are using the folder name as a range, so you'll need to get the folder name, find it on the page, and then offset from where its found.
    Thanks for your reply Nathan, since Im new to vba, it will be helpful for me if you post the codes.

  4. #4
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Display folder name in Excel - VBA Macro

    Use the match function to find it.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Display data from all excel files in a folder
    By smugglersblues in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 10-22-2013, 12:27 PM
  2. Creating CULUM results - Folder / Sub Folder from excel macro
    By mash1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-13-2012, 10:19 AM
  3. Replies: 3
    Last Post: 03-27-2008, 07:30 PM
  4. Replies: 1
    Last Post: 06-23-2006, 08:18 PM
  5. Replies: 1
    Last Post: 05-10-2006, 07:10 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1