Check i f your cell B2 have whole path and filename with extension
your open commend with InputFilePath requires it.
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)
wsm.Range("A:A").ClearContents
wsm.Range("B3").ClearContents
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
1 answer
Sort by: Most helpful
-
Oskar Shon 866 Reputation points MVP
2022-12-01T20:54:42.017+00:00