|
-
Jul 3rd, 2003, 10:36 AM
#1
Thread Starter
Member
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.
-
Jul 3rd, 2003, 10:45 AM
#2
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:
Workbooks.Open FileName:="bla.csv"
Windows("Your.xls").Activate
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!
-
Jul 3rd, 2003, 11:56 AM
#3
Thread Starter
Member
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.
-
Jul 3rd, 2003, 12:55 PM
#4
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!
-
Jul 3rd, 2003, 12:59 PM
#5
Thread Starter
Member
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
-
Jul 3rd, 2003, 01:08 PM
#6
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:
Workbooks.Open filename:=filename
is missing. After that you need to reactivate the original file.
VB Code:
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!
-
Jul 3rd, 2003, 01:11 PM
#7
Thread Starter
Member
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.
-
Jul 3rd, 2003, 01:13 PM
#8
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!
-
Jul 3rd, 2003, 01:14 PM
#9
Thread Starter
Member
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!
-
Jul 3rd, 2003, 01:35 PM
#10
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
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!
-
Jul 3rd, 2003, 01:43 PM
#11
Thread Starter
Member
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
-
Jul 3rd, 2003, 02:04 PM
#12
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:
Dim filename As String
Dim Worksheet As String
Dim filepath As String
Dim Value 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
Value = Sheets(Worksheet).Range("A3").Value
Windows("SW Training A(2)1.xls").Activate
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!
-
Jul 7th, 2003, 10:26 AM
#13
Thread Starter
Member
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.
-
Jul 7th, 2003, 12:10 PM
#14
Funny, that line works without problem for me!
To close a workbook use:
VB Code:
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!
-
Jul 8th, 2003, 10:16 AM
#15
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|