PC Review


Reply
Thread Tools Rate Thread

Counting number of columns in CSV/Text file

 
 
ExcelMonkey
Guest
Posts: n/a
 
      30th Nov 2007
CSV/My text file has data in it which originates from a spreadsheeet. My
goal is count the number of columns of data which were pulled into the text
file. In a simple world I would just count the fields at the top of the text
file. But I have no control over the imported data and the first line will
not necessarily have the fields. I need to build a loop which loops though
the rows in my text file and then performs a field count per row and stores
the field count number. Then the Max of all field counts per row = number of
columns in original data.

How do I do this?

Thanks

EM


 
Reply With Quote
 
 
 
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      30th Nov 2007
Assuming none of your individual fields contain quote-encased text with
embedded commas (if they do, let me know as there is a work around for it),
this function should return the maximum number of columns (fields) in your
rows (records); just pass in the full path to your cvs file and use its
return value...

Function MaxColumns(PathFileName As String) As Long
Dim X As Long
Dim FileNum As Long
Dim TotalFile As String
Dim Records() As String
Dim Fields() As String
' Let VB generate the file channel number
FileNum = FreeFile
' Reads the whole file into memory all at once
Open PathFileName For Binary As #FileNum
TotalFile = Space(LOF(FileNum))
Get #FileNum, , TotalFile
Close #FileNum
Records = Split(TotalFile, vbCrLf)
For X = 0 To UBound(Records)
Fields = Split(Records(X), ",")
If UBound(Fields) > MaxColumns Then MaxColumns = UBound(Fields)
Next
' Since Split returns a zero-based array, the UBound function
' values we have been comparing and saving are one less than
' the count of fields in the array; so we add one to adjust for that
MaxColumns = MaxColumns + 1
End Function

Rick


"ExcelMonkey" <(E-Mail Removed)> wrote in message
news:027B67F9-9826-4472-BBC4-(E-Mail Removed)...
> CSV/My text file has data in it which originates from a spreadsheeet. My
> goal is count the number of columns of data which were pulled into the
> text
> file. In a simple world I would just count the fields at the top of the
> text
> file. But I have no control over the imported data and the first line
> will
> not necessarily have the fields. I need to build a loop which loops
> though
> the rows in my text file and then performs a field count per row and
> stores
> the field count number. Then the Max of all field counts per row = number
> of
> columns in original data.
>
> How do I do this?
>
> Thanks
>
> EM
>
>


 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Counting 10 Columns * X Number of Rows Rob Microsoft Excel Worksheet Functions 5 7th Apr 2009 09:22 PM
Counting number of columns gatarossi@ig.com.br Microsoft Excel Discussion 2 12th Aug 2007 02:06 PM
Counting the actual number of records in a text file. doyapore Microsoft Access External Data 3 29th Dec 2004 09:09 AM
text/csv file import - variable number of columns =?Utf-8?B?RGFycmVu?= Microsoft Access External Data 2 23rd Nov 2004 09:54 PM
counting the number of columns in a data grid Brian Anderson Microsoft Dot NET 1 30th Sep 2003 12:19 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:12 PM.