Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Importing csv by code

Hi there,
i wrote some vba code to open a csv file within Excel 2000.
Since the separator is ';' and not ',' i wrote:

Set wb = Workbooks.Open(FILENAME:=filename, ReadOnly:=True, Format:=4)

But the file is not imported correctly.
BTW: if i double-click on the file, Excel are not able to import it but if I
open the file within Excel, the file are ok.

Any idea?
--
SteM


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Importing csv by code

First, I'd rename the .csv file to .txt. .CSV file means something special to
excel and you'll have trouble in your code if you leave the file named .csv.

Then instead of writing the code yourself, try recording a macro when you open
the .txt file manually. You'll be able to specify the separator you want.

SteM wrote:

Hi there,
i wrote some vba code to open a csv file within Excel 2000.
Since the separator is ';' and not ',' i wrote:

Set wb = Workbooks.Open(FILENAME:=filename, ReadOnly:=True, Format:=4)

But the file is not imported correctly.
BTW: if i double-click on the file, Excel are not able to import it but if I
open the file within Excel, the file are ok.

Any idea?
--
SteM


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Importing csv by code

Try this code. Change Default folder and delimiter.



Sub GetCSVData()

Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Const Delimiter = ","
Set fsread = CreateObject("Scripting.FileSystemObject")

'default folder
Folder = "C:\temp\test"
ChDir (Folder)

FName = Application.GetOpenFilename("CSV (*.csv),*.csv")


RowCount = LastRow + 1
If FName < "" Then
'open files
Set fread = fsread.GetFile(FName)
Set tsread = fread.OpenAsTextStream(ForReading, TristateUseDefault)

Do While tsread.atendofstream = False

InputLine = tsread.ReadLine

'extract comma seperated data
ColumnCount = 1
Do While InputLine < ""
DelimiterPosition = InStr(InputLine, Delimiter)
If DelimiterPosition 0 Then
Data = Trim(Left(InputLine, DelimiterPosition - 1))
InputLine = Mid(InputLine, DelimiterPosition + 1)
Else
Data = Trim(InputLine)
InputLine = ""
End If

Cells(RowCount, ColumnCount) = Data
ColumnCount = ColumnCount + 1
Loop
RowCount = RowCount + 1
Loop

tsread.Close
End If
End Sub


"SteM" wrote:

Hi there,
i wrote some vba code to open a csv file within Excel 2000.
Since the separator is ';' and not ',' i wrote:

Set wb = Workbooks.Open(FILENAME:=filename, ReadOnly:=True, Format:=4)

But the file is not imported correctly.
BTW: if i double-click on the file, Excel are not able to import it but if I
open the file within Excel, the file are ok.

Any idea?
--
SteM



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
Importing Alan Beban's code on Arrays; Importing a module or a project Steve G Excel Worksheet Functions 4 August 27th 07 04:18 PM
help: code for importing xml into excel sheet deepakbadki Excel Programming 0 December 29th 05 08:35 AM
Importing txt into XL as Delimited, what's wrong w my code? Mslady Excel Programming 3 October 4th 05 06:22 PM
importing worksheet via code Willie Smith Excel Programming 2 July 15th 05 10:18 PM
Importing Code into 'ThisWorkbook' Mark Excel Programming 3 April 16th 04 03:29 PM


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

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

About Us

"It's about Microsoft Excel"