+ Reply to Thread
Results 1 to 2 of 2

Move all files from one directory to another if they meet criteria

Hybrid View

  1. #1
    Registered User
    Join Date
    05-21-2015
    Location
    England
    MS-Off Ver
    2013
    Posts
    25

    Move all files from one directory to another if they meet criteria

    Afternoon all,

    I have an excel template which emails PDF documents found in a directory to the relevant email addresses. I am trying to add some code into the macro which will also move all the sent files to a new folder called "Sent".

    The amount off attachments in each email will vary.

    I currently use the below code which only sort of works:

    For b = 2 To lastRow
    
        Dim FSO As Object
        Dim SourceFileName, DestinFileName As String
        Set FSO = CreateObject("Scripting.Filesystemobject")
        Sheets("Email List").Select
        Range("C2").Select
            On Error Resume Next
        SourceFileName = "M:\2017\Collections AR\Group Debts\Small Business\Statements\" & Cells(b, 1) & "*"
        DestinFileName = "M:\2017\Collections AR\Group Debts\Small Business\Statements\Sent\"
        FSO.MoveFile Source:=SourceFileName, Destination:=DestinFileName
        Next
    The issue I have is that I may have 20 files that are named something like ACC109 - ????? and I would need it to move every file which has ACC109 -, the code I currently use only moves the first one and then moves on to the next "b" which may be file names like ACC110 - ???? etc.

    Hope that makes sense?

    I feel like there is a simple solution such as a "do while sourcefilename > 0" or something like that but unfortunately im not quite at that stage in my development yet.

    Thanks in advance
    Dan

  2. #2
    Registered User
    Join Date
    05-21-2015
    Location
    England
    MS-Off Ver
    2013
    Posts
    25

    Re: Move all files from one directory to another if they meet criteria

    Managed to work this out myself, wasn't far off with my initial thoughts. For anyone who encounters something similar, this was my solution:

    Sub End_Test()
    
    Application.ScreenUpdating = False
    
    Dim Foldername As String
    Foldername = "M:\2017\Collections AR\Group Debts\Small Business\Statements\"
    
    
    Sheets("Statement").Visible = True
    Sheets("Invoice Template").Visible = True
    Sheets("ACC.Outstanding").Visible = True
    
        Sheets("Email List").Select
        Range("C2").Select
        
    For b = 2 To 250
    
        Dim FSO As Object
        Set FSO = CreateObject("Scripting.Filesystemobject")
        Dim SourceFileName, DestinFileName As String
        
        Do
        On Error Resume Next
        SourceFileName = "M:\2017\Collections AR\Group Debts\Small Business\Statements\" & Cells(b, 1) & "*"
        DestinFileName = "M:\2017\Collections AR\Group Debts\Small Business\Statements\Sent\"
        FSO.MoveFile Source:=SourceFileName, Destination:=DestinFileName
            If Err.Number <> 0 Then
        Err.Clear
        GoTo Next1
        End If
        Loop While SourceFileName > 0
    Next1:
        Next
        
        Sheets("Statement").Visible = False
        Sheets("Invoice Template").Visible = False
        Sheets("ACC.Outstanding").Visible = False
        
    Application.ScreenUpdating = True
      ActiveWorkbook.FollowHyperlink Foldername
    MsgBox ("Emails Sent - Print the files remaining in the file"), , "Complete"
        
    End Sub

+ 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. Macro to Move files from one directory to other directory
    By Shanthuday in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-05-2013, 07:21 AM
  2. Move rows to different worksheet that meet certain criteria
    By pstritt in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-25-2013, 02:50 PM
  3. Move files from one directory to another based on excel data
    By CWinkler in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-28-2011, 10:05 PM
  4. Replies: 1
    Last Post: 04-28-2011, 05:02 PM
  5. Problem with code - move files to another directory
    By uncleslinky in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-14-2011, 10:06 AM
  6. Macro to move files from one directory to another
    By Barb Reinhardt in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-21-2006, 12:40 PM
  7. Move all files from one directory to another
    By Prema in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-14-2005, 07:09 PM
  8. [SOLVED] move all files in a directory into another
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-24-2005, 12:06 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