excel vba help

shariq

New Member
Joined
Mar 10, 2022
Messages
30
Office Version
  1. 2010
Platform
  1. Windows
hello experts i need your help please understand my problem i have thousand of file with diffrents extention like (jpg.pdf.chrome.outloolk mail) and i want to move them into the folder with the match of first 5 to 6 digit code to the folder.

my file name are like this..
09437 experience letter
04298 experience letter
07897 contract letter
00058 Bsc dgree
CO4298 experience letter
C02142 certificate

my folder name are like this
09437 john
04298 alberto
07897 micheal
00058 christopher
C04298 jackson
C02142 polard

iam try this code but its not working fine this script create new folder with same name of file but i have already created i want just read this code not read full name if file code match with folder code then move file into the folder.
script are given below..


Sub CopyFiles()

'Declare variables
Dim FSO As Object
Dim SourceFolder As Object
Dim DestinationFolder As Object
Dim File As Object

'Set the source and destination folders
Set FSO = CreateObject("Scripting.FileSystemObject")
Set SourceFolder = FSO.GetFolder("c:\users\employee.records\desktop\shariq")
Set DestinationFolder = FSO.GetFolder("D:\employee.records\desktop\back up by immad")

'Loop through all the files in the source folder
For Each File In SourceFolder.Files
'Extract the file name without the extension
Dim FileNameWithoutExtension As String
FileNameWithoutExtension = FSO.GetBaseName(File.Name)

'If a folder with the same name as the file doesn't exist in the destination folder, create it
If Not FSO.FolderExists(DestinationFolder & "\" & FileNameWithoutExtension) Then
Set DestinationSubFolder = FSO.CreateFolder(DestinationFolder & "\" & FileNameWithoutExtension)
Else
'If the folder already exists, set it as the destination folder
Set DestinationSubFolder = FSO.GetFolder(DestinationFolder & "\" & FileNameWithoutExtension)
End If
'Copy the file to the destination folder
File.Copy DestinationSubFolder & "\" & File.Name
Next

End Sub
 
hi holger hope you are fine holger i can face again same issue look this screen shoot .
 

Attachments

  • IMG-20230129-WA0000.jpg
    IMG-20230129-WA0000.jpg
    203.9 KB · Views: 10
Upvote 0

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hi shariq,

does this happen with every code you try to run? As no codeline is highlighted in yellow in the pictures the error might be caused by an add-in installed or another open workbook.

I rebuild a sample based on the data you supplied in the opening post, adding 4 of the given folders to the destination path and run the code. Two new folders are created, the files are copied into the corresponding folders. I checked and rechecked the folders but the result for me was always the same: files had been copied, no error raised.

I'm sorry I don't have a clue what happens on your computer. And I can't spot any reason why my code should raise this error on your computer.

Holger
 
Upvote 0

Forum statistics

Threads
1,223,902
Messages
6,175,278
Members
452,629
Latest member
SahilPolekar

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