# Office 365 >  > [SOLVED] How to copy Jpeg files from one folder to another folder with file name reference in excel

## raghavanhr

Hi all,

How to copy Jpeg files from one folder to another folder with file name reference in excel. Eg. as below

I want to do medical insurance for my employees, for which I need to send their photos. I have all photos of staff and workers in one folder with their employee number as file name.jpg.

I want to select only worker's photos and copy them to another folder to share the same to the insurance agency, with reference to their employee number as base reference.

Eg: 
Pic file name in a folder as below
9100567.jpg
9100486
9101592

name list in excel as below
Emp.No
9100567
9100486
9101592

please help me to get this automation to solve my time-consuming work.

Thanks
Raghavan

----------


## xlnitwit

Hi,

Perhaps this



```
Please Login or Register  to view this content.
```

----------


## raghavanhr

Dear xlnitwit,

Tried that macro, but giving runtime error 53, while debugging this line is being selected 

_FileCopy fromPath & cell.Value & "jpg", toPath & cell.Value & "jpg"_

----------


## xlnitwit

I've corrected the code- I had omitted two full stops.

----------


## raghavanhr

so where to correct the code now?

----------


## xlnitwit

Use the revised version above.

----------


## raghavanhr

I have done this code with correction of my file path, but still the same runtime error 53 is coming. I don't know where i'm going wrong. please correct me.

Sub copyPictures()
    Dim cell As Range
    Dim fromPath As String
    Dim toPath As String

    fromPath = "C:\Named"
    toPath = "C:\Named\Selected"

    If Right$(fromPath, 1) <> "\" Then fromPath = fromPath & "\"
    If Right$(toPath, 1) <> "\" Then toPath = toPath & "\"

    For Each cell In Range("A2:A526")

        FileCopy fromPath & cell.Value & ".jpg", toPath & cell.Value & ".jpg"
    Next cell
End Sub

----------


## raghavanhr

Below pic for ref.

Debug.JPGDebug1.JPG

----------


## xlnitwit

Either one of the file names doesn't exist, or one (or both) of the folder paths doesn't.

----------


## raghavanhr

My picture file name is in a folder as below (without ".jpg") for understanding the file format i've given filename.jpg
9100567
9100486
9101592

name list in excel as below
Emp.No
9100567
9100486
9101592

correct me what correction to be made in the code

----------


## raghavanhr

Tried the below by removing &".jpg", but it copied only one file to the specified location.
Sub copyPictures()
    Dim cell As Range
    Dim fromPath As String
    Dim toPath As String

    fromPath = "C:\Named"
    toPath = "C:\Named\Selected"

    If Right$(fromPath, 1) <> "\" Then fromPath = fromPath & "\"
    If Right$(toPath, 1) <> "\" Then toPath = toPath & "\"

    For Each cell In Range("A2:A526")

        FileCopy fromPath & cell.Value, toPath & cell.Value
    Next cell
End Sub

----------


## xlnitwit

If you get an error 53 then the file name is wrong. There is no other cause.

----------


## raghavanhr

By using the below code, when i run the macro it throws runtime error 53 and while pressing debug. it completed the file copy till 99 files to the specified folder, any changes to be made for the cells in rows of 500 plus. i have mentioned A2:A526 and changed the excel numbersplus .jpg for executing and the result as said above.

eg. name in excel sheet as 9100567.jpg and so on...

Sub copyPictures()
    Dim cell As Range
    Dim fromPath As String
    Dim toPath As String

    fromPath = "C:\Named"
    toPath = "C:\Named\Selected"

    If Right$(fromPath, 1) <> "\" Then fromPath = fromPath & "\"
    If Right$(toPath, 1) <> "\" Then toPath = toPath & "\"

    For Each cell In Range("A2:A526")

        FileCopy fromPath & cell.Value, toPath & cell.Value
    Next cell
End Sub

I don't know how to attache my excel file for your ref.

----------


## raghavanhr

Hope I have attached the file. please correct me if the code is wrong.

----------


## xlnitwit

The file will not help. If it completed 99 copies, then the file name of the 100th one must be incorrect.

----------


## raghavanhr

Let me check for that too...

----------


## raghavanhr

Great job Mr Xlnitwit, thanks and you made my day. The job was done. thank you very much.

----------


## xlnitwit

You're welcome. Please remember to mark the thread Solved.

----------


## raghavanhr

Marked my thread 'Solved'

----------

