Hi,
I have heard that recording macros and watch the code develop in the VBA window is a great way to develop an intuitive sense on how different things work in the VBA environment. Today, I explored recording a macro on creating graphs. I successfully recorded a macro by creating a box & whisker graph and was amazed to see all the lines of code that were created. Within the first 8 lines that the macro recorded, I gained two thoughts. I was hoping that somebody might clear up my thoughts:
- First, and unimportantly, I understand a lot of the code is redundant and is not necessarly needed. This thought is unimportant at the moment.
- Second, and the most important thought is this: Based of the line of code on line 8, this code will not work again if I try to re-run it.
I went back into Excel, deleted Chart 5, and re-ran the Macro. I hoped my intuition would be wrong and the code would run fine. But my intuition was right and the code crashed. ActiveSheet.ChartObjects("Chart 5").Activate: The problem is here, isn't? Once I deleted Chart 5, and reran the Macro to see if it would work, the Macro didn't have a chart to reference, right?
So my question is this: If I was to write some code that would create a chart, how would I go about putting in a variable for the chart name? I am not even sure where to begin? Would it look something like this?
Bookmarks