• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Open the Excel sheet with file Name

webmax

Member
Hi

i have Folder Name "TEST" in that folder i have excel sheet (i have create workbook name test macro).
In that Folder i have another excel file Name called "TESTFILE.XLS"
With the help of macro i want to open the "TESTFILE.XLS"

I have searched the macro but all has given to put the path name like "C:\"
But I want the Macro with Current Folder with File Name to open the file.


Regards
Shahul
 
I think this will help you out.
Code:
Sub OpenFile()
'Opens a file called TESTFILE.XLS from same folder as this workbook
Dim fPath As String
Const fName As String = "TESTFILE.XLS"

fPath = ThisWorkbook.Path & fName

'Check if file path is legit, then open
If Dir(fPath) <> "" Then Workbooks.Open fPath
End Sub
 
Hi i tried that the macro is not working

I stored the file Name as TESTFILE.XLS in the same folder.
 
Hi Shahul..

Just a lil modification Luke's Code.. :)

fPath = ThisWorkbook.Path & "\" & fName
 
hi i need one more help

i want to activate the open workbook by workbook name how to do that in excel macro code.
 
Hi Shahul..

Let me know, if you need further details..

Code:
Sub OpenFile()
    'Opens a file called TESTFILE.XLS from same folder as this workbook
    Dim fPath As String
    Const fName As String = "Test1.XLSx"
   
    fPath = ThisWorkbook.Path & "\" & fName
   
    'Check if file path is legit, then open
    If Dir(fPath) <> "" Then Workbooks.Open fPath
   
    'By default at the time of opening a file .. its always active
    MsgBox "Active Window is " & ActiveWindow.Parent.Name
     
    'Still if you wnat to swap to another workbook..
    Application.Windows(ThisWorkbook.Name).Activate
    MsgBox "Now Active Window is " & ActiveWindow.Parent.Name
End Sub

PS: Save the file before proceed..
 
Ugh, thanks Deb for catching my mistake. I keep forgetting the .Path doesn't include the trailing slash. :(
 
Back
Top