Macro not working when using on another sheet

winstela

New Member
Joined
Feb 24, 2019
Messages
28
Office Version
  1. 2016
Platform
  1. Windows
Morning everyone

I am hoping this is a quick one to resolve for the experts here.

I have a code that is using a cell reference as part of a string to open another workbook.

When I run the code from the same sheet (Template) that holds the reference its work, but when I try to run the code from a different worksheet (Home) in the same workbook, the message cannot find file pops up (as per the code)

I have tried references Thisworkbook but get the same results. its seems that the (template) sheet needs to be activate but I don't want the user to see that. I want the user to see (Home) sheet until the other workbook has opened.

Heres my code
VBA Code:
Sub OpenLatestFile()

    'Declare the variables
    Dim MyPath As String
    Dim MyFile As String
    Dim LatestFile As String
    Dim LatestDate As Date
    Dim LMD As Date
    Dim sFolderPath As String
Dim sIndexNumber As String
 Dim wb As Workbook


' Get the index number
sIndexNumber = Sheets("Template").Cells(1, 12)
    
' Create the file path using the IndexNumber
sFolderPath = "\\mcuk-adc\Prod_Results\01 Value Stream\01 Bowler\" + Mid(sIndexNumber, 1, 4) + "\" + Mid(sIndexNumber, 5, 5) + "\" + Cells(1, 11) + "\"
    
    'Specify the path to the folder
    MyPath = sFolderPath
    
    'Make sure that the path ends in a backslash
    If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
    
    'Get the first Excel file from the folder
    MyFile = Dir(MyPath & "*.xls", vbNormal)
    

    If Len(MyFile) = 0 Then
        MsgBox "Sorry No files were found...", vbExclamation
        Exit Sub
    End If
    
  
    'Loop through each Excel file in the folder
    Do While Len(MyFile) > 0
    
        'Assign the date/time of the current file to a variable
        LMD = FileDateTime(MyPath & MyFile)
        
        'If the date/time of the current file is greater than the latest
        'recorded date, assign its filename and date/time to variables
        If LMD > LatestDate Then
            LatestFile = MyFile
            LatestDate = LMD
        End If
        
        'Get the next Excel file from the folder
        MyFile = Dir
        
    Loop
    
    'Open the latest file
     On Error GoTo ErrorHandler
'Call function to check if the file is open
If Not wbOpen(MyFile, wb) Then Workbooks.Open MyPath & LatestFile, ReadOnly:=True


ErrorHandler:
Exit Sub

End Sub

Its being used with this function
VBA Code:
Function wbOpen1(MyFile As String, wbO As Workbook) As Boolean
    On Error Resume Next
    Set wbO = Workbooks(MyFile)
    wbOpen1 = Not wbO Is Nothing
End Function

Thanks
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
In this line:
VBA Code:
sFolderPath = "\\mcuk-adc\Prod_Results\01 Value Stream\01 Bowler\" + Mid(sIndexNumber, 1, 4) + "\" + Mid(sIndexNumber, 5, 5) + "\" + Cells(1, 11) + "\"
Cells(1, 11) doesn't refer to a specific sheet and will work only when the macro is launched from sheet Template. Change it to:
Code:
sFolderPath = "\\mcuk-adc\Prod_Results\01 Value Stream\01 Bowler\" + Mid(sIndexNumber, 1, 4) + "\" + Mid(sIndexNumber, 5, 5) + "\" + Sheets("Template").Cells(1, 11) + "\"
 
Upvote 0
Solution
Thankyou rollis13, that has worked perfectly!

Appreciate the quick response
 
Upvote 0
Thanks for the positive feedback(y), glad having been of some help.
 
Upvote 0

Forum statistics

Threads
1,214,522
Messages
6,120,022
Members
448,939
Latest member
Leon Leenders

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top