Results 1 to 15 of 15

Thread: Reading .CSV format in Excel Object

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    267

    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

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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:
    1. Workbooks.OpenText Filename:="P:\sid.csv", Origin:=xlMSDOS, StartRow:=1, _
    2.         DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter _
    3.         :=False, Tab:=True, Semicolon:=False, Comma:=False, Space:=False, _
    4.         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

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  4. #4
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    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
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    267

    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.

  6. #6
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Reading .CSV format in Excel Object

    I tried it and it works fine...

    vb Code:
    1. Private Sub CommandButton1_Click()
    2. Set exlApp = New Excel.Application
    3. exlApp.Visible = True
    4. exlApp.Workbooks.OpenText Filename:="p:\Sid.CSV", _
    5. Origin:=xlMSDOS, StartRow:=1, DataType:=xlDelimited, _
    6. TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, _
    7. Tab:=True, Semicolon:=False, Comma:=False, Space:=False, _
    8. Other:=False, FieldInfo:=Array(1, 1), TrailingMinusNumbers:=True
    9.  
    10. MsgBox (exlApp.Cells(2, 1))
    11. 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

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    267

    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

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    267

    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.

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    267

    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?

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    267

    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

  13. #13
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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?

  14. #14
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    267

    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
  •  



Click Here to Expand Forum to Full Width