VBA - CSV data import: "Run-time error '62': Input past end of file"

kingofcamden

New Member
Joined
Feb 13, 2015
Messages
13
Hi all,

I would like to use a macro script to import a (TAB separated) CSV file which was exported from a database. It seems to work - I can see that rows are being imported (although the first cell contains two additional weird characters: ÿþCampaign Name). However the script stops at "Line input #FileNum, ResultStr" with the following error message "Run-time error '62': Input past end of file". This is the code:

Code:
Sub Import_Button()

      Dim ResultStr As String
      Dim filename As String
      Dim FileNum As Integer
      Dim Counter As Double
      'Ask User for File's Name
      filename = Application.GetOpenFilename("*,*.csv", , "Please select CSV file for January Data to import")
      'Check for no entry or if user clicks cancel
      If filename = "False" Then Exit Sub
      'Get Next Available File Handle Number
      FileNum = FreeFile()
      'Open Text File For Input
      Open filename For Input As #FileNum
      'Turn Screen Updating Off
      Application.ScreenUpdating = False
      'Set The Counter to 1
      Counter = 1


      'Loop Until the End Of File Is Reached
      Do While Seek(FileNum) <= LOF(FileNum)
         'Display Importing Row Number On Status Bar
          Application.StatusBar = "Importing Row " & _
          Counter & " of text file " & filename
          'Store One Line Of Text From File To Variable
          Line Input #FileNum, ResultStr


          'Store Variable Data Into Active Cell
          Dim splitValues As Variant
          splitValues = Split(ResultStr, vbTab)
          'below format: Sheets("Tab the content should be pasted to").Cells(Counter + ROW, COLUMN)
Sheets("January_Data").Cells(Counter + 49, 1) = Replace(splitValues(0), Chr(34), "")
Sheets("January_Data").Cells(Counter + 49, 2) = Replace(splitValues(1), Chr(34), "")
Sheets("January_Data").Cells(Counter + 49, 3) = Replace(splitValues(2), Chr(34), "")
Sheets("January_Data").Cells(Counter + 49, 4) = Replace(splitValues(3), Chr(34), "")
Sheets("January_Data").Cells(Counter + 49, 5) = Replace(splitValues(4), Chr(34), "")
Sheets("January_Data").Cells(Counter + 49, 6) = Replace(splitValues(5), Chr(34), "")
Sheets("January_Data").Cells(Counter + 49, 7) = Replace(splitValues(6), Chr(34), "")
Sheets("January_Data").Cells(Counter + 49, 8) = Replace(splitValues(7), Chr(34), "")
Sheets("January_Data").Cells(Counter + 49, 9) = Replace(splitValues(8), Chr(34), "")
Sheets("January_Data").Cells(Counter + 49, 10) = Replace(splitValues(9), Chr(34), "")
Sheets("January_Data").Cells(Counter + 49, 11) = Replace(splitValues(10), Chr(34), "")
          Counter = Counter + 1
      'Start Again At Top Of 'Do While' Statement
      Loop
      'Close The Open Text File
      Close
      'Remove Message From Status Bar
      Application.StatusBar = False
      MsgBox ("January_Data data successfully imported")
End Sub

My questions are:
  • Is the TAB separation the problem? I wasn't really sure what to enter into "splitValues = Split(ResultStr, vbTab)" for the TAB.
  • How to get rid of the weird characters in the first cell of the imported file: "ÿþCampaign Name"
  • How to stop the error message from showing up: "Run-time error '62': Input past end of file"

Hope you can help! :)
Many thanks.
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
I don't think this is a tab seperation issue. using Split(x, vbTab) should be OK.

I'm not sure about the weird characters, I've had the same issue myself

if you change this line:
Code:
Do While Seek(FileNum) <= LOF(FileNum)
to
Code:
Do until EOF(FileNum)
that will help with the error message as it will stop at the End Of File
 
Upvote 0
Hi eddvrs,

Superb! Thanks - this did the trick! :)

I read somewhere that the character issue appears because I'm not importing a (unicode) text file...but I can live with the two weird characters, so no problem.

Thanks again and happy Friday!
 
Upvote 0
No problem mate.

You could try Substitute(x,"ÿþ","") to get rid of them.

OT: Is the Kingof Camden from Camden? I'm down the road in Angel
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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