Excel VBA runtime error: Cannot access the file.

Syed Nadeem 1 Reputation point

Here is the code I'm using. It's a worksheet which has few buttons and the input path is chosen from one of the cells and is variable. I'm getting a run time error stating file not accessible or it's read-only.

Sub accountant()

Dim wb As Workbook  
Dim wbi As Workbook  
Dim ws As Worksheet  
Dim wsi As Worksheet  
Dim wsm As Worksheet  
Dim InputFilePath As Variant  
Set wb = ThisWorkbook  
Set wsm = wb.Sheets("Mapping")  
Set wsi = wb.Sheets("Input")  
InputFilePath = Trim(wsi.Cells(2, 2).Value)  
Set wbi = Workbooks.Open(InputFilePath)  
Set ws = wbi.Sheets("Procount_Monthly_PermianProduct")  
wsEndRow = ws.Cells(Rows.Count, 1).End(xlUp).Row  
Path = Application.ActiveWorkbook.Path  
'ws.Range("AP7:AP" & wsEndRow).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=wsm.Range("A2"), Unique:=True  
ws.Range("AP7:AP" & wsEndRow).Copy Destination:=wsm.Range("A2")  
wsm.Range("A:A").RemoveDuplicates Columns:=1, Header:=xlNo  
wsmEndRow = wsm.Cells(Rows.Count, 1).End(xlUp).Row  

End Sub

Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,709 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Oskar Shon 866 Reputation points MVP

    Check i f your cell B2 have whole path and filename with extension
    your open commend with InputFilePath requires it.

    0 comments No comments