Creating

Recording your own macros can be very useful although there will be times when you want to write a macro from scratch.
Examples of good VBA code can be found on web sites and in newsgroups and forums which you might find useful.
You might want to use this code or modify the code to make it specific to the task at hand.
It is very easy to create a macro to allow you to enter (or copy and paste) the necessary code.


Using the Macro dialog box

Select (Tools > Macro > Macros) to display the Macros dialog box.
Enter the name in the Macro name box.
The default location for any macros you create is your Normal.dot.
Press Create to create the macro.

microsoft excel docs

Insert or Write the Code

The Visual Basic for Applications window will be opened automatically.
You will see that a code module has been added to the Normal.dot and that the following code has been added:

microsoft excel docs

You are now ready to insert or paste in you own VBA code.
Once you have entered your VBA code select (Debug > Compile) to make sure your code does not contain any syntax errors.
After you have finished you can return to Word by selecting (File > Close and Return to Microsoft Word).


Save your changes

Then select (File > Save) to save the macro in that workbook.
You can actually run the macro while you are in the Visual Basic Editor window by pressing F5.
You can also resize the windows so you can see them both at the same time.
You can then run the macro by selecting (Tools > Macro > Macros), selecting the name of the macro and pressing the "run" button.


Adding Shortcut keys and Descriptions to your Macros

This does not have to be done when you record the macro and can easily be done afterwards.
You can add either a Shortcut Key or a Description to your macro by selecting (Tools > Macro > Macros) and selecting the "Options" button at the bottom.
If you add a description before recording a macro then this description will appear as a comment on the first few lines of the recorded VBA code.


Public vs Private

All procedures and Public by default.
To prevent a macro or procedure from appearing in the (Tools > Macro > Macros) dialog box you can change the scope from Public to Private.
It is always a good habit to explicitly state whether public or private to avoid any confusion.


Using the Status bar

If you switch the ScreenUpdating off to prevent the screen from flickering it is worth using the Status bar to keep the user informed about the current status of the macro.
If your macro takes a bit of time to complete using the status bar will ensure that the user does not think that the macro has crashed or is not responding.

   ' Make sure that the status bar is visible
Application.DisplayStatusbar = True

' Enter the message to be displayed
Application.Statusbar = "Please wait .."

' **** Add Your Code Here ****

' When the macro has finished reset the status bar
Application.Statusbar = False

If you change the status bar to display a message to the user - make sure you reset it.


Running your Macros

An alternative to using a command button might be to run a macro by using a form field.


Important

If you are writing VBA code then make sure the following two options are selected in your (Tools > Options)(Editor tab) of the Visual Basic Editor window: "Auto List Members" and "Auto Quick Info".


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext