Pull data from .csv files into one main .csv file  
Author Message
Donaghy





PostPosted: Visual Basic for Applications (VBA), Pull data from .csv files into one main .csv file Top

Hi all,

Does anyone know how to pull data from specific columns from specific .csv files into one main .csv files /

Help appreciated, thanks.



Microsoft ISV Community Center Forums3  
 
 
duck thing





PostPosted: Visual Basic for Applications (VBA), Pull data from .csv files into one main .csv file Top

Sure. Here's a quick piece of code that demonstrates grabbing data from a CSV file using the row and column number:

Option Explicit
Option Base 0

Public Sub Main()
Dim fptr As Long, fnum As Integer
fnum = FreeFile()
Open "C:\temp\Book1.csv" For Input As fnum

MsgBox GetDataFromCSV(fnum, 1, 2)

Close fnum
End Sub

' 1-based row/col parameters
Private Function GetDataFromCSV(tFileNum As Integer, tRowNum As Integer, tColNum As Integer)
Dim ll As Long, tInStr As String
For ll = 1 To tRowNum
Line Input #tFileNum, tInStr
If EOF(tFileNum) Then Exit Function ' trap eof
Next ll
' now tInStr contains the requested record. parse for the correct column

GetDataFromCSV = ParseColumns(tInStr, ",", tColNum)
End Function


Private Function ParseColumns(tRecordStr As String, tDelimiter As String, ByVal tColumn As Integer) As String
Dim tptr As Integer, tnextptr As Integer
tptr = 1
Do
tptr = InStr(tptr, tRecordStr, tDelimiter)
If tptr > 0 Then
tColumn = tColumn - 1
If tColumn = 0 Then
tnextptr = InStr(tptr + 1, tRecordStr, tDelimiter) ' find next delimiter
If tnextptr = 0 Then tnextptr = Len(tRecordStr) ' if none, use eos
ParseColumns = Mid(tRecordStr, tptr + 1, tnextptr - tptr - 1)
Exit Function
End If
End If
Loop
End Function

Let me know if you've got questions about this.



 
 
Donaghy





PostPosted: Visual Basic for Applications (VBA), Pull data from .csv files into one main .csv file Top

thanks for the help.

I think i would probably need more backgorund info on the syntax, etc, used to have a better idea of the code.

Is there any good helpful documentation / example code available regarding my sort of problem