VBA to Import Data from a CSV File into Excel - working with 'End of File' scenarios on Large Files
I'm reviewing some code and I'm reviewing some code and I'm trying to configure I'm prototyping a solution and I'm working through a tutorial and I recently switched to I'm trying to create a VBA macro that imports data from a CSV file into an Excel worksheet, but I'm working with an 'End of File' behavior when processing larger files (over 100,000 rows)..... My current implementation uses `Open` and `Input` statements. I've tested with smaller files, and it works fine there. Here's the code I'm using: ```vba Sub ImportCSVData() Dim filePath As String Dim fileNum As Integer Dim lineData As String Dim rowIndex As Long filePath = "C:\path\to\your\file.csv" fileNum = FreeFile() rowIndex = 1 Open filePath For Input As #fileNum Do While Not EOF(fileNum) Line Input #fileNum, lineData Cells(rowIndex, 1).Value = lineData rowIndex = rowIndex + 1 Loop Close #fileNum End Sub ``` When I run this code with larger CSV files, I get the following behavior: "Run-time behavior '62': Input past end of file". I've tried adding a check to see if the file is open and ensuring that I properly handle any blank lines, but the behavior continues. Is there a more robust way to read large CSV files into Excel using VBA? Additionally, should I consider using the `QueryTable` method for better performance and behavior handling? Any guidance or best practices would be appreciated! I recently upgraded to Vba latest. Any feedback is welcome! This is happening in both development and production on CentOS. I appreciate any insights! Any ideas how to fix this? Is there a better approach? This is part of a larger service I'm building.