Hi I presently have a difficulty with importing csv files into excel using VBA. I have created a macro which is meant to read each record from a csv file and put it into excel. The code also needs to handle the splitting into different sheets when the csv file has more than the allowed 65536 rows. Can you please help. My code is ignoring the first record, thus not bringing back all records. code below.
'Imports text file into Excel workbook using ADO.
'If the number of records exceeds 65536 then it splits it over more than one sheet.
Dim strFilePath As String, strFilename As String, strFullPath As String
Dim lngCounter As Long
Dim oConn As Object, oRS As Object, oFSObj As Object
'Get a text file name
strFullPath = Application.GetOpenFilenam
e("Text Files (*.csv),*.csv", , "Please selec text file...")
If strFullPath = "False" Then Exit Sub 'User pressed Cancel on the open file dialog
'This gives us a full path name e.g. C:tempfolderfile.txt
'We need to split this into path and file name
Set oFSObj = CreateObject("SCRIPTING.FI
LESYSTEMOB
JECT")
strFilePath = oFSObj.GetFile(strFullPath
).ParentFo
lder.Path
strFilename = oFSObj.GetFile(strFullPath
).Name
'Open an ADO connection to the folder specified
Set oConn = CreateObject("ADODB.CONNEC
TION")
oConn.Open "Provider=Microsoft.Jet.OL
EDB.4.0;" & _
"Data Source=" & strFilePath & ";" & _
"Extended Properties=""text;HDR=Yes;
FMT=Delimi
ted"""
Set oRS = CreateObject("ADODB.RECORD
SET")
'Now actually open the text file and import into Excel
oRS.Open "SELECT * FROM " & strFilename, oConn, 3, 1, 1
While Not oRS.EOF
Sheets.Add
ActiveSheet.Range("A1").Co
pyFromReco
rdset oRS, 65536
Wend
oRS.Close
oConn.Close
Start Free Trial