+ Reply to Thread
Results 1 to 4 of 4

Thread: Timestamp

  1. #1
    Registered User
    Join Date
    12-21-2011
    Location
    NY, USA
    MS-Off Ver
    Excel 2010
    Posts
    36

    Timestamp

    I don't understand. IT worked perfectly at home. But when I trying another computer, the first part of the MACRO works GREAT!, it was able to create a Folder with Date Stamp and copy from old path to this new path.

    But when it got to the 2nd part where it need to rename the file in the new path it gave me a warning that the file does not exist. How is that possible? It was created in the first place to begin with. It can't be right. I have Microsoft Excel 2010 while the other computer has 2007. I really dont think that would matter. How do I revise this to make it work? (but it works at my home computer)

    
    'This example move all Excel files from FromPath to ToPath.  Note: It will create the folder ToPath for you with a date stamp
    
    Sub RenamedFiles_To_New_Folder()
    
        Dim FSO As Object
        Dim FromPath As String
        Dim ToPath As String
        Dim FileExt As String
        Dim FNames As String
        Dim FilePath As Object
    
    Dim OldDir As String
    Dim i As Long
    OldDir = CurDir
    Dim fName As String
    
    FromPath = "C:\Users\Juan\Desktop\test\Macrofolder"  '<< Change
    
    ToPath = "C:\Users\Juan\Desktop\test\Macrofolder\" & Format(Date,
    "yyyy-mm-dd")  '<< Change only the destination folder
    
    FileExt = "*.xlsx"    '<< Change
    
    'You can use *.* for all files or *.doc for word files
    
    If Right(FromPath, 1) <> "\" Then
            FromPath = FromPath & "\"
    End If
    
    FNames = Dir(FromPath & FileExt)
    
    If Len(FNames) = 0 Then
            MsgBox "No files in " & FromPath
            Exit Sub
    End If
    Set FSO = CreateObject("scripting.filesystemobject")
    
    FSO.CreateFolder (ToPath)
    FSO.CopyFile Source:=FromPath & FileExt, Destination:=ToPath
    Set FilePath = FSO.GetFolder(ToPath)
    
    ChDir FilePath & "\"
    fName = Dir(FilePath & "\" & "*.xlsx")
    
    Do While Len(fName) <> 0
    i = i + 1
    
    If Not FSO.FileExists(FilePath & "\" & fName & Format(Date, " mm-dd-yy") & ".xlsx") Then
    
    Name fName As Left(fName, InStr(1, fName, ".", vbTextCompare) - 1) & Format(Date, " mm-dd-yy") & ".xlsx" 'THIS IS WHERE THE PROBLEM IS 
     End If
    
            fName = Dir
    Loop
    
    ChDir OldDir
    
    MsgBox "You can find the files from " & FromPath & " in " & ToPath
    End Sub

  2. #2
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    359

    Re: Timestamp

    The code seems fine - is it one particular file causing the problem - does it rename any files in the new directory. It may be a permissions issue as you are working in "C\Users". Just for interests sake add some error handling to see if it is just one file or all files. Add
    Do While Len(fname)<>0
    On Error Resume Next
    and see if any files are copied. THIS IS NOT A SOLUTION. It is just to see if one file is an issue. Try the code outside of "C:\users" - set up a test folder "C:\Test" and put some excel files in here - if it works in the test folder then it may be a permissions issue.
    I have tried this code and it works fine so it must be a problem on your end.
    Hope this helps.
    Anthony
    Pack my box with five dozen liquor jugs
    PS: Remember to mark your questions as Solved once you are satisfied. Please rate the answer(s) by selecting the Star in the lower left next to the Triangle. It is appreciated

  3. #3
    Registered User
    Join Date
    12-21-2011
    Location
    NY, USA
    MS-Off Ver
    Excel 2010
    Posts
    36

    Re: Timestamp

    Thanks I will try it at work first thing in the morning. I'm not yet familiar with error handler. Will let you know. Thanks

  4. #4
    Registered User
    Join Date
    12-21-2011
    Location
    NY, USA
    MS-Off Ver
    Excel 2010
    Posts
    36

    Re: Timestamp

    Yes it has something to do with permission. Thank for your help

+ 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.2.0