VBA Copy/ Pasting w/ Tricky File Name

Littlemalky

Board Regular
Joined
Jan 14, 2011
Messages
223
Hey can someone help me out with perfecting this line of code because I continue to get the error, "Subscript out of range", and I'm not sure what I'm doing wrong. I'm assuming that this code isn't exactly synonymous with the actual file name, but I don't know how to correct it.

One workbook that I'm copying the data from is called "detail w add.xlsx". The workbook that I'm pasting into is called, "Fullfillment 20110301 - Domestic.xlsx". The numbers will always be the current day's date formatted yyyymmdd. Can anyone help?

Code:
Sub DetailWAdd()
        Workbooks("detail w add.xlsx").Cells.Copy _
  [COLOR=red]      Destination:=Workbooks("Fulfillment " & Format(Date, "yyyymmdd") & "- Domestic" & ".xlsx").Worksheets("detail w add").Range("A1")
[/COLOR]    Application.CutCopyMode = False
    Workbooks("detail w add.xlsx").Close SaveChanges:=False 'Closes without saving changes
End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Upon first glance, it looks like you forgot to include a space before the hyphen:

Code:
Sub DetailWAdd()
        Workbooks("detail w add.xlsx").Cells.Copy _
        Destination:=Workbooks("Fulfillment " & Format(Date, "yyyymmdd") &[B][COLOR=red] " - Domestic"[/COLOR][/B] & ".xlsx").Worksheets("detail w add").Range("A1")
    Application.CutCopyMode = False
    Workbooks("detail w add.xlsx").Close SaveChanges:=False 'Closes without saving changes
End Sub
 
Upvote 0
I just tried to correct that as you suggested; however, when I run this macro the code is still highlighted yellow. Do I need to reflect the spaces differently?
 
Upvote 0
Is your destination workbook open? If not, you need to open the workbook before VBA can paste to it.
 
Upvote 0
Yes, I have that line of code before everything else, I just didn't post it because I didn't want to disclose any kind of path or company information.

Code:
Sub DetailWAdd()
    Workbooks.Open filename:="[URL="file://\\Cobalt\Groups\Materials\DMS\Jesse's"]...[/URL]\Domestic\detail w add.xlsx"
    Workbooks("detail w add.xlsx").Cells.Copy _
        Destination:=Workbooks("Fulfillment " & Format(Date, "yyyymmdd") & " - Domestic" & ".xlsx").Worksheets("detail w add").Range("A1")
    Application.CutCopyMode = False
    Workbooks("detail w add.xlsx").Close SaveChanges:=False 'Closes without saving changes
End Sub
 
Upvote 0
"detail w add.xlsx" is open, but is "Fullfillment 20110301 - Domestic.xlsx" open?
 
Upvote 0
Oh I apologize, yes that one is open as well. I haven't written the code to open it yet as I'm just testing individual macros with the file open from me manually doing it. I still need to figure out how to open the most recent dated file, but I think that is somewhere on the forumns here.
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

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