Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open a CSV file from string given in cell of active workbook 1

Status
Not open for further replies.

Zac5

Programmer
Jan 30, 2003
75
US
Dear All,

1. My cell refernce (1,1) contains a string e.g. myfilename (without the extension e.g. myfilename.csv) I want my macro in the active workbook to open myfilename.

Workbooks.Open Filename:=Range(1, 1) does not work because its a csv. How can I open it, I don't want to have to convert it or anything.

2. I will then want to do some copy/pasting from it into my active workbook, do I need to convert the csv to an excel workbook before I can do this, if so, how can I convert?
 
What about this ?
Workbooks.Open Filename:=Trim(Range(1, 1)) & ".csv"

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 



Hi,

I would not use the Workbooks.Open method.

Rather on a blank sheet, do this FIRST... Data > Import External Data > IMPORT... and import the .csv file, parsing as appropriate.

THEN, turn on your macro recorder and record eiding the query table you just added to your sheet... Data > Import External Data > Edit Text Import do the import and FINISH.

Now you have recorded some VBA code. Post back with your recorded code to get help modifying to fit your requirement.

Skip,
[glasses]Don't let the Diatribe...
talk you to death![tongue]

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
For rrng = 2 To LastRow

Workbooks.Open Filename:=Cells(rrng, 1)

'fails at next line because (rrng, 2) is of type csv

Workbooks.Open Filename:=Cells(rrng, 2)
Workbooks.Open Filename:=Cells(rrng, 3)

Next rrng
 
What about something like?
Workbooks.Open Filename:=Sheets("Sheet1").Cells(1, 1).Value & ".csv"
 
'fails at next line because (rrng, 2) is of type csv
I'd say; fails because the active workbook has changed ...
What about this ?
Code:
Set ws = ActiveSheet
For rrng = 2 To LastRow
  Workbooks.Open Filename:=ws.Cells(rrng, 1)
  Workbooks.Open Filename:=ws.Cells(rrng, 2)
  Workbooks.Open Filename:=ws.Cells(rrng, 3)
Next rrng

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks a lot all PHV's last post has solved my problem

Thanks PHV :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top