Run-time Error 91 when opening file

Alex Lee

New Member
Joined
Dec 22, 2023
Messages
3
Office Version
  1. 365
Platform
  1. Windows
When I run the following routine, the file opens with no error:

Sub Test1()

Workbooks.Open (ThisWorkbook.Path & "\Export_CitiMD_20231221_114052.csv")

i = 1

End Sub


However, when I try to put the name of the CSV file into a cell, immediately AFTER the CSV file opens, Run-time error 91 pops up. May I know why?

Sub FindNewISINs()

Dim currentWorkbook As Workbook
Dim csvWorkbook As Workbook
Dim csvFileName As String

Set currentWorkbook = ThisWorkbook
csvFileName = currentWorkbook.Path & "\" & currentWorkbook.Sheets("Control").Range("G44").Value

Set csvWorkbook = Workbooks.Open(csvFileName) ' Error occurs at this line. CSV file did open. But Error 91 comes up and the rest of the code cannot be executed.

' <Other codes>

End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Welcome to the Board!

I tested it out and your code works just fine for me.
Are you sure that the name is EXACTLY the same?
Do you have the ".CSV" extension as part of the file name?

An easy way to confirm everything looks just right, add a MsgBox to your code to return the full path and file name you are building, i.e.
Rich (BB code):
Sub FindNewISINs()

Dim currentWorkbook As Workbook
Dim csvWorkbook As Workbook
Dim csvFileName As String

Set currentWorkbook = ThisWorkbook
csvFileName = currentWorkbook.Path & "\" & currentWorkbook.Sheets("Control").Range("G44").Value
MsgBox csvFileName

Set csvWorkbook = Workbooks.Open(csvFileName) ' Error occurs at this line. CSV file did open. But Error 91 comes up and the rest of the code cannot be executed.

' <Other codes>

End Sub
 
Upvote 0
The file name was OK. That's why the CSV file did open. But immediately after that, the code stopped running due to the Run-time Error 91.
 
Upvote 0
More specifically, the sequence of event is this after adding the MsgBox command:

-> CSV file opens -> MsgBox pops up, showing the correct path name -> Run-time Error 91 occurs -> the rest of the code aborted.

Anyway, I found the problem. It turns out that about 5 rows down where the error occurs, there is one line of code that has an undefined variable used in an IF statement. The error is probably triggered by that.

Thanks for the help.
 
Upvote 0

Forum statistics

Threads
1,215,076
Messages
6,122,983
Members
449,092
Latest member
Mr Hughes

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