Opening and closing files
Much the same as before.
- Open ThisWorkbook.Path & "\things.csv" For Output As #1
- Close #1
things.csv doesn't exist, then VBA will create it. If it already exists, then VBA will erase the file, and make a new empty one.
We're doing output so this is in the O part of an IPO program.
Say you have the variables
tProductName, a string, and
sPrice, a single. Output them to a CSV file like this:
- Write #1, tProductName, sPrice
Goat t-shirt, and
17.95, you'd get:
Write statement puts quotes around strings for you. M0000t!
Aussie rules with CSV output
Repeat the Aussie rules exercise, but with output to a CSV file. The worksheet starts like this:
Your program should:
- Compute the score and show it on the worksheet.
- Output to a CSV file with the name the user types in the worksheet.
- Show when the operation is complete.
Here's some output:
Here, your program would make a file called
rules.csv, with this:
Notice that your program sends output to two places: the worksheet, and a file.
Here's another example. Suppose the user put this data in:
They click Run, and see:
The program would create
moose.csv. Its contents:
Upload your worksheet.
If you were logged in as a student, you could submit a solution to this exercise.