Reputation: 1
I am trying to read data from another spreadsheet in the same folder as my currently opened workbook, whenever I run this code I get the error file already open. Is there something wrong with my syntax or anything that might stop this from happening?
Here is the code.
Sub ReadFile()
Infile = ThisWorkbook.Path & "\smallDataset.csv"
Open Infile For Input As #1
Input #1, a, b
Close #1
Debug.Print (a & " " & b)
End Sub
Thanks.
Upvotes: 0
Views: 12827
Reputation: 12167
Try the following code to make sure there is no file open with handle #1
Sub ReadFile()
InFile = ThisWorkbook.Path & "\smallDataset.csv"
Close #1
Open InFile For Input As #1
Input #1, a, b
Close #1
Debug.Print (a & " " & b)
End Sub
Or you use Freefile to get the next number
Sub ReadFile
FileNumber = FreeFile
InFile = ThisWorkbook.Path & "\smallDataset.csv"
Open InFile For Input As FileNumber
Input #FileNumber, a, b
Close #FileNumber
Debug.Print (a & " " & b)
End Sub
Upvotes: 1
Reputation: 440
I am also experiencing the errors which you have, these are the probable causes of the said error.
1) The CSV
is open along with the Excel
file
2) Your code has memory leaks
To resolve number 1
To resolve number 2
CSV
object after you have written your data inside it.I'm not sure about your full code, but please check if you have something like Excel.Close
or Excel.Quit
.
Upvotes: 0