copy file gives bad file name or number

jolivanes

Well-known Member
Joined
Sep 5, 2004
Messages
1,917
Office Version
  1. 2013
  2. 2007
Platform
  1. Windows
If I use this
Code:
Sub CopyToDifferentFolder()
Dim SrceFile
Dim DestFile
SrceFile = """" & ActiveCell.Offset(, 3).Value & "\" & ActiveCell.Value & " - " & ActiveCell.Offset(, 1).Value & """"
DestFile = """" & Range("M2").Value & "\" & ActiveCell.Value & " - " & ActiveCell.Offset(, 1).Value & """"
Range("M15").Value = SrceFile
Range("M16").Value = DestFile
End Sub
The outcome is
M15 = "D:\Music\My MusicMP3\French\Adamo - A demain sur la lune.mp3"
M16 = "C:\Temp Music\Adamo - A demain sur la lune.mp3"

However, when I change the code to
Code:
Sub CopyToDifferentFolder()
Dim SrceFile
Dim DestFile
SrceFile = """" & ActiveCell.Offset(, 3).Value & "\" & ActiveCell.Value & " - " & ActiveCell.Offset(, 1).Value & """"
DestFile = """" & Range("M2").Value & "\" & ActiveCell.Value & " - " & ActiveCell.Offset(, 1).Value & """"
FileCopy SrceFile, DestFile
End Sub

I get an Error 52 (Bad filename or number) and it highlights
Code:
FileCopy SrceFile, DestFile

I have tried on 2 different computers (2003 and 2007) with the same results.
I checked all the cells and none have leading/trailing spaces (ran TRIM on all of them)

When hardcoding the folder and file names in the code, it works as expected.

Can someone tell me where I went wrong please.

Thank you very much.

John
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,449
Don't include the quotes when building the strings.

Code:
Dim SrceFile As String
Dim DestFile As String
SrceFile = ActiveCell.Offset(, 3).Value & "\" & ActiveCell.Value & " - " & ActiveCell.Offset(, 1).Value
DestFile = Range("M2").Value & "\" & ActiveCell.Value & " - " & ActiveCell.Offset(, 1).Value
 

jolivanes

Well-known Member
Joined
Sep 5, 2004
Messages
1,917
Office Version
  1. 2013
  2. 2007
Platform
  1. Windows
Right on the button. Here I thought I was doing good by including the quotes.
Thank you very much AlphaFrog.

Regards
John
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,596
Messages
5,832,638
Members
430,149
Latest member
amitk1

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
Top