The VBA Editor Workout
- 06:11
Using the VBA editor to step into the code from a recorded macro.
Transcript
Let's go into the Visual Basic Editor and examine the code, examining the code and walking through the code to see how our code is being executed is best done.
If we tile the Excel front end window with the Visual Basic window in the Visual Basic Editor, we can see our code for module one and our code is here and it starts with the instruction sub.
This is our sub procedure called Copy Paste.
A sub procedure comprises a set of instructions.
Excel VBA executes each line of the procedure.
In order to see each line of our code being executed, we can step into the procedure and follow Excel VBA as each line of code is executed.
In order to do this, let's go back to the Excel window and clear all of the cells that we copied to. Going into the Visual Basic Editor.
To step into our code, we can either choose debug from the menu and press step into, or we can use the shortcut key F 8 to step into the code.
In the code. Let's press F 8 and each time we press F 8, a line of code is executed.
You'll notice that when I press F 8, it jumps right the way past the line.
That starts with apostrophe copy paste macro.
A line in the Visual Basic editor that starts with an apostrophe is treated as a comment.
Comments are there for our information and they're there to help our colleagues potentially understand any code that we have written, they will not be executed.
Writing comments into our code is really, really good practice.
Let's press F 8 again and walk through each line of code being executed.
The area is highlighted, The selection is copied.
Cell F 10 is selected.
The contents of our highlighted area are pasted.
Copy paste mode is turned off.
G 11 to G 22 is selected and the font changed to bold.
If we walk through each of these subsequent lines of code, we can see that the cell alignment is centered, but we don't need to change the cell alignment to the bottom wrap text.
We don't need orientation, add, indent, indent level, shrink to fit.
We can see as we walk through each of these lines of code, it does not change our Excel.
The last thing that we did was select cell H 10, and this was really superfluous pressing F 8 one final time completes our step into the code.
We can walk through our macro again by stepping into the code and this time we can attempt to remove any superfluous lines.
These are lines of code that do not change our Excel in anyway.
Let's try this one again.
So back in Excel, let's clear out columns F and G, and we'll step into a line of code.
So either debug, step into or F 8.
We need all of these first lines, but we can see from vertical alignment equals Excel bottom.
None of these lines of code are making any changes to our Excel spreadsheet, and we certainly don't need range H 10 to be selected.
We can remove those lines of code either by deleting them, or we can put an apostrophe in front of that line of code to treat it like a comment back to the Excel front end.
Let's clear all of the contents From columns F and G, and we're going to put an apostrophe in front of the lines of code that we don't think actually perform actions we require.
So we can put an apostrophe in front of the vertical alignment, the wrap text, the orientation, add, indent, indent level reading, order, merge cells, and then finally the range H 10 select.
Let's make sure that our code still carries out all of the instructions that we want it to.
And again, we'll step into this code pressing F 8 steps into our code, and each time we press F 8, we execute a line of code.
The area is highlighted, copied, sell F 10 selected, and then pasted cells G 11 to G 22 selected, turned bold, and then then center aligned.
Even though our code is down much more efficient, it could still be refined further.