Hi
I'm new to excel land self taught, I'm having trouble with a Macro I wish to create which will extract the file path from the active workbook and use this file path to open a new file with a predetermined name that sits in the same directory.
The code works fine when I manually enter the file directory but I wish to obtain the file directory from the active work books location.
Option Explicit
Option Compare Text
Function IsWbOpen(wbName As String) As Boolean
Dim i As Long
For i = Workbooks.Count To 1 Step -1
If Workbooks(i).Name = wbName Then Exit For
Next
If i <> 0 Then IsWbOpen = True
End Function
' Parts below are where the trouble lies
Sub My_Attempt()
Dim Main As Workbook ' The first workbook will be allocated to Main
Set Main = ActiveWorkbook
Dim wbpath As String
wbpath = ThisWorkbook.Path
Dim wb As Workbook, strName As String, strPath As String
strName = "The workbook mane which I will open goes here"
strPath = Left$(wbpath, InStrRev(wbpath, "\")) '<<== I think this part may be wrong, The code works if I simply enter the file path in quotation marks here
If IsWbOpen(strName) Then
Main.Activate
'<<== Do this and that
Else
Set wb = Workbooks.Open(strPath & strName) <<== This is where the error occurs
'<<== Do this and that
End If
End Sub
Bookmarks