Introduction to Visual Basic for Applications (VBA) in Excel
Visual Basic for Applications can be used to extend the capabilities of Excel with custom calculations. Microsoft Excel itself is not a programming language but does include an integrated development environment for recording and running macros (VBA environment). Excel macros are a series of commands that can be repeated to operate repeatedly on sets of data.
Below is an example problem that demonstrates whether a tank of particular dimensions will overflow with a certain amount of time. The tutorial shows how to import data from the worksheet, perform volume calculations, determine whether the tank will overflow with an IF statement, print out a message box, and export the volume values to the active worksheet.
This same example problem is also demonstrated with MATLAB and in the Python programming language
Generative AI Learning
Use these prompts to test your understanding after completing the tutorial. This is the first exercise where AI-generated code is on the table - practice the full pattern: specify, generate, verify, defend.
Tip: The second prompt is the pattern for every code generation in this course: an ambiguous spec produces confident wrong code. Run all 3 test cases, including the boundary case - a macro that works on one example is not verified. Compare the generated macro to your own recorded/written version: differences are where the learning is.
What to Turn In
Submit a short report (PDF, 1-2 pages) with your homework that curates your results into a demonstration of what you learned. You may use Generative AI to help write the report, but you must guide it to the correct code, justifications, and assumptions. Answer these questions:
- Include your own macro (written or recorded and cleaned) for the assigned problem, with a comment on each block saying what it does.
- Include the spec you wrote for the AI, the generated code, and the results of the 3 verification test cases (one boundary case). Did any test fail, and what did you fix - the spec or the code?
- What assumptions did the AI list that your spec left open? Which one could have produced a wrong engineering answer?
- For the tank-overflow logic: hand-calculate one case (given dimensions and fill rate, does it overflow?) and show the macro agrees.
- From the quiz prompt: one question you missed and the corrected answer.
