My vba script in myMacro.xls Workbooks.Open Method work well as below,
Workbooks.Open Filename:="D:\ExcelMacroProj\myTest.xls", ReadOnly:=True
But when I try to change the Filename value to a new path as below, but all my practices didn’t work. Show Run time error 1004.
Workbooks.Open Filename:="myTest.xls", ReadOnly:=True
or
Workbooks.Open Filename:="./myTest.xls", ReadOnly:=True
or
Workbooks.Open Filename:=".\myTest.xls", ReadOnly:=True
Actually myMacro.xls and myTest.xls was placed in the same folder. That’s why I want to change to a flexible folder directory.
how could I fix this issue? Appreciated for your read and reply.
You might try using
ThisWorkbook.Pathto make an absolute path. It returns the folder path of the workbook running the macro (excluding the filename). Something like this should work:Make sure to include a backslash, since the workbook path doesn’t end with one.