crgeorge64
New Member
- Joined
- Sep 29, 2009
- Messages
- 1
Hello,
I am new to the forum and have been learning VBA on my own and with books. I have a task where I need to read data from several CSV files contained in a directory. The number of files varies but all of the file I need, start with "SPL" and are numbered sequentially, such as SPL0000.csv, SPL0002.csv, etc. Also note that there is no header in these files but the data is in 3 columns. the columns are dateandtime / a code that could be "P" or "S" / a numeric value for which for which I need the data for further analysis. There are other CSV files in the same directory that I don't need for this purpose.
The intention is to create a loop such that the code will loop (look at) each of the CSV files, read the data and append the data into a particular sheet in the workbook. When I try to run the code It just seems to keep trying to read from the initial file.
Here is my code. It should be noted that I'm no expert and would appreciate any help getting in the right direction. Thanks.
Public Sub QueryTextFile()
Dim rsData As ADODB.Recordset
Dim sConnect As String
Dim sSQL As String
Dim FileDirectory
Dim sh As Worksheet, sPath As String, sName As String
Dim startselect As Range
Set sh = ActiveSheet
Set startselect = Sheet1.Range("A1")
Workbooks("ADO Sample Code.xlsm").Activate
Sheets("Old").Activate
sPath = "J:\Planning\Community Mobility\Speed Trailer Data\2009\Signs\2009-04-16 Eureka Ave bet Wicklow Rd and Palmetto St\Downloads\"
sName = Dir(sPath & "SPL*.csv")
'Selection.CurrentRegion.ClearContents
Do While sName <> ""
'Create the connection string
'sConnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
' "Data Source=J:\Planning\Community Mobility\Speed Trailer Data\2009\Signs\2009-04-16 Eureka Ave bet Wicklow Rd and Palmetto St\Downloads\;" & _
' "Extended Properties=""Text;HDR=No"";"
sConnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=J:\Planning\Community Mobility\Speed Trailer Data\2009\Signs\2009-04-16 Eureka Ave bet Wicklow Rd and Palmetto St\Downloads\;" & _
"Extended Properties=""Text;HDR=No;FMT=Delimited"";"
'Create the SQL statement
'sSQL = "SELECT * FROM SPL0000.csv WHERE [F2]='P';"
sSQL = "SELECT * FROM " & sName & " WHERE [F2]='P';"
Debug.Print sName
Set rsData = New ADODB.Recordset
rsData.Open sSQL, sConnect, adOpenForwardOnly, adLockReadOnly, adCmdText
'Check to make sure we received data.
If Not rsData.EOF Then
'Dump the returned data onto Sheet1.
'Sheet1.Range("A1").CopyFromRecordset rsData
startselect.CopyFromRecordset rsData
Else
MsgBox "No records returned.", vbCritical
End If
'Clean up our Recordset object.
rsData.Close
Set rsData = Nothing
Dim LastRow As Integer
Workbooks("ADO Sample Code.xlsm").Activate
Sheets("Old").Activate
LastRow = ActiveSheet.UsedRange.Rows.Count
LastRow = LastRow + 1
'MsgBox "The last row is " & LastRow
startselect = Range("A" & LastRow).Select
Loop
End Sub
I am new to the forum and have been learning VBA on my own and with books. I have a task where I need to read data from several CSV files contained in a directory. The number of files varies but all of the file I need, start with "SPL" and are numbered sequentially, such as SPL0000.csv, SPL0002.csv, etc. Also note that there is no header in these files but the data is in 3 columns. the columns are dateandtime / a code that could be "P" or "S" / a numeric value for which for which I need the data for further analysis. There are other CSV files in the same directory that I don't need for this purpose.
The intention is to create a loop such that the code will loop (look at) each of the CSV files, read the data and append the data into a particular sheet in the workbook. When I try to run the code It just seems to keep trying to read from the initial file.
Here is my code. It should be noted that I'm no expert and would appreciate any help getting in the right direction. Thanks.
Public Sub QueryTextFile()
Dim rsData As ADODB.Recordset
Dim sConnect As String
Dim sSQL As String
Dim FileDirectory
Dim sh As Worksheet, sPath As String, sName As String
Dim startselect As Range
Set sh = ActiveSheet
Set startselect = Sheet1.Range("A1")
Workbooks("ADO Sample Code.xlsm").Activate
Sheets("Old").Activate
sPath = "J:\Planning\Community Mobility\Speed Trailer Data\2009\Signs\2009-04-16 Eureka Ave bet Wicklow Rd and Palmetto St\Downloads\"
sName = Dir(sPath & "SPL*.csv")
'Selection.CurrentRegion.ClearContents
Do While sName <> ""
'Create the connection string
'sConnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
' "Data Source=J:\Planning\Community Mobility\Speed Trailer Data\2009\Signs\2009-04-16 Eureka Ave bet Wicklow Rd and Palmetto St\Downloads\;" & _
' "Extended Properties=""Text;HDR=No"";"
sConnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=J:\Planning\Community Mobility\Speed Trailer Data\2009\Signs\2009-04-16 Eureka Ave bet Wicklow Rd and Palmetto St\Downloads\;" & _
"Extended Properties=""Text;HDR=No;FMT=Delimited"";"
'Create the SQL statement
'sSQL = "SELECT * FROM SPL0000.csv WHERE [F2]='P';"
sSQL = "SELECT * FROM " & sName & " WHERE [F2]='P';"
Debug.Print sName
Set rsData = New ADODB.Recordset
rsData.Open sSQL, sConnect, adOpenForwardOnly, adLockReadOnly, adCmdText
'Check to make sure we received data.
If Not rsData.EOF Then
'Dump the returned data onto Sheet1.
'Sheet1.Range("A1").CopyFromRecordset rsData
startselect.CopyFromRecordset rsData
Else
MsgBox "No records returned.", vbCritical
End If
'Clean up our Recordset object.
rsData.Close
Set rsData = Nothing
Dim LastRow As Integer
Workbooks("ADO Sample Code.xlsm").Activate
Sheets("Old").Activate
LastRow = ActiveSheet.UsedRange.Rows.Count
LastRow = LastRow + 1
'MsgBox "The last row is " & LastRow
startselect = Range("A" & LastRow).Select
Loop
End Sub