Activate a CSV file that is already open

Mike UK

New Member
Joined
Dec 14, 2011
Messages
41
My code opens 7 new workbooks and saves them as CSV files without closing them. Later in the code I need to activate the relevant CSV file to post data into it. However, I can't seem to activate the appropraite CSV file. If the files are saved as FileFormat:= xlNormal the code runs perfectly however when i change to FileFormat:= xlCSV - the following code won't activate the relevant CSV file. Any ideas?

Workbooks((Format(Date, "yymmdd") & " " & arrPlatform(0) & " " & arrModelData(R, 2))).Activate

Thanks
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
VBA needs
Workbooks("FileName.csv").Activate

At a guess perhaps
VBA Code:
Workbooks(Format(Date, "yymmdd") & " " & arrPlatform(0) & " " & arrModelData(R, 2) [B]& ".csv"[/B]).Activate

If that does not work, look at the string that is generated by
Format(Date, "yymmdd") & " " & arrPlatform(0) & " " & arrModelData(R, 2)
and modify it give VBA what it is looking for
 
Upvote 0
Oops I bolded first :oops:
- forgot that VBA code tags do not accept extra formatting ( hence [ B ] & ".csv" [ /B ] )

Should look like this ....
VBA Code:
Workbooks(Format(Date, "yymmdd") & " " & arrPlatform(0) & " " & arrModelData(R, 2) & ".csv").Activate
 
Upvote 0
Oops I bolded first :oops:
- forgot that VBA code tags do not accept extra formatting ( hence [ B ] & ".csv" [ /B ] )

Should look like this ....
VBA Code:
Workbooks(Format(Date, "yymmdd") & " " & arrPlatform(0) & " " & arrModelData(R, 2) & ".csv").Activate

Hi Yongle,
Thanks for your reply the code didn't appear to work. I've read that VBA doesn't recognise CSV files as a workbook is that correct?
Mike
 
Upvote 0
Did you test the string Format(Date, "yymmdd") & " " & arrPlatform(0) & " " & arrModelData(R, 2) ?

Print it to the Immediate Window in VBA like this
VBA Code:
Debug.Print Format(Date, "yymmdd") & " " & arrPlatform(0) & " " & arrModelData(R, 2)
Debug.Print Format(Date, "yymmdd") & " " & arrPlatform(0) & " " & arrModelData(R, 2) & ".csv"
Display Immediate Window in VBA editor with {CTRL} g

Does either provide the correct file name ?
 
Upvote 0
Did you test the string Format(Date, "yymmdd") & " " & arrPlatform(0) & " " & arrModelData(R, 2) ?

Print it to the Immediate Window in VBA like this
VBA Code:
Debug.Print Format(Date, "yymmdd") & " " & arrPlatform(0) & " " & arrModelData(R, 2)
Debug.Print Format(Date, "yymmdd") & " " & arrPlatform(0) & " " & arrModelData(R, 2) & ".csv"
Display Immediate Window in VBA editor with {CTRL} g

Does either provide the correct file name ?

Hi Yongle,
Many thanks for your assistance and help with this. Your coding works fine. What I hadn't realised was that when I changed the FileFormat to xlCSV it changed the Active Window and also the sheet name. Needed to add a bit of code to ensure ThisWorkBook.Activate was in place and ignore the name of the sheet tab.

All working fine now. Thanks for you time.
Mike
 
Upvote 0
good - glad you were able to work it out.
thanks for the fedback
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,243
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top