简体   繁体   中英

Workbook.Open can not find the file

I am running the following code. The code is for finding some particular values in all the Excel sheets in a folder.

It gives me the error

"Sorry, we couldn't find the file. Is it possible it was moved, renamed or deleted?"

when it executes

Set wbSlave = Workbooks.Open(filename:=myfilname)

Please help.

Sub find_cells()
    Dim i, j As Integer
    Dim cell_content As String
    Dim total_values As Long
    Dim cell_location As Range
    Dim cell_address As String
    Dim sht As Worksheet
    Dim myfilname As String
    Dim fldrpath As String
    Dim wbMaster As Workbook
    Dim wbSlave As Workbook
    Dim currentfilename As String
    
    Set wbMaster = ThisWorkbook
    j = 3
    total_values = Application.WorksheetFunction.CountA(wbMaster.Sheets("Engine").Range("A:A")) 
    fldrpath = "C:\test\" 
    myfilname = Dir(fldrpath & "*.xls*")
    
    Application.ScreenUpdating = False
    
    Do While myfilname <> ""
        'myfilname = Dir()
        Set wbSlave = Workbooks.Open(filename:=myfilname)
    
            For Each sht In wbSlave.Sheets
                For i = 2 To total_values
                    cell_content = wbMaster.Sheets("Engine").Range("A" & i).Value
                        With sht.UsedRange
                            Set cell_location = .Find(cell_content, LookIn:=xlValues, MatchCase:=False, SearchFormat:=False)
                    
                                If Not cell_location Is Nothing Then               
                                    cell_address = cell_location.Address
    
                                        Do              
                                            wbMaster.Sheets("Sheet1").Columns(j).Value = cell_location.EntireColumn.Value
                                            j = j + 1
                    
                                            Set cell_location = .FindNext(cell_location)                               
                    
                                        Loop While Not cell_location Is Nothing And cell_location.Address <> cell_address
                
                                End If
                        End With
                        Set cell_location = Nothing
                      
                If i = total_values Then Exit For
                Next i
            Next
                
    myfilname = Dir()
    Loop
End Sub

The Dir function returns only the matching filename - no path. Then if you try to open it and you are currently not in a correct folder, you get this error.

Set a watch to the variable myfilname or just add Debug.Print myfilname and you see it right away.

https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/dir-function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM