I'm using excel VBA. I want to press a button that opens another file directly without the effect of "choosing file window".
This is the current code:
Sub loadFile_click()
Workbooks.Open("C:\Users\GIL\Desktop\ObsReportExcelWorkbook.xlsx")
End Sub
In this case, the file is in the same folder as the main file.
Is there any way to do it without entering the file's path?
Steps to Open a Workbook using VBAType a dot (.) after that and select the Open method from the list. Specify the file path in the first argument and make sure to enclose it in double quotation marks. In the end, run the code to open the workbook.
Step 1: Open Excel. Step 2: Add a shape (Read Text File) to your worksheet . Step 3: Right-click on “Read Text file” and “Assign Macro..” Step 7: Adjust column width in your excel file.
If the file name is fixed you can use the ActiveWorkbook.Path function to return the path of the current workbook:
Dim FileName as string
FileName = ActiveWorkbook.Path & "\myfile.xlsx
Check if you need that extra slash character.
If the file is in the same folder as the document containing your VBA macro, use
ThisWorkbook.Path
for example:
Workbooks.Open(ThisWorkbook.Path & "\ObsReportExcelWorkbook.xlsx")
(this works even if the document is not the active one any more, or you changed the current directory).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With