How to open another workbook in same folder?

tgregoire

New Member
Joined
Oct 3, 2021
Messages
7
Office Version
  1. 2013
Platform
  1. Windows
hi,
i am trying to open a file in the same folder. i have tried the below code and it is not working for me.
i get runtime error 1004
method 'open' of object 'workbook' failed

i have tried several other things and none seem to work. i can get it to open but that is only if i hardcode the file path. the issue with that is the user can move the workbook and file that needs opening around so it needs to be dynamic.

also could use some help closing it once i extract the data i need.



Workbooks.Open Filename:=ThisWorkbook.Path & "\openthis.xls"

i need to complete this project soon.
please help!
THANK YOU!! :)
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hi

Are you sure on your file extension? is it not xlsx

Workbooks.Open Filename:=ThisWorkbook.Path & "\openthis.xlsx"
 
Upvote 0
also

close

Workbooks("openthis.xlsx").Close savechanges:=True
 
Upvote 0
ok, so your code was wrong, since the x was missing on your original post.

so it now works?

dave
 
Upvote 0
That is strange the, since you are not doing anything wrong

copy and paste the below just to make sure.

just to confirm, the "openthis.xlsx" is saved in the same directoty as the workbook with this macro?


VBA Code:
Sub open_me()
Workbooks.Open Filename:=ThisWorkbook.Path & "\openthis.xlsx"
End Sub
Sub close_me()
Workbooks("openthis.xlsx").Close savechanges:=True
End Sub
 
Upvote 0
i got 2 error messages and still not working. i copied and pasted your code exact.

1633297181592.png


1633297209252.png
 
Upvote 0
So i only get that message if the filename is incorrect

fyi the below the below should open the workbook, copy data and close it again.
although until this error is fixed it is useless to you.

it should work, is your filename 100% correct, and both excel files are in the exact same directoty.

VBA Code:
Sub copy_data()
Application.ScreenUpdating = False
main_file = ThisWorkbook.Name
Workbooks.Open Filename:=ThisWorkbook.Path & "\openthis.xlsx"


'copy data
Range("a1").Copy
Workbooks(main_file).Activate
Range("a1").PasteSpecial
'copy data


Workbooks("openthis.xlsx").Close savechanges:=True
Application.ScreenUpdating = True
End Sub
 
Upvote 0
files are in the same directory and the file name is 100% correct. any other thoughts on this? the file i am trying to open has given much issues with other parts of my code. things that normally work don't seem to work and has made me spend many many hours looking for work arounds.

any other guesses or ideas on how to do this dynamically as hard coding works and i just cant trust the users not to move the folder around.
 
Upvote 0

Forum statistics

Threads
1,215,331
Messages
6,124,311
Members
449,152
Latest member
PressEscape

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