+ Reply to Thread
Results 1 to 3 of 3

Get name of file and store it in cell

  1. #1
    Forum Contributor
    Join Date
    04-11-2013
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    108

    Question Get name of file and store it in cell

    Hi. I have a section of code which opens all the files in a folder and captures some data from it. How can I also capture the name of the each file and store it in cell so that the data collected can be correctly referenced. My code is along the following lines


    strPath = "Folder Path"
    strFilename = Dir(strPath & Application.PathSeparator & "*.xls*")

    'Loop through all workbooks in folder
    Do Until Len(strFilename) = 0

    'Set the current workbook it has found as the source workbook
    Set wbSrc = Workbooks.Open(strPath & Application.PathSeparator & strFilename)

    [Code for collecting data is in here]

    'close workbook
    wbSrc.Close False

    strFilename = Dir

    Loop

    Any way of storing the name of each file every time it loops so I can paste it in a cell?

    Thanks

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Get name of file and store it in cell

    Hi,

    Untested but why not Dim a new counter variable, say Dim c as Long, then just before the close workbook line add

    Please Login or Register  to view this content.
    Obviously change Sheet1 and Range A1 as necessary
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645
    Where exactly do you want to store the filename?

    Do you only want to store it for the file the code is working on or do you want to record the names of all the files?
    If posting code please use code tags, see here.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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