|
-
Jul 31st, 2008, 07:01 AM
#1
Thread Starter
Hyperactive Member
Reading .CSV format in Excel Object
Code:
Set exlApp = New Excel.Application
Set exlSheet = exlApp.Workbooks.Open("filename.xls").Worksheets(1)
irow = 1
While exlSheet.Cells(irow, 1) <> ""
TextBox2.Text = TextBox2.Text & exlSheet.Cells(irow, 4) & vbCrLf
irow = irow + 1
Wend
This works fine if filename is an excel file. But what if i want to use a .CSV file. If i change the filename to filename.csv and then i use exlSheet.Cells(irow, 4), i dont get the right information.
Thans in advanced
-
Jul 31st, 2008, 07:11 AM
#2
Re: Reading .CSV format in Excel Object
two things
When you open a cvs
1) you use Workbooks.OpenText instead of Workbooks.Open
something like
vb Code:
Workbooks.OpenText Filename:="P:\sid.csv", Origin:=xlMSDOS, StartRow:=1, _
DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter _
:=False, Tab:=True, Semicolon:=False, Comma:=False, Space:=False, _
Other:=False, FieldInfo:=Array(1, 1), TrailingMinusNumbers:=True
2) CSV file doesn't have multiple worksheets but just one sheet
Try these changes and if you still have questions, post them
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Jul 31st, 2008, 07:13 AM
#3
Re: Reading .CSV format in Excel Object
if a file is named .csv, but not an actual csv file it will not open correctly
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jul 31st, 2008, 07:42 AM
#4
Re: Reading .CSV format in Excel Object
When use .OpenText with *.csv file, it seems to be all other arguments have no effect : Excel treats *.csv file as a native file type.
To use other arguments, *.csv file must be renamed to *.txt
-
Jul 31st, 2008, 09:37 AM
#5
Thread Starter
Hyperactive Member
Re: Reading .CSV format in Excel Object
Code:
Set exlApp = New Excel.Application
exlApp.Workbooks.OpenText FileName:="c:\08.CSV", Origin:=xlMSDOS, StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(1, 1), TrailingMinusNumbers:=True
MsgBox (exlApp.Cells(2, 1))
This will give me a complete line line "1;test;test2;test3;etc" .. i dont get the right cell that i want
tried also Semicolon:=True
thanks for the input so far
Last edited by Amien; Jul 31st, 2008 at 10:27 AM.
-
Jul 31st, 2008, 11:40 AM
#6
Re: Reading .CSV format in Excel Object
I tried it and it works fine...
vb Code:
Private Sub CommandButton1_Click()
Set exlApp = New Excel.Application
exlApp.Visible = True
exlApp.Workbooks.OpenText Filename:="p:\Sid.CSV", _
Origin:=xlMSDOS, StartRow:=1, DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, _
Tab:=True, Semicolon:=False, Comma:=False, Space:=False, _
Other:=False, FieldInfo:=Array(1, 1), TrailingMinusNumbers:=True
MsgBox (exlApp.Cells(2, 1))
End Sub
Can you upload your csv file?
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Jul 31st, 2008, 01:34 PM
#7
Thread Starter
Hyperactive Member
Re: Reading .CSV format in Excel Object
http://home.wanadoo.nl/martijnolivie...of%20Book1.csv
not the actually .csv file (cant post that one) .. but this file is also not imported correctly.
Tested with your script
-
Jul 31st, 2008, 04:03 PM
#8
Re: Reading .CSV format in Excel Object
csv file must be separated by comma "," (csv = comma separated values)
yours are separated by semicolon ";"
excel will not work correctly with it, you need to either change the separators in your file or rename your file from .csv to .txt or some other appropriate extension
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jul 31st, 2008, 04:18 PM
#9
Thread Starter
Hyperactive Member
Re: Reading .CSV format in Excel Object
renaming the .csv to .txt and changing Semicolon:= to True works.
not renaming and changing all ; to , works also.
Why is this? Why cant i keep the .csv extention and the semicolon? when i load the csv file (with semicolon) in Excel it shows just fine
Last edited by Amien; Jul 31st, 2008 at 04:26 PM.
-
Jul 31st, 2008, 04:50 PM
#10
Re: Reading .CSV format in Excel Object
(csv = comma separated values)
excel treats csv file as exactly that, so if they are not formatted correctly they will not work right when opened by code
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jul 31st, 2008, 04:51 PM
#11
Thread Starter
Hyperactive Member
Re: Reading .CSV format in Excel Object
but i thought when opening with excel works, it would be possible to open it by code.. guess not?
-
Aug 6th, 2008, 11:10 AM
#12
Thread Starter
Hyperactive Member
Re: Reading .CSV format in Excel Object
exlApp.Workbooks.OpenText FileName:=TextBox1.Text, _
Origin:=xlMSDOS, StartRow:=1, DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, _
Tab:=True, Semicolon:=True, Comma:=False, Space:=False, _
Other:=False, FieldInfo:=Array(1, 1), TrailingMinusNumbers:=True
i use this code to read a .text which is really a .csv file .. one of the fields contains a date. That date is a europian date. Then i import the file using the above code and then i use
ExpApp.Cells(1,5) i get the date but only in american type .. so the date is in the original text file 7 january 2008 .. in the ExpApp.Cells(1,5) it will be an American date
how can i fixed this .. very ugent
-
Aug 6th, 2008, 12:05 PM
#13
Re: Reading .CSV format in Excel Object
Right-click on one of the cells and select "Format Cell", then "Number".
Which item is currently selected in the list?
If you change it to Date, and the format you want, does the preview work correctly?
-
Aug 6th, 2008, 01:34 PM
#14
Re: Reading .CSV format in Excel Object
Hi Amien
If you want to achieve the same in code then you can use the format() to suit your needs
for example if cell A1 = 7 january 2008 (which is of "dd-mmmm-yyyy" format) and you want a dd/mm/yyyy format then
MsgBox Format("7 january 2008", "dd/mm/yyyy") or
MsgBox Format(Range("A1").Value, "dd/mm/yyyy")
will give you
07/01/2008
Hope this helps...
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Aug 6th, 2008, 04:28 PM
#15
Thread Starter
Hyperactive Member
Re: Reading .CSV format in Excel Object
i fixed it with Local:=true
Other:=False, FieldInfo:=Array(1, 1), Local:=True, TrailingMinusNumbers:=True
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|