-
Book Overview & Buying
-
Table Of Contents
VBA Automation for Excel 2019 Cookbook
By :
Our first Sub procedure was as simple as it gets. It wouldn't take much imagination to understand that coding can become more complex than that. If that's the case, it makes you wonder how long it would take to manually type the coding for super-long Sub procedures.
All depending on the situation, there is more than one option to create coding. In this recipe, we will investigate the different methods to create code.
With Excel open, activate a worksheet in the workbook.
We will now go through the steps of working with each method to create code.
When using this method, you'll need to perform the following steps:

Figure 2.18 – Fill the range A1:A10

Figure 2.19 – The Macro dialog box

Figure 2.20 – Formatting the range

Figure 2.21 – Code for the recorded macro
In this method, we will be typing the code manually using the following steps:
Sub MsgBoxOKCancel() MsgBox "Do you want to continue?", vbOKCancel End Sub
When you work in MS Word and want to copy text from one document to the other, you need both pages to be open. The same applies for code in VBA.
Open the module with the code you want to copy, as well as the one where you want to paste it. Select the code in the first module – use Ctrl + C, or right-click and Copy – then switch to the new module. Ctrl + V will paste it, or you can right-click and select Paste.
Now let's see how each method works.
Many Excel users are introduced to the world of VBA coding after recording a macro or two. Looking at the recorded steps and starting to understand how VBA works is quite exciting.
If you have to record a series of steps in Excel, there is no better or faster way to create the code than with a macro.
It normally comes as a shock when you realize that not all coding can be done with the macro recorder.
Take, for instance, the message box we created with the sample code. There is no way to create that message box other than manually typing the code.
If you are new to VBA, typing code manually might sound like a challenge. However, with a little bit of experience, you will soon be able to type lines of code like a professional.
Like with any other typing, you can cut, copy, paste, and delete text in the Editor. To make the code easier to read, indent some of the lines with the Tab key. Don't forget that undo and redo can also be used!
If a single line becomes too long to read on one screen, use a space and an underscore (_) to break to a new line.
This sounds like the lazy man's escape, but there are many good reasons for copying code. Firstly, if you created a Sub procedure for another project that can be used in your current project, it would be a waste of time to retype it.
You will also find many examples of VBA code on the internet. This is a very good way of learning from people with experience.
To make the macro we recorded a bit more useful, do the following:
Currency_Format Sub procedure and paste it directly below the last line.Range_Format.Range_Format procedure.Your code should now look like this:
Figure 2.22 – Altered code
If you switch back to Excel, you can now use the second macro to highlight any range and format it to accounting.