VBA Filing to a folder - I have been stuck on this for a MONTH!

alf810

New Member
Joined
Dec 30, 2015
Messages
17
Hey everyone,

At my work I'm trying to use Excel VBA to take scanned PDF documents of patient information from the path they're scanned into and have a macro that will automatically go through the PDF files, look at the name of the files and be able to go to another path (patient accounts directly) and put said file into the correct patient folder.

Step-by-Step
Macro looks at the first PDF file in scanned folder
The PDF files will be named with two letters and up to six numbers. The letters represent the first two letters of the patient's last name, the number represents the account.
Macro looks at the two letters, goes to the patient accounts directly and determines the correct letter (folders are split as: Aa-Al, Am-Az, Ba-Bl, Bm-Bz, etc...)
Macro goes into correct alphabetic folder and looks for the exact account number (if patient's number is 112, and another patient is 1125, it might cause an issue otherwise)
If Macro finds the folder then it simply moves the file to the folder. If Macro doesn't find the folder then I want it to just place the folder in a "TO FILE" folder on my computer.
Macro then looks at the next PDF file, process is repeated.

I want to thank user LockeGarmin for helping me on this Macro. He got it to work perfectly at my home computer (Excel 2016), but my work computer is give me issues (either Excel 2010 or 2013, I believe it is 2013). I would think it would be a network issue (the folders are on different drives), however when in debug mode it looks like the paths are all fully recognized, as well as the file name that needs to be moved, so I'm wondering if it's a version issue?

Things don't get screwed up (again at work, not home) until around where the code states Set CustomerDirectly = FindCustomer[[SurnamePrefix, AccountNumber, CustomerDocumentsDirectly

It seems after this code, in debug mode) a lot of variables just start equally "Nothing" instead of a proper path or file. Even in the Function FindCustomerFolder(SurnamePrefix As String, AccountNumber As String, CustomerDocumentsDirect As Object) As Object pretty much everything equals "nothing", including Folder itself at that point.

When I do try to run the script I get an error around the For Each Folder in SurnameGroupFolder.SubFolders... etc.. the error states that I am missing a With block?

