closing a file using the entire file path

lillypop

New Member
Joined
Jul 20, 2018
Messages
14
Hello

I tried to close a file in a folder. Part of folder path is named using cell ranges in another worksheet. It keeps on giving me an error message 'run-time error 9, script out of range'.

My code runs fine, until the last line which is to close a specific file (bolded line below):
codes are long, so I pasted only the relevant codes here. Any idea on what should be changed?


sub macro2()

dim fcurrentyear As String
dim fyyyymmdd As String
dim fdate = InputBox("Enter YYYYMMDD")
dim fdate1 = fdate & "data.xlsm"

fcurrentyear = Workbooks("total.xlsm").Worksheets("Main").Range("j2")
fyyyymmdd= Workbooks("total.xlsm").Worksheets("Main").Range("j2")

'''open some files here'''

Workbooks("
P:\users\" & fcurrentyear & "" & fyyyymmdd & "" & "test" & "" & fyyyymmdd & "_data.xlsm". Close savechanges:=false

end sub()
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Inside the brackets should be a string comprising the file name including extension but excluding the folder path
 
Last edited:
Upvote 0
Can you post the code that opens the workbook(s)?
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,380
Members
449,080
Latest member
Armadillos

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