copy file gives bad file name or number

jolivanes

Well-known Member
Joined
Sep 5, 2004
Messages
3,186
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

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
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
 
Upvote 0
Right on the button. Here I thought I was doing good by including the quotes.
Thank you very much AlphaFrog.

Regards
John
 
Upvote 0

Forum statistics

Threads
1,224,591
Messages
6,179,767
Members
452,940
Latest member
rootytrip

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