Here's the code, please help if you can. You can edit the code below or create your own if you feel you could make that'll work on an Excel 2013 working with network drives. Also, if the folders going alphabetically seems too difficult (like South, Adam #1115, file called SO1115.PDF going into Sm-Sz, then I can name the first half of files with only A and the second with only Z, if it's easier, so the above filed would be called SZ1115.PDF). Anyway, thanks for any help!

Code:
Option Explicit
 
Sub MoveFiles()
 
Dim Folder As Object
Dim File As Object
 
Const FolderPath As String = "C:\Users\MIS\Documents\MACRO TEST\ScannerDocs"
 
 
  Set Folder = CreateObject("Scripting.FileSystemObject").GetFolder(FolderPath)
  
  For Each File In Folder.Files
    Call MoveCustomerDocument(File.Path)
  Next File
 
End Sub
 
Sub MoveCustomerDocument(DocumentPath As String)
 
Const CustomerDocumentsDirectoryPath As String = "C:\Users\MIS\Documents\MACRO TEST\CustomerDocs"
Const NewCustomerDocumentsDirectoryPath As String = "C:\Users\MIS\Documents\MACRO TEST\NewCustomers"
 
Dim FSO As Object 'Scripting.FileSystemObject
Dim CustomerDocumentsDirectory As Object 'Scripting.Folder
Dim CustomerDirectory As Object 'Scripting.Folder
 
Dim DestinationDirectoryPath As String
 
Dim DocumentName As String
Dim SurnamePrefix As String
Dim AccountNumber As String
 
  Set FSO = CreateObject("Scripting.FileSystemObject")
 
  'There could be room for error checking in this section in case an improper file name is passed into the function i.e. "arc123" instead of "ar123"
  SurnamePrefix = Left$(FSO.GetBaseName(DocumentPath), 2)
  AccountNumber = Mid$(FSO.GetBaseName(DocumentPath), 3)
  Set CustomerDocumentsDirectory = FSO.GetFolder(CustomerDocumentsDirectoryPath)
  
  Set CustomerDirectory = FindCustomerFolder(SurnamePrefix, AccountNumber, CustomerDocumentsDirectory)
  
  If CustomerDirectory Is Nothing Then
    DestinationDirectoryPath = NewCustomerDocumentsDirectoryPath
  Else
    DestinationDirectoryPath = CustomerDirectory.Path
  End If
  
  FSO.MoveFile DocumentPath, FSO.BuildPath(DestinationDirectoryPath, FSO.GetFileName(DocumentPath))
 
    
 
End Sub
 
Function FindCustomerFolder(SurnamePrefix As String, AccountNumber As String, CustomerDocumentsDirectory As Object) As Object
 
Dim Folder As Object 'Scripting.Folder
Dim SurnameGroupFolder As Object 'Scripting.Folder
  
  'Loops through each Surname-Group Folder Alphabetically
  For Each Folder In CustomerDocumentsDirectory.SubFolders 'Subfolders are the GroupFolders followed by the Actual Customer's Directory
  
    'Stop once we've found a folder that is "greater than" the surname (Case Insensitive)
    If StrComp(Left$(Folder.Name, 2), SurnamePrefix, vbTextCompare) = 1 Then
      Exit For
    End If
    
    Set SurnameGroupFolder = Folder
  Next Folder
  
  'Search through the SurnameGroupFolder for a folder that matches the account number
  For Each Folder In SurnameGroupFolder.SubFolders
    If Folder.Name Like "*[#]" & AccountNumber Then
      Set FindCustomerFolder = Folder
      Exit Function
    End If
  Next Folder
 
End Function
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
I don't know what the problem is with the existing macro.

It seems to me that the existing code unnecessarily searches for the account number in all the sub-group folders (like Aa-Al). The first two letters in the PDF file name determine what sub-group folder to look for the account number in.

Give this a try. I don't know if it fixes the problem.

Code:
[COLOR=darkblue]Sub[/COLOR] Move_Patient_PDF_Files()
    
    [COLOR=darkblue]Dim[/COLOR] fsoFile [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Object[/COLOR], counter [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] SurnamePrefix1 [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR], SurnamePrefix2 [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] AccountNumber [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR], GroupFolder [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR], CustFolder [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    
    [COLOR=darkblue]Const[/COLOR] ScannerDocsPath [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR] = "C:\Users\MIS\Documents\MACRO TEST\ScannerDocs"
    [COLOR=darkblue]Const[/COLOR] CustomerDocumentsPath [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR] = "C:\Users\MIS\Documents\MACRO TEST\CustomerDocs"
    [COLOR=darkblue]Const[/COLOR] NewCustomerDocumentsPath [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR] = "C:\Users\MIS\Documents\MACRO TEST\NewCustomers"
        
    [COLOR=darkblue]With[/COLOR] CreateObject("Scripting.FileSystemObject")
    
        [COLOR=green]'loop through each file in scanner folder[/COLOR]
        [COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] fsoFile [COLOR=darkblue]In[/COLOR] .GetFolder(ScannerDocsPath).Files
            
            [COLOR=green]'Parse Surname letters and account number from file name[/COLOR]
            SurnamePrefix1 = Mid$(fsoFile.Name, 1, 1)       [COLOR=green]'Surname 1st letter[/COLOR]
            SurnamePrefix2 = Mid$(fsoFile.Name, 2, 1)       [COLOR=green]'Surname 2nd letter[/COLOR]
            AccountNumber = Mid$(.GetBaseName(fsoFile), 3)  [COLOR=green]'Account number[/COLOR]
            
            [COLOR=green]'Define Surname subgroup folder e.g.; Aa-Al or Am-Az[/COLOR]
            [COLOR=darkblue]If[/COLOR] Asc(UCase(SurnamePrefix2)) < 77 [COLOR=darkblue]Then[/COLOR] [COLOR=green]'test if 2nd letter is between A-L[/COLOR]
                GroupFolder = SurnamePrefix1 & "a-" & SurnamePrefix1 & "l"
            [COLOR=darkblue]Else[/COLOR]                                    [COLOR=green]'else the 2nd letter is between M-Z[/COLOR]
                GroupFolder = SurnamePrefix1 & "m-" & SurnamePrefix1 & "z"
            [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
            GroupFolder = CustomerDocumentsPath & "\" & GroupFolder & "\"
            
            [COLOR=green]'Find folder based on exact Account number and move file[/COLOR]
            CustFolder = Dir(GroupFolder & "\*#" & AccountNumber, vbDirectory)
            [COLOR=darkblue]If[/COLOR] CustFolder <> "" [COLOR=darkblue]Then[/COLOR]    [COLOR=green]'Test if exact account number folder found[/COLOR]
                [COLOR=green]'Move file to account number folder[/COLOR]
                fsoFile.Move GroupFolder & CustFolder & "\"
            [COLOR=darkblue]Else[/COLOR]
                [COLOR=green]'move file to new customer folder[/COLOR]
                fsoFile.Move NewCustomerDocumentsPath & "\"
            [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
            
            counter = counter + 1   [COLOR=green]'Count files[/COLOR]
            
        [COLOR=darkblue]Next[/COLOR] fsoFile
    
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
    
    MsgBox counter & " files moved. ", , "Move Customer PDF Files Complete"
    
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
Last edited:
Upvote 0
Give some examples of the pdf file names and their expected destination folder.

Hi John, thanks for replying. Here are some examples of pdf file names:

NOTE: The lastname, firstname #accountnumber is the universal standard for all patient files in the patient directory


  • Smith, John #8 file name would be SM8.PDF
  • Burton, Harold #293049 file name would be BU293049.PDF (Don't know if it would matter, but we only have an est. 250,000 worth of patients, so this is an extreme example)
  • Helms, Charles #5468 file name would be HE5468.PDF
  • Grant, Carol #39 file name would be GR39.PDF
  • Tulip Penny #3019 file name would be TU3019
  • -
With that said, if the folder ranges cause problems with anyone, then the files could be changed to something simpler for each foler (like SM8.PDF, BM293049.PDF, HA5468.PDF, GM39.PDF, TM3019.PDF respectively), that way the macro can just look up the first two letters of the folder, rather than figure out the entire range.

As for your second question, the example, SM8.PDF would be moved from the folder C:\Users\MIS\Documents\MACRO TEST\ScannerDoc and would go to the folder C:\Users\MIS\Documents\MACRO TEST\CustomerDocs, look for the subfolder SM-SZ, then once in that folder would look for the subfolder that had the exact (can't illustrate enough how important it is that the macro looks for the exact number, due to how many numbered accounts there are) number, in this case just "8" (not 81, or 781, etc... just 8) and it would file it in that folder, which would be Smith, John #8 .

That's if the macro finds the folder, if not (the patient may be new and no one set up a folder yet), then it would just go to a folder I'd handle manually like
C:\Users\MIS\Documents\MACRO TEST\NewCustomers

Unfortunately, I don't have the actual locations on me atm, but if wanted I can get the network folders from work tomorrow.
 
Upvote 0
I don't know what the problem is with the existing macro.

It seems to me that the existing code unnecessarily searches for the account number in all the sub-group folders (like Aa-Al). The first two letters in the PDF file name determine what sub-group folder to look for the account number in.

Give this a try. I don't know if it fixes the problem.

Code:
[COLOR=darkblue]Sub[/COLOR] Move_Patient_PDF_Files()
    
    [COLOR=darkblue]Dim[/COLOR] fsoFile [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Object[/COLOR], counter [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] SurnamePrefix1 [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR], SurnamePrefix2 [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] AccountNumber [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR], GroupFolder [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR], CustFolder [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    
    [COLOR=darkblue]Const[/COLOR] ScannerDocsPath [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR] = "C:\Users\MIS\Documents\MACRO TEST\ScannerDocs"
    [COLOR=darkblue]Const[/COLOR] CustomerDocumentsPath [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR] = "C:\Users\MIS\Documents\MACRO TEST\CustomerDocs"
    [COLOR=darkblue]Const[/COLOR] NewCustomerDocumentsPath [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR] = "C:\Users\MIS\Documents\MACRO TEST\NewCustomers"
        
    [COLOR=darkblue]With[/COLOR] CreateObject("Scripting.FileSystemObject")
    
        [COLOR=green]'loop through each file in scanner folder[/COLOR]
        [COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] fsoFile [COLOR=darkblue]In[/COLOR] .GetFolder(ScannerDocsPath).Files
            
            [COLOR=green]'Parse Surname letters and account number from file name[/COLOR]
            SurnamePrefix1 = Mid$(fsoFile.Name, 1, 1)       [COLOR=green]'Surname 1st letter[/COLOR]
            SurnamePrefix2 = Mid$(fsoFile.Name, 2, 1)       [COLOR=green]'Surname 2nd letter[/COLOR]
            AccountNumber = Mid$(.GetBaseName(fsoFile), 3)  [COLOR=green]'Account number[/COLOR]
            
            [COLOR=green]'Define Surname subgroup folder e.g.; Aa-Al or Am-Az[/COLOR]
            [COLOR=darkblue]If[/COLOR] Asc(UCase(SurnamePrefix2)) < 77 [COLOR=darkblue]Then[/COLOR] [COLOR=green]'test if 2nd letter is between A-L[/COLOR]
                GroupFolder = SurnamePrefix1 & "a-" & SurnamePrefix1 & "l"
            [COLOR=darkblue]Else[/COLOR]                                    [COLOR=green]'else the 2nd letter is between M-Z[/COLOR]
                GroupFolder = SurnamePrefix1 & "m-" & SurnamePrefix1 & "z"
            [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
            GroupFolder = CustomerDocumentsPath & "\" & GroupFolder & "\"
            
            [COLOR=green]'Find folder based on exact Account number and move file[/COLOR]
            CustFolder = Dir(GroupFolder & "\*#" & AccountNumber, vbDirectory)
            [COLOR=darkblue]If[/COLOR] CustFolder <> "" [COLOR=darkblue]Then[/COLOR]    [COLOR=green]'Test if exact account number folder found[/COLOR]
                [COLOR=green]'Move file to account number folder[/COLOR]
                fsoFile.Move GroupFolder & CustFolder & "\"
            [COLOR=darkblue]Else[/COLOR]
                [COLOR=green]'move file to new customer folder[/COLOR]
                fsoFile.Move NewCustomerDocumentsPath & "\"
            [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
            
            counter = counter + 1   [COLOR=green]'Count files[/COLOR]
            
        [COLOR=darkblue]Next[/COLOR] fsoFile
    
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
    
    MsgBox counter & " files moved. ", , "Move Customer PDF Files Complete"
    
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

Thanks so much for the reply AlphaFrog :), I'm going to email the code to my work email and try it out tomorrow. I'm at home now and, since the last test worked at home, but not at work, I don't really see a reason to try it at home now. Again, thanks and I'll keep you updated and whether or not I got it to work! If it works it would be a life saver!
 
Upvote 0
  • Smith, John #8 file name would be SM8.PDF
  • Burton, Harold #293049 file name would be BU293049.PDF (Don't know if it would matter, but we only have an est. 250,000 worth of patients, so this is an extreme example)
  • Helms, Charles #5468 file name would be HE5468.PDF
  • Grant, Carol #39 file name would be GR39.PDF
  • Tulip Penny #3019 file name would be TU3019

As for your second question, the example, SM8.PDF would be moved from the folder C:\Users\MIS\Documents\MACRO TEST\ScannerDoc and would go to the folder C:\Users\MIS\Documents\MACRO TEST\CustomerDocs, look for the subfolder SM-SZ, then once in that folder would look for the subfolder that had the exact number

That's if the macro finds the folder, if not (the patient may be new and no one set up a folder yet), then it would just go to a folder I'd handle manually like
C:\Users\MIS\Documents\MACRO TEST\NewCustomers
A lot of description, but you haven't really answered the destination folder question explicitly enough.

Summarising:

SM8.PDF -> C:\Users\MIS\Documents\MACRO TEST\CustomerDocs\SM-SZ\8\
BU293049.PDF -> C:\Users\MIS\Documents\MACRO TEST\CustomerDocs\BM-BZ\293049\
HE5468.PDF -> C:\Users\MIS\Documents\MACRO TEST\CustomerDocs\HA-HL\5468\

Is that right? If so, along the same line's as Alphafrog's code:

Code:
Const PDFscansFolderPath As String = "C:\Users\MIS\Documents\MACRO TEST\ScannerDoc"
Const CustomerDocumentsFolderPath As String = "C:\Users\MIS\Documents\MACRO TEST\CustomerDocs"
Const NewCustomerDocumentsFolderPath As String = "C:\Users\MIS\Documents\MACRO TEST\NewCustomers"


Public Sub Move_PDF_Files()
 
    Dim FSO As Object
    Dim PDFfile As Object
    Dim SurnamePrefix1 As String, SurnamePrefix2 As String
    Dim AccountNumber As String
    Dim CustomerFolder As String
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
        
    For Each PDFfile In FSO.GetFolder(PDFscansFolderPath).Files
        
        'Parse Surname letters and account number from file name
        SurnamePrefix1 = Mid(PDFfile.Name, 1, 1)
        SurnamePrefix2 = Mid(PDFfile.Name, 2, 1)
        AccountNumber = Mid(FSO.GetBaseName(PDFfile), 3)
        
        'Define Surname subgroup folder e.g. AA-AL or AM-AZ
        If UCase(SurnamePrefix2) <= "L" Then    'test if 2nd letter is between A-L
            CustomerFolder = SurnamePrefix1 & "A-" & SurnamePrefix1 & "L"
        Else                                    'else the 2nd letter is between M-Z
            CustomerFolder = SurnamePrefix1 & "M-" & SurnamePrefix1 & "Z"
        End If
        
        'Construct full destination folder from base folder, surname subgroup and account number
        
        CustomerFolder = CustomerDocumentsFolderPath & "\" & CustomerFolder & "\" & AccountNumber
        
        If FSO.FolderExists(CustomerFolder) Then
            Debug.Print PDFfile.Name; " moved to " & CustomerFolder
            PDFfile.Move CustomerFolder & "\"
        Else
            Debug.Print PDFfile.Name; " moved to " & NewCustomerDocumentsFolderPath
            PDFfile.Move NewCustomerDocumentsFolderPath & "\"
        End If
    
    Next
 
End Sub
I think the problem with your code is this line:
Code:
If Folder.Name Like "*[#]" & AccountNumber Then
The * matches zero or more characters. The [#] matches a single literal # character. Therefore it is looking for folder names with a literal # preceding the account number, e.g. "#1234" or "xx#1234". You made no mention of this # character in your explanations, so my code looks for the folder with the exact account number, without a # character preceding it.
 
Upvote 0
Thanks so much for the reply AlphaFrog :), I'm going to email the code to my work email and try it out tomorrow. I'm at home now and, since the last test worked at home, but not at work, I don't really see a reason to try it at home now. Again, thanks and I'll keep you updated and whether or not I got it to work! If it works it would be a life saver!

You should try it a home just to confirm that the new code is functional. I may have not fully understood your folder organizational structure or file naming convention.

Also, I just want to confirm the group sub folders are named exactly like Aa-Al or Am-Az. No spaces?
 
Upvote 0
You should try it a home just to confirm that the new code is functional. I may have not fully understood your folder organizational structure or file naming convention.

Also, I just want to confirm the group sub folders are named exactly like Aa-Al or Am-Az. No spaces?

Yup, you were correct and the macro worked perfectly!!! :) I tested one file on one run, then four on the second run and they all filed exactly as they should. Unfortunately, though, the last macro worked perfectly at home too, so tomorrow we'll have to play the waiting game. I guess the only issue, which I can figure out myself, is to make sure that only PDF's get filed. I actually don't have PDFs on my home computer, so I used txt, rtf, and docx files. The only reason it's important at work is because for some reason thumbs.db gets selected sometimes to be filed (I know because, even though the old macro didn't work, when I hovered in debug mode over paths it listed it as the target file). Otherwise everything seems great, thanks again for all of your help and I'll keep you notified!
 
Upvote 0
A lot of description, but you haven't really answered the destination folder question explicitly enough.

Summarising:

SM8.PDF -> C:\Users\MIS\Documents\MACRO TEST\CustomerDocs\SM-SZ\8\
BU293049.PDF -> C:\Users\MIS\Documents\MACRO TEST\CustomerDocs\BM-BZ\293049\
HE5468.PDF -> C:\Users\MIS\Documents\MACRO TEST\CustomerDocs\HA-HL\5468\

Is that right? If so, along the same line's as Alphafrog's code:

Code:
Const PDFscansFolderPath As String = "C:\Users\MIS\Documents\MACRO TEST\ScannerDoc"
Const CustomerDocumentsFolderPath As String = "C:\Users\MIS\Documents\MACRO TEST\CustomerDocs"
Const NewCustomerDocumentsFolderPath As String = "C:\Users\MIS\Documents\MACRO TEST\NewCustomers"


Public Sub Move_PDF_Files()
 
    Dim FSO As Object
    Dim PDFfile As Object
    Dim SurnamePrefix1 As String, SurnamePrefix2 As String
    Dim AccountNumber As String
    Dim CustomerFolder As String
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
        
    For Each PDFfile In FSO.GetFolder(PDFscansFolderPath).Files
        
        'Parse Surname letters and account number from file name
        SurnamePrefix1 = Mid(PDFfile.Name, 1, 1)
        SurnamePrefix2 = Mid(PDFfile.Name, 2, 1)
        AccountNumber = Mid(FSO.GetBaseName(PDFfile), 3)
        
        'Define Surname subgroup folder e.g. AA-AL or AM-AZ
        If UCase(SurnamePrefix2) <= "L" Then    'test if 2nd letter is between A-L
            CustomerFolder = SurnamePrefix1 & "A-" & SurnamePrefix1 & "L"
        Else                                    'else the 2nd letter is between M-Z
            CustomerFolder = SurnamePrefix1 & "M-" & SurnamePrefix1 & "Z"
        End If
        
        'Construct full destination folder from base folder, surname subgroup and account number
        
        CustomerFolder = CustomerDocumentsFolderPath & "\" & CustomerFolder & "\" & AccountNumber
        
        If FSO.FolderExists(CustomerFolder) Then
            Debug.Print PDFfile.Name; " moved to " & CustomerFolder
            PDFfile.Move CustomerFolder & "\"
        Else
            Debug.Print PDFfile.Name; " moved to " & NewCustomerDocumentsFolderPath
            PDFfile.Move NewCustomerDocumentsFolderPath & "\"
        End If
    
    Next
 
End Sub
I think the problem with your code is this line:
Code:
If Folder.Name Like "*[#]" & AccountNumber Then
The * matches zero or more characters. The [#] matches a single literal # character. Therefore it is looking for folder names with a literal # preceding the account number, e.g. "#1234" or "xx#1234". You made no mention of this # character in your explanations, so my code looks for the folder with the exact account number, without a # character preceding it.

Thanks for helping too John! The file formats in the customer directly aren't like the ones you in your post, they would be more like this:
SM8.PDF -> C:\Users\MIS\Documents\MACRO TEST\CustomerDocs\SM-SZ\Smith, John #8\
BU293049.PDF -> C:\Users\MIS\Documents\MACRO TEST\CustomerDocs\BM-BZ\Burton, Harold #293049\
HE5468.PDF -> C:\Users\MIS\Documents\MACRO TEST\CustomerDocs\HA-HL\Helms, Charles #5468\

I would rather they were formatting the way you put it, it would be easier to file and we use HDMS (a healthcare management program), so the billers should know the account numbers for every patient and not really need to rely on names anyway. The [#] was my fault, I did mention to LockeGarmin
that the file names had a #. The exact format is: lastname, firstname #accountnumber

But to be honest, I don't think it matters if it reads "#" anyway, as long as it can match the exact number it should be fine. Sorry for any confusion, let's home the code works tomorrow!
 
Upvote 0
The only reason it's important at work is because for some reason thumbs.db gets selected sometimes to be filed (I know because, even though the old macro didn't work, when I hovered in debug mode over paths it listed it as the target file).

The thumbs.db file may be the cause for the error. I don't think it can me moved and the code would try to move it to the new customer folder. The code below filters for PDF files.

Code:
Sub Move_Patient_PDF_Files()
    
    Dim fsoFile As Object, counter As Long
    Dim SurnamePrefix1 As String, SurnamePrefix2 As String
    Dim AccountNumber As String, GroupFolder As String, CustFolder As String
    
    Const ScannerDocsPath As String = "C:\Users\MIS\Documents\MACRO TEST\ScannerDocs"
    Const CustomerDocumentsPath As String = "C:\Users\MIS\Documents\MACRO TEST\CustomerDocs"
    Const NewCustomerDocumentsPath As String = "C:\Users\MIS\Documents\MACRO TEST\NewCustomers"
    
    With CreateObject("Scripting.FileSystemObject")
    
        'loop through each file in scanner folder
        For Each fsoFile In .GetFolder(ScannerDocsPath).Files
           If LCase(fsoFile.Name) Like "*.pdf" Then
            
                'Parse Surname letters and account number from file name
                SurnamePrefix1 = Mid$(fsoFile.Name, 1, 1)       'Surname 1st letter
                SurnamePrefix2 = Mid$(fsoFile.Name, 2, 1)       'Surname 2nd letter
                AccountNumber = Mid$(.GetBaseName(fsoFile), 3)  'Account number
                
                'Define Surname subgroup folder e.g.; Aa-Al or Am-Az
                If Asc(UCase(SurnamePrefix2)) < 77 Then 'test if 2nd letter is between A-L
                    GroupFolder = SurnamePrefix1 & "a-" & SurnamePrefix1 & "l"
                Else                                    'else the 2nd letter is between M-Z
                    GroupFolder = SurnamePrefix1 & "m-" & SurnamePrefix1 & "z"
                End If
                GroupFolder = CustomerDocumentsPath & "\" & GroupFolder & "\"
                
                'Find folder based on exact Account number and move file
                CustFolder = Dir(GroupFolder & "*#" & AccountNumber, vbDirectory)
                If CustFolder <> "" Then    'Test if exact account number folder found
                    'Move file to account number folder
                    fsoFile.Move GroupFolder & CustFolder & "\"
                Else
                    'move file to new customer folder
                    fsoFile.Move NewCustomerDocumentsPath & "\"
                End If
                
                counter = counter + 1   'Count files
            
            End If
        Next fsoFile
    End With
    
    MsgBox counter & " files moved. ", , "Move Customer PDF Files Complete"
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,110
Messages
6,123,139
Members
449,098
Latest member
Doanvanhieu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top