Consulting

Results 1 to 10 of 10

Thread: Solved: macro to add FileName and currentdate to an existing worksheet

  1. #1
    VBAX Contributor
    Joined
    Apr 2008
    Posts
    136
    Location

    Solved: macro to add FileName and currentdate to an existing worksheet

    Hello

    I need to add some code to a macro so that each time it is run it will add the FileName (in Cell A2) and CurrentDate (in cell B2) into an already set up spreadsheet called "Files". Headings (already set up) will be in A1 and B1

    Everytime the macro is run there will be new files in the folder (probably between 50-100), so data in "Files" should not be overwriten data but instead add name and current date to the next un-used row down and so on.

    Thanks in advance

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]Sub FileDate()
    Sheets("Files").Cells(Rows.Count, 1).End(xlUp).Offset(1).Resize(, 2) = Array(ActiveWorkbook.Name, Date)
    End Sub
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Contributor
    Joined
    Apr 2008
    Posts
    136
    Location
    Thanks- can you tweek the code so that it adds names and current date of all files in a specific folder that reside in a folder, also would like the code to open a separate Workbook called Files and adds the information in Sheet1 of "Files". It will then always add information to this workbook

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]Sub FileDate()
    Dim WB As Workbook
    Set WB = Workbooks.Open("C:\AAA\Files.xls")
    Sheets(1).Cells(Rows.Count, 1).End(xlUp).Offset(1).Resize(, 2) = Array(ActiveWorkbook.Name, Date)
    WB.Close True
    End Sub[/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Contributor
    Joined
    Apr 2008
    Posts
    136
    Location
    Thanks -nearly there. I need the names of the files that are in another directory, i.e ("C:\BBB\"). There will be around 50-100 files probably. The information (filename and current date +time) still has to go into "Files"

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Is this what you are after?
    [VBA]
    Sub FileDate()
    Dim WB As Workbook, MyFile As String
    Set WB = Workbooks.Open("C:\AAA\Files.xls")
    MyFile = Dir("C:\BBB\*.*")
    Do Until MyFile = ""
    Sheets(1).Cells(Rows.Count, 1).End(xlUp).Offset(1).Resize(, 2) = Array(MyFile, Date)
    MyFile = Dir
    Loop
    WB.Close True
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    VBAX Contributor
    Joined
    Apr 2008
    Posts
    136
    Location
    This works perfectly! Many Thanks. I just replaced Date with Now so it gives me a date and time. Because I will have between 50-100 files may be more, in the BBB folder is this the most efficient code? Just a query.

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Can't think of a quicker method offhand.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    VBAX Contributor
    Joined
    Apr 2008
    Posts
    136
    Location
    How do I change this thread to solved?

  10. #10
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Use the Thread Tools dropdown
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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