Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 117
Default Problems with CSV file input

I have a macro that opens a CSV file. The problem is that it
recognizes text fields containing slashes as dates, which I do not
want.

I have searched for a solution with Google, but none of them seem to
work. I tried from the Excel menu importing CSV files as external
data using General and then Text format, but this did not work. I
tried changing the file extension to .vcs or .txt and then importing.
This did not work with either General nor Text form.

Can anyone tell me how to get around this problem?

I am using Excel 2003. I also have 2007.

Thanks, Alan
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,049
Default Problems with CSV file input

one way

read the data in using the OPEN method, then you control the input more
easily

use the READ LINE method to real a comma separated line of text out of your
file, the use the SPLIT command to break it into pieces
All you need to do then is from each into a cell....and if the cell is
formatted as TEXT then Excel won't try top convert it.

there are other ways to do this of course, but this is meant to be quite
simplistic

not tested, but this will help get you going

Option Explicit

Sub readCSV()

Dim ff As Long
Dim text As String
Dim index As Long
Dim data As Variant
Dim rw As Long

ff = FreeFile
Open "C:\temp\demo4.csv" For Input As #ff
Do While Not EOF(ff)
Line Input #ff, text
data = Split(text, ",")
rw = rw + 1
For index = LBound(data, 1) To UBound(data, 1)
With Cells(rw, index + 1)
.NumberFormat = "@"
.Value = data(index)
End With
Next
Loop


End Sub
"Alan" wrote in message
...
I have a macro that opens a CSV file. The problem is that it
recognizes text fields containing slashes as dates, which I do not
want.

I have searched for a solution with Google, but none of them seem to
work. I tried from the Excel menu importing CSV files as external
data using General and then Text format, but this did not work. I
tried changing the file extension to .vcs or .txt and then importing.
This did not work with either General nor Text form.

Can anyone tell me how to get around this problem?

I am using Excel 2003. I also have 2007.

Thanks, Alan


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Problems with CSV file input

Your macro must do exactly what a human would do faced with the same situation.

HUMAN:

Data Import External Data Import data...

The human would then tell the Import Wizard the filespec and the format of
the various fields. Thus 12/25/2009 can be imported as Text rather than Date.

Just switch on the Recorder while the human pecks away at the keyboard and
then make adaptations:

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 6/2/2009 by James Ravenswood
'

'
With ActiveSheet.QueryTables.Add(Connection:="TEXT;C:\t est
folder\x.csv", _
Destination:=Range("A1"))
.Name = "x"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(2)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Columns("A:A").ColumnWidth = 18
End Sub



--
Gary''s Student - gsnu200855


"Alan" wrote:

I have a macro that opens a CSV file. The problem is that it
recognizes text fields containing slashes as dates, which I do not
want.

I have searched for a solution with Google, but none of them seem to
work. I tried from the Excel menu importing CSV files as external
data using General and then Text format, but this did not work. I
tried changing the file extension to .vcs or .txt and then importing.
This did not work with either General nor Text form.

Can anyone tell me how to get around this problem?

I am using Excel 2003. I also have 2007.

Thanks, Alan

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 117
Default Problems with CSV file input

On Jun 2, 12:06*pm, Gary''s Student
wrote:
Your macro must do exactly what a human would do faced with the same situation.

HUMAN:

Data Import External Data Import data...

The human would then tell the Import Wizard the filespec and the format of
the various fields. *Thus 12/25/2009 can be imported as Text rather than Date.


BUT I tried this. It did not work. Neither General nor Text import
worked. See original post.

Thanks, Alan
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8,520
Default Problems with CSV file input

Alan

Try below

1. Rename the file to txt
2. FileOpen*.txt
3. From Text Import Wizard Step 1 ..hit Next
4. Select comma as the delimiter in Step 2
5. In Step 3 , from Data Preview area..select the column you want to be
displayed as text. If you want all columns to be selected with Shift key
pressed select all fields.. The background of selected fields wil be black.

Hit OK

Try this and proceed with recording a macro....if this is not for a one time
business.

If this post helps click Yes
---------------
Jacob Skaria


"Alan" wrote:

On Jun 2, 12:06 pm, Gary''s Student
wrote:
Your macro must do exactly what a human would do faced with the same situation.

HUMAN:

Data Import External Data Import data...

The human would then tell the Import Wizard the filespec and the format of
the various fields. Thus 12/25/2009 can be imported as Text rather than Date.


BUT I tried this. It did not work. Neither General nor Text import
worked. See original post.

Thanks, Alan



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 117
Default Problems with CSV file input


This did not work either. Alan




  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 117
Default Problems with CSV file input


This did not work either. Alan




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
BRAND NEW USER -: PROBLEMS WITH LIST AND TEXT BOX INPUT [email protected] Excel Programming 0 April 20th 07 08:04 AM
BRAND NEW USER -: PROBLEMS WITH LIST AND TEXT BOX INPUT [email protected] Excel Programming 0 April 19th 07 12:28 AM
Problems with date input timbloke Excel Discussion (Misc queries) 1 April 26th 06 09:39 AM
Problems merging an excel file due to code or file problems? Cindy M -WordMVP- Excel Programming 0 September 14th 04 02:58 PM
Having problems with adding input from combobox stevem[_5_] Excel Programming 1 April 2nd 04 03:44 AM


All times are GMT +1. The time now is 10:28 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"