copy file gives bad file name or number

jolivanes

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

Some videos you may like

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,275
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,612
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
 

Watch MrExcel Video

Forum statistics

Threads
1,108,491
Messages
5,523,255
Members
409,506
Latest member
reneekeane

This Week's Hot Topics

Top