VBA code to move files from folder to subfolder based in partial name in both file and folder

LMFM

New Member
Joined
May 27, 2021
Messages
1
Office Version
  1. 2013
Platform
  1. Windows
Hi everyone,
Before registering and deciding to post a thread I read through some posts in the forum with specific criteria search. I saw several examples but I was unable to find a case similar to mine. I apologize in advance if there are already. If you could please support and point me out in the right direction it would be great.

Issue: I am trying to copy files from one folder to another folder based on the name of the file (P_7072012132.pdf) and destination folder (803 - P_7072012132)

Specifications:
File name has always the same format "P_*.pdf" - example: P_7072012132.pdf. The only part that changes is the numbering of the document itself. Numbers are unique.

Source folder is always the same C:\Users\Pc\Desktop\Change
Destination folder might vary C:\Users\Pc\Desktop\Change\2021\Billing documents\"CASE NR - DOCUMENT NUMBER"(other data)

Explanation for the variation: i already have a macro that creates the folders based on the date of creation and increasing the case number. Meaning, for example above, folder "803 - P_7072012132 (other data)" was created. Other examples: 802 - P_7072012131; 803 - P_7072012132; 804 - P_7072012149.

By other data in each folder case I mean other specifications which i believe are not relevant for the coding, but please let me know otherwise. Example of complete name of folder is 803 - P_7072012132 7073000013 4560079

Documents are always pdfs

Thank you in advance for your support.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hi, a VBA demonstration for starters :​
VBA Code:
Sub Demo1()
  Const E = ".pdf", P = "C:\Users\Pc\Desktop\Change\", D = P & "2021\Billing documents\"
    Dim F$(), S$, N&
  ReDim F(1 To Rows.Count)
        S = Dir(P & "P_*" & E):  While S > "":  N = N + 1:  F(N) = S:  S = Dir:  Wend
    For N = 1 To N
        S = Dir(D & "* " & Replace(F(N), E, "") & " *", vbDirectory)
        If S > "" Then Name P & F(N) As S & "\" & F(N)
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,869
Messages
6,122,015
Members
449,060
Latest member
LinusJE

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