Results 1 to 15 of 15

Thread: SOLVED reading from a csv document

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2003
    Location
    Los Angeles, CA
    Posts
    49

    SOLVED reading from a csv document

    How can I read data from a .csv document into a .xls document? Something along the lines of this:

    Code:
    Range("A1").Value = Doc("blah.csv").Sheets("blah").Range("A1").Value
    I made up the Doc function, but I think you guys know what I mean

    Thanks,
    Alex
    Last edited by LodBot; Jul 8th, 2003 at 10:17 AM.

  2. #2
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    One way is to open the .csv file using EXCEL.
    If you are using VBA try something like this:
    I assume your .xls is open (holding this code in a module)
    VB Code:
    1. Workbooks.Open FileName:="bla.csv"
    2.         Windows("Your.xls").Activate
    3.         Range("A1").Value = Windows("blah.csv").Sheets("blah").Range("A1").Value
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2003
    Location
    Los Angeles, CA
    Posts
    49
    I can open the file fine when I include the filepath before it. But, even if the file I want to open is in the same directory as the file that is executing the macro I need the path.

    So my question is:

    How can I get excel to open the file from the same directory w/ out including a path.

    Thanks,
    Alex
    Last edited by LodBot; Jul 3rd, 2003 at 12:02 PM.

  4. #4
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    Since you can't be sure the path to this directory is set in the path-section of your OS, you have to include the path.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  5. #5

    Thread Starter
    Member
    Join Date
    Jul 2003
    Location
    Los Angeles, CA
    Posts
    49
    allright, now check this out... I can't get this code to compile:

    Code:
    dim filename as string
    dim worksheet as string
    
    filename = "C:\Documents and Settings\s118479\My Documents\training\_var_opt_psoft_prd_chr815_intf_out_tiperb32.csv"
    
    worksheet = "_var_opt_psoft_prd_chr815_intf_"
    
    Range("A1").Value = Windows(filename).Sheets(worksheet).Range("A1").Value
    I get "Error number 9 - Subscript out of Range"

    any ideas?

    Thanks for all the help opus!
    Alex

  6. #6
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    Compiling?
    Are you really using VBA or isn't it VB

    Using VBA I have no problem running that lines, but they are NOT opening the file filename

    VB Code:
    1. Workbooks.Open filename:=filename

    is missing. After that you need to reactivate the original file.
    VB Code:
    1. Windows("Your.xls").Activate
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  7. #7

    Thread Starter
    Member
    Join Date
    Jul 2003
    Location
    Los Angeles, CA
    Posts
    49
    I'm using VBA, by compiling I meant hitting F5. I tried the following lines of code:

    Code:
    dim filename as string
    dim Worksheet as string
    
    filename = "C:\Documents and Settings\s118479\My Documents\training\_var_opt_psoft_prd_chr815_intf_out_tiperb32.csv"
    
    Worksheet = "_var_opt_psoft_prd_chr815_intf_"
    
    Workbooks.Open filename:=filename
    Windows("SW Training A(2)1.xls").Activate
    
    Range("A1").Value = Windows(filename).Sheets(Worksheet).Range("A1").Value
    and am still getting the same error

    any ideas?

    Thanks,
    Alex
    Last edited by LodBot; Jul 3rd, 2003 at 01:15 PM.

  8. #8
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    Doesn't it tell where the error is?
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  9. #9

    Thread Starter
    Member
    Join Date
    Jul 2003
    Location
    Los Angeles, CA
    Posts
    49
    sorry, I should have posted that

    the error exhists in this line:

    Code:
    Range("A529").Value = Windows(filename).Sheets(Worksheet).Range("A3").Value
    I changed the cells so incase it DID work, no data would be overwritten.

    Thanks for all the help dude!

  10. #10
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    Found it, I think

    you need to keep the path and filename seperate, since you need the path and filename for the .open statement and only the fielname when you refer to the already open workbook!

    Maybe you can try to use
    VB Code:
    1. ChDir "C:\your path"
    to change the directory and use filename just with your filename.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  11. #11

    Thread Starter
    Member
    Join Date
    Jul 2003
    Location
    Los Angeles, CA
    Posts
    49
    Code:
    Dim filename As String
    Dim Worksheet As String
    Dim filepath As String
    
    filename = "_var_opt_psoft_prd_chr815_intf_out_tiperb32.csv"
    filepath = "C:\Documents and Settings\s118479\My Documents\training\"
    
    Worksheet = "_var_opt_psoft_prd_chr815_intf_"
    
    Workbooks.Open filename:=filepath & filename
    Windows("SW Training A(2)1.xls").Activate
    
    Sheets("Software Personnel").Range("A529").Value = Windows(filename).Sheets(Worksheet).Range("A3").Value
    I got the following error:

    error '439' object doesn't support this property or method
    and it didn't like this line:

    Code:
    Sheets("Software Personnel").Range("A529").Value = Windows(filename).Sheets(Worksheet).Range("A3").Value
    Do you think maybe my excel install went bad?

    Thanks for all the help dude,
    Alex

  12. #12
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    It looks like you can't read directly from the not activated file, try this (but since the error I got was '438' you might have another problem
    VB Code:
    1. Dim filename As String
    2. Dim Worksheet As String
    3. Dim filepath As String
    4. Dim Value As String
    5.  
    6. filename = "_var_opt_psoft_prd_chr815_intf_out_tiperb32.csv"
    7. filepath = "C:\Documents and Settings\s118479\My Documents\training\"
    8.  
    9. Worksheet = "_var_opt_psoft_prd_chr815_intf_"
    10.  
    11. Workbooks.Open filename:=filepath & filename
    12. Value = Sheets(Worksheet).Range("A3").Value
    13. Windows("SW Training A(2)1.xls").Activate
    14.  
    15. Sheets("Software Personnel").Range("A529").Value = Value
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  13. #13

    Thread Starter
    Member
    Join Date
    Jul 2003
    Location
    Los Angeles, CA
    Posts
    49
    that code ran! Now I'm getting an error that is really starting to piss me off!!!! Anyway, here's that STUPID line:

    Code:
    Workbooks.Open filename:="C:\training_dump\" & Range("A35").Value
    The correct filename is in A35, all the filenames and paths are correct! The reason why I'm so iratated is because when I substitute "C:\training_dump\" & Range("A35").Value with "C:\training_dump\open.csv" it works! The only answer to this question that I could think of is that maybe when you read from a cell, a new line gets added at the end?

    Any ideas?

    Thanks,
    Alex

    One more thing: How do I close the document that I opened previously, but still keep the main document open? If that doesn't make sense: How would I close the file "blah.xls" w/ out closing "open.xls" Thanks
    Last edited by LodBot; Jul 7th, 2003 at 11:44 AM.

  14. #14
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    Funny, that line works without problem for me!
    To close a workbook use:
    VB Code:
    1. Workbooks("blah.xls").Close
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  15. #15

    Thread Starter
    Member
    Join Date
    Jul 2003
    Location
    Los Angeles, CA
    Posts
    49
    thanks

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