Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
VBA Automation for Excel 2019 Cookbook

You're reading from   VBA Automation for Excel 2019 Cookbook Solutions to automate routine tasks and increase productivity with Excel and other MS Office applications

Arrow left icon
Product type Paperback
Published in Sep 2020
Publisher Packt
ISBN-13 9781789610031
Length 362 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Mike Van Niekerk Mike Van Niekerk
Author Profile Icon Mike Van Niekerk
Mike Van Niekerk
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Preface 1. Chapter 1: Getting Started with VBA 2. Chapter 2: Working with the VBA Editor FREE CHAPTER 3. Chapter 3: The VBA Object Model 4. Chapter 4: Working with Procedures 5. Chapter 5: Next Level Recording 6. Chapter 6: VBA Language Elements 7. Chapter 7: Working with Ranges 8. Chapter 8: Using Functions 9. Chapter 9: Implementing Program Flow 10. Chapter 10: Implementing Automation 11. Chapter 11: Handling Errors 12. Chapter 12: Debugging 13. Chapter 13: Creating and Modifying Dialog Boxes 14. Chapter 14: Creating UserForms 15. Chapter 15: UserForm Controls 16. Chapter 16: Creating Custom Functions 17. Chapter 17: Creating Word Documents with Excel VBA 18. Chapter 18: Working with PowerPoint in Excel VBA 19. Other Books You May Enjoy

Creating a basic procedure in the code window of the Editor

In this recipe, we will create a basic procedure in the code window of the Editor. We're at the point where everything is coming together now; you know the different components, and you know how to create a module. All that's left is to create code – a simple Sub procedure – in the newly created module.

Getting ready

Make sure that you've created a module in the VBA Editor. The module should be visible as an object in the Project window, and the title bar of the code window should display the name Module1 (Code). If you cannot see the title bar, click on the restore window button to make the window smaller.

How to do it…

Let's go through the steps for this recipe:

  1. In the code window, type the following code:
    Sub MyName()
        MsgBox "My name is Yourname" (Replace Yourname _
        with your first name.)
    End Sub
  2. Press Alt + F11 to switch to Excel.
  3. Click on any cell of the active sheet. Go to Developer | Code, and click on the Macros icon. The Macro dialog box appears:
Figure 2.16 – The Macro dialog box

Figure 2.16 – The Macro dialog box

The only available macro is the one that you've just created; MyName. Click on the Run button.

A message box will appear with the words My name Mike, as shown:

Figure 2.17 – Message box from a basic procedure

Figure 2.17 – Message box from a basic procedure

A much shorter way to execute the code is to press F5 while in the VBA Editor. You will automatically switch to Excel, where the message box will be displayed.

How it works…

Entering code in the VBA Editor is relatively simple, on the condition that you have a basic understanding of VBA coding. Looking at a few code samples online before going solo is advisable, and will save you many hours of frustration.

As a bonus, the Editor will make adjustments to the text you enter. The first example of this is when you type the Sub statement and the name of your procedure. When you press Enter, the Editor automatically inserts the End Sub statement. As we progress, you will see how spaces are inserted, and how capitalization and text colors are changed.

Once your code is error-free, the code can be run. In this case, the outcome is a message box, displaying the words My name Mike.

You have been reading a chapter from
VBA Automation for Excel 2019 Cookbook
Published in: Sep 2020
Publisher: Packt
ISBN-13: 9781789610031
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image