Look for value in cell then move file to correct folder

Nick70

Active Member
Joined
Aug 20, 2013
Messages
299
Office Version
  1. 365
Platform
  1. Windows
Hi,

I am using a previous code given to me to sort files in different folders but unfortunately it does not seem to work for new criteria.

I need a code that opens files in a specific folder and then depending on whether the cell value in Cells(2, 3) is "ClientA" or "ClientB", the file will be moved to folder ClientA or folder ClientB.

Code below:
VBA Code:
Sub Sort()

Dim fPathSrc As String
    Dim fPathA As String
    Dim fPathB As String
    Dim strFile As String
    Dim Cellx As Long
  
    Application.ScreenUpdating = False

'   Set filepaths
    fPathSrc = "C:\Users\user\Desktop\Folder\Macros\"           'Source file path
    fPathA = "C:\Users\user\Desktop\Folder\Macros\ClientA\"     'Path for ClientA
    fPathB = "C:\Users\user\Desktop\Folder\Macros\ClientB\"     'Path for ClientB
  
'   Loop through all Excel files in source path
    strFile = Dir(fPathSrc & "*.xl*")
    Do While Len(strFile) > 0
'       Open file
        Workbooks.Open Filename:=fPathSrc & strFile
'       Look at Cellx
        Cellx = Cells(2, 3).Value
'       Close workbook
        ActiveWorkbook.Close
'       Move to correct folder
        Select Case Cellx
            Case "ClientA"    'ClientA
                Name fPathSrc & strFile As fPathA & strFile
            Case "ClientB"    'ClientB
                Name fPathSrc & strFile As fPathB & strFile
        End Select
'       Go to next file
        strFile = Dir
    Loop
  
    Application.ScreenUpdating = True
  
    MsgBox "Macro done!"
  
End Sub

Variable Cellx is cell C2 (or cells(2, 3)), if this cell has text ClientA then move file to folder ClientA otherwise if cell has text ClientB move file to folder ClientB.

I get an error message at line

Code:
Cellx = Cells(2, 3).Value

Can someone me help fix this code or send me new code that works (either way would be great)?

Thanks,
N.
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try changing this
VBA Code:
Dim Cellx As Long
to String.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,861
Members
449,052
Latest member
Fuddy_Duddy

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