Announcement

Collapse
No announcement yet.

Saving files from a folder with Yesterdays date to a folder with Todays Date

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Saving files from a folder with Yesterdays date to a folder with Todays Date



    Hi,

    I am trying to copy all the files in a folder with Yesterdays date to a folder with Todays date.


    In my main sheet I have created a formula that shows todays date. I then subtracted 1 to get Yesterdays date.

    I concatenated this with the source path name to get the exact folder but it;s not copying anything across.

    Would appreciate any help.

    Code:
    Sub Move2()
    'Declaration
    Dim FSO
    Dim sFile As String
    Dim sSFolder As String
    Dim sDFolder As String
    
    
    'This is Your File Name which you want to Copy.You can change File name at c20.
    sFile = Sheets("How_To").Range("C20")
    
    'Change to match the source folder path. You can change Source Folder name at D21.
    sSFolder = Sheets("How_To").Range("D21")
    
    'Change to match the destination folder path. You can change Destination Folder name at D22.
    sDFolder = Sheets("How_To").Range("D22")
    
    'Create Object for File System
    Set FSO = CreateObject("Scripting.FileSystemObject")
    
    'Checking If File Is Located in the Source Folder
    If Not FSO.FileExists(sSFolder & sFile) Then
        MsgBox "Specified File Not Found in Source Folder", vbInformation, "Not Found"
        'Copying If the Same File is Not Located in the Destination Folder
    ElseIf Not FSO.FileExists(sDFolder & sFile) Then
        FSO.CopyFile (sSFolder & sFile), sDFolder, True
        MsgBox "Specified File Copied to Destination Folder Successfully", vbInformation, "Done!"
    Else
        MsgBox "Specified File Already Exists In The Destination Folder", vbExclamation, "File Already Exists"
    End If
    End Sub

  • #2
    Code Tags
    Your post does not comply with our Forum RULES. Use code tags around code.

    Posting code between tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Click on Edit to open your thread, then highlight your code and click the # icon at the top of your post window. Please take a minute to read the few rules you agreed to when you joined.

    (This thread should receive no further responses until this moderation request is fulfilled)

    Alan

    Did you debug your code first? http://www.cpearson.com/excel/DebuggingVBA.aspx
    FORUM RULES----->http://www.ozgrid.com/forum/announcement.php?f=8

    If someone has helped you, say "thank you" by clicking on the Like Button.

    Comment


    • #3


      Can you attach an example of your file. It might not be working because Excel records dates differently to what you see. Dates are stored as 5 digit numbers.

      How are you naming the Folders?

      It might simply need the cell contents to be changed to Strings

      Code:
      Sub Move2()
      'Declaration
      Dim FSO
      Dim sFile As String
      Dim sSFolder As String
      Dim sDFolder As String
      
      
      'This is Your File Name which you want to Copy.You can change File name at c20.
      sFile = CStr(Sheets("How_To").Range("C20"))
      
      'Change to match the source folder path. You can change Source Folder name at D21.
      sSFolder = CStr(Sheets("How_To").Range("D21"))
      
      'Change to match the destination folder path. You can change Destination Folder name at D22.
      sDFolder = CStr(Sheets("How_To").Range("D22"))
      
      'Create Object for File System
      Set FSO = CreateObject("Scripting.FileSystemObject")
      
      'Checking If File Is Located in the Source Folder
      If Not FSO.FileExists(sSFolder & sFile) Then
          MsgBox "Specified File Not Found in Source Folder", vbInformation, "Not Found"
          'Copying If the Same File is Not Located in the Destination Folder
      ElseIf Not FSO.FileExists(sDFolder & sFile) Then
          FSO.CopyFile (sSFolder & sFile), sDFolder, True
          MsgBox "Specified File Copied to Destination Folder Successfully", vbInformation, "Done!"
      Else
          MsgBox "Specified File Already Exists In The Destination Folder", vbExclamation, "File Already Exists"
      End If
      End Sub
      Hope that Helps

      Roy

      New users should read the Forum Rules before posting

      For free Excel tools & articles visit my web site

      RoyUK's Web Site

      royUK's Database Form

      Where to paste code from the Forum

      About me.

      Comment

      Working...
      X