Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Read 500 columns of CSV data into a spreadsheet

I have a csv file
apx 500 fields in double quotes, separated by commas. Each "value" mwill
vary in length
There may be many rows of data like this in the file, each with the same
number of values
Blank values are represented by 2 double quotes
Example:
"Name","Address","Phone","Gender",etc.etc.etc. etc
"Mary","123 Any Street","555-1212","F"
"Bill","PO Box 1","","M"
"Darth","","",""

What I want to do is bring in each field from row 1 and put it in a
spreadsheet going down say 500 rows in column A.
Then move to the next data row and insert each of those fields into column B.
Like this:
"Mary"
"123 Any Street"
"555-1212"
"F"
etc etc etc etc

I know READLINE will read a whole line, but I haven't found a way to read a
"value".
Any ideas?

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Read 500 columns of CSV data into a spreadsheet

Bill
The following macro should do the job as requested. I'm sure it can be
tidied up but might give you something to work with. Apart from this
the easy way on a occasional basis is to open the CSV file directly in
Excel Select and Copy the data Insert a new Worksheet Select cell
A1 on the new worksheet Paste Special selecting Values & Transpose
from the paste special options.

Option Explicit
Sub GetData() ' to get data from csv file
Dim Fnam As String
Dim ColNdx As Integer
Dim RowNdx As Integer
Dim TempVal As Variant
Dim WholeLine As String
Dim Pos As Integer
Dim NextPos As Integer
Dim Sep As String

Fnam = "c:\testdata.csv" ' data file name as required
Application.ScreenUpdating = False ' prevent screen flicker
On Error GoTo EndMacro:
Sep = "," ' input data csv file field seperator
Open Fnam For Input Access Read As #1 ' open csv file
ColNdx = 0 ' column number

While Not EOF(1)
Line Input #1, WholeLine
' add comma to last field of data line if not present
If Right(WholeLine, 1) < Sep Then WholeLine = WholeLine & Sep
Pos = 1 ' reset field number
RowNdx = 1 ' reset row index
ColNdx = ColNdx + 1 ' advance column number
NextPos = InStr(Pos, WholeLine, Sep) 'locate first field seperator
While NextPos = 1
TempVal = Mid(WholeLine, Pos, NextPos - Pos) ' extract the
field
Cells(RowNdx, ColNdx).Value = TempVal 'put field value in
worksheet
RowNdx = RowNdx + 1 ' advance row number
Pos = NextPos + 1 ' advance field position
NextPos = InStr(Pos, WholeLine, Sep) ' is there another
field
Wend ' if yes then loop and extract it
Wend
Sep = Chr(34) ' remove the quotes if required
Range("a1").CurrentRegion.Replace What:=Sep, Replacement:=""

EndMacro:
On Error GoTo 0
Close #1
Application.ScreenUpdating = True
End Sub

HTH
Chris Bruce

Bill Anderson wrote:
I have a csv file
apx 500 fields in double quotes, separated by commas. Each "value" mwill
vary in length
There may be many rows of data like this in the file, each with the same
number of values
Blank values are represented by 2 double quotes
Example:
"Name","Address","Phone","Gender",etc.etc.etc. etc
"Mary","123 Any Street","555-1212","F"
"Bill","PO Box 1","","M"
"Darth","","",""

What I want to do is bring in each field from row 1 and put it in a
spreadsheet going down say 500 rows in column A.
Then move to the next data row and insert each of those fields into column B.
Like this:
"Mary"
"123 Any Street"
"555-1212"
"F"
etc etc etc etc

I know READLINE will read a whole line, but I haven't found a way to read a
"value".
Any ideas?


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I rotate data in a spreadsheet so that rows become columns? Dennis_Kesselring Excel Discussion (Misc queries) 4 November 22nd 05 05:30 PM
Read 500 columns of CSV data into a spreadsheet Bill Anderson Excel Programming 1 June 2nd 05 03:03 AM
Read/Write data to/from text files from a spreadsheet. lothario[_37_] Excel Programming 8 November 2nd 03 03:10 PM
cboBox to read data from a spreadsheet Range igor dudkin Excel Programming 0 July 18th 03 04:49 AM
cboBox to read data from a spreadsheet Range Tom Ogilvy Excel Programming 0 July 17th 03 09:14 PM


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

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"