Results 1 to 6 of 6

Extract file path from sheet location - then open another file in the same directory

Threaded View

  1. #1
    Registered User
    Join Date
    05-05-2009
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    68

    Question Extract file path from sheet location - then open another file in the same directory

    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
    Last edited by mick86; 09-18-2012 at 12:21 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1