Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
52 views
VBA For Modelers (Chapter 3)
Capitulo 3
Uploaded by
Jorge Rodríguez Juárez
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save VBA for Modelers (Chapter 3) For Later
Download
Save
Save VBA for Modelers (Chapter 3) For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
52 views
VBA For Modelers (Chapter 3)
Capitulo 3
Uploaded by
Jorge Rodríguez Juárez
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save VBA for Modelers (Chapter 3) For Later
Carousel Previous
Carousel Next
Save
Save VBA for Modelers (Chapter 3) For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 17
Search
Fullscreen
8 3.1 3.2 The Visual Basic Editor Introduction At this point, you might be asking where VBA lives. I claimed in Chapter 1 that if you own Excel, you also own VBA, but many of you have probably never seen it. You do your VBA work in the Visual Basic Editor (VBE), which you can access easily from Excel by pressing the Alt+F11 key combination. The VBE provides a very user-friendly environment for writing your VBA programs. This chapter walks you through the VBE and shows you its most important features. It also helps you write your first VBA program. By the way, you might also hear the term Integrated Development Environment (IDE). This is a general term for an environment where you do your programming, regardless of the programming language. The VBE is the IDE for programming with VBA in Excel, Important Features of the VBE To understand this section most easily, you should follow along at your computer. Open Excel and press Alt+F11 to get into the VBE." It should look something Figure 3.1, although the configuration you see might be somewhat different. By the time this discussion is completed, you will be able to make your sereen look like that in Figure 3.1 or change it according to your own preferences. This is your pro- gramming workspace, and you have a lot of control over how it appears. This chapter provides some guidance, but the best way to learn is by experimenting. The large blank pane on the right is the Code window. This is where you write your code. (If any of the windows discussed here are not visible on your screen, you can select the View menu from the VBE and then select the window you want to make visible.) The rest of the VBE consists of the top menu, one or more toolbars, and one or more optional windows. Let’s start with the windows. "In Excel 2008 and eae, the Tools -> Maero ~+ Visual Basic Editor men item also gets you into the VBE, but Alt+F11 is quicker. In Excel 2007 and later versions, you should first make the Developer ribbon visible. To do this in Excel 2007, click the Office button and then Excel Options. Under the Popular tab, Select the third option at the top: Show Developer tab in the Ribbon. In Excel 2010 and later versions, rightclick any ribbon and seleet Customize the Ribbon. Then check the Developer item in the right pane. You need to do this only once, The Developer tab is a must for programmers. Among other things, you can gee to the VBE by clicking on its Visual Basic button, but again, AltsFLL is quicker‘The Visual Basic Editor 19 Figure 3.1. Visual Basic Editor (VBE) Type a question for hep 5-8 Personal (PERSONALS) 1-8 SoLveR (SoLvER-xLA) 5 Bf StatToolsxt (StatToolsa) 28 VoaProject (Bookz) EG) Marra Excel objets Shot (Sect!) Tsrbok SG Mees Radi The Project Explorer window, repeated in Figure 3.2, shows an Explorer- type list of all open projects. (Your list will probably be different from the one shown here. It depends on the files you have open and the add-ins that are loaded.) For example, the active project shown here has the generic name VBA- Project and corresponds to the workbook Book2—that is, the file Book2.xlsx.” Below a given project, the Project Explorer window shows its “elements.” In the Microsoft Excel Objects folder, these clements include any worksheets or chart sheets in the Excel file and an clement called ThisWorkbook, which refers to the workbook itself. There can also be folders for modules (for VBA code), user forms (for dialog boxes), and references (for links to other libraries of code you need), depending on whether you have any of these in your project. Modules, user forms, and references are discussed in detail in later chapters. 2For our purposes, there i no difference between a project and a workbook. However, VBA allows them to have separate names: VBAProject and Book2, for example. If you save Book2 as Practicealsm, sy, the project name will sll be VBAProject Admitely, it i somewhat confusing, but just think of projects as Excl les20. Chapter 3 Figure 3.2 Figure 3.3 Project Explorer Window © & Personal (PERSONAL.XLS) [© SS SOLVER (SOLVER.XLA) fe BS StatToolski (StatTools.xla) [88 vBaProject (Book2) -& Microsoft Excel Objects BB) Sheett (Sheet!) 8) ThisWorkbook 65 Modules The Properties window, shown in Figure 3.3, lists a set of properties. This list depends on what is currently selected. For example, the property list in Figure 3.3 is relevant for the project itself: It indicates a single property only—the project’s name, Therefore, if you want to change the name of the project from the generic VBAProject to something more meaningful, such as MyFirstProgram, this is the place to do it. Chapter 11 discusses the use of the Properties window in much more detail, At this point, you don’t really need the Properties window, so you can close it by clicking on its close button (the upper right X), The VBE also has at least three toolbars that are very useful: Standard, Edit, and Debug. They appear in Figures 3.4, 3.5, and 3.6, where some of the most useful buttons are indicated. (If any of these toolbars are not visible on your Properties Window eI Alphabetic | categorized | VeAProjectThe Visual Basic Editor 21 Figure 3.4 Standard Toolbar (Bs | 4 Pa 8 OO) ee | Se | @ | TC i Runa program ‘Show Control Toolbox Show Object Browser Pause a program cam prota ‘Show Properties Window ‘Show Project Window Figure 3.5 Edit Toolbar hh Bee Bae pa | \ \ Yoram sta block, Sate ‘Camment a block Seta Break Figure 3.6 Debug Toolbar Saas % [aaron] rl into] [Step over] computer, you can make them visible through the View menu.) From the Standard toolbar, you can run, pause, or stop a program you have written, You can also display the Project or Properties window (if itis hidden), and you can display the Object Browser or the Control Toolbox (more about these later). From the Edit toolbar, you can perform useful editing tasks, such as indenting or outdenting (the opposite of indenting), and you can comment or uncomment blocks of code, as is discussed later. Finally, although the Debug toolbar will probably not mean much at this point, itis invaluable when you need to debug your programs—as you will undoubtedly need to do. Itis discussed in more detail in Chapter 5. For fiature reference, here are a few menu items of particular importance. You usually need at least one module in a project. This is where you will typ: ically store your code. To insert a module, use the Insert > Module menu item, If you ever have a module you do not need, highlight the module in the Project Explorer window and use the File > Remove Module menu item. (Answer No to whether you want to export the module.)22 Chapter 3 3.3 ‘© Chapter 11 explains how to build your own dialog boxes. VBA calls these user forms. To insert a new user form into a project, use the Insert -> User Form menu item. You can delete an unwanted user form in the same way you delete a module. ‘© Under the Insert menu, there is also a Class Module item. You can usually ignore this. It is more advanced, but it is discussed briefly in Chapter 18. ‘© The Tools > Options menu item allows you to change the look and feel of the VBE in a variety of ways. You should probably leave the default settings alone— with one important exception. Try it now. Select Tools -* Options, and make sure the Require Variable Declarations box under the Editor tab is checked. ‘The effect of this is explained in Chapter 5. You might also want to uncheck the Auto Syntax Check box, as I always do. Ifit is checked, the editor beeps at you each time you make a syntax error ina line of code and then press Enter. This can be annoying. Even if this box is unchecked, the editor will still warn you about a syntax error by coloring the offending line red. ‘* Ifyou ever want to password-protect your project so that other people can- not see your code, use the Tools -> VBA Properties menu item and click the Protection tab. This gives you a chance to enter a password. (Just don’t forget it, or you will not be able to see your own code.) * Ifyou click the familiar Save button (or use the File + Save menu item), this saves the project currently highlighted in the Project Explorer window. It saves your code avd anything in the underlying Excel workbook. (It is all saved in the .xlsm file.) You can achieve the same objective by switching back to Excel and saving in the usual way from there. (Note, however, that in Excel 2007 and later versions, if your file started as an .xlsx file without any VBA code, you will have to save it as an .xlsm file once it contains code.) The Object Browser VBA’s Object Browser is a wonderful online help tool. To get to it, open the VBE and click the Object Browser button on the Standard toolbar (see Figure 3.4). Ifyou prefer keyboard shortcuts, you can press the F2 key. Either way, this opens the win- dow shown in Figure 3.7. At the top left, there is a dropdown list of Hibraries that you can get help on. Our main interest is in the Excel library, the VBA library, and, to a lesser extent, the Office library. ‘The Excel library provides help on all of the objects and their properties and methods in the Excel object model. The VBA library provides help on the VBA elements that are common to all applications that can use VBA: Excel, Access, Word, and others. The Office library provides help on objects, common to all Office programs, such as CommandBars objects (menus and toolbars) For now, sclect the Excel library. In the bottom left pane, you see a list of all objects in the Excel object model, and in the right pane, you see a list of all properties and methods for any object selected in the left pane. A property is des- ignated by a hand icon, and a method is designated by a green rectangular icon. A few objects, such as the Workbook object, also have events they can respond to. An event is designated by a lightning icon.
You might also like
PE3013 Computer Applications in Petroleum Engineering Slides PDF
PDF
No ratings yet
PE3013 Computer Applications in Petroleum Engineering Slides PDF
28 pages
Introduction To VBA: Prof. Name: Position (123) 456-7890 University Name
PDF
No ratings yet
Introduction To VBA: Prof. Name: Position (123) 456-7890 University Name
16 pages
Page 1 of 38 VBA For Excel Lesson 1: The
PDF
No ratings yet
Page 1 of 38 VBA For Excel Lesson 1: The
38 pages
VBA For Excel
PDF
No ratings yet
VBA For Excel
56 pages
VBA For Excel
PDF
No ratings yet
VBA For Excel
60 pages
VBA For Excel Lesson 1
PDF
No ratings yet
VBA For Excel Lesson 1
54 pages
VBA For Excel Lesson 1
PDF
No ratings yet
VBA For Excel Lesson 1
72 pages
VBA With Excel VBA With Excel
PDF
No ratings yet
VBA With Excel VBA With Excel
51 pages
1.0 VB Editor 1.1 Opening The VB Editor: Flash Exercise
PDF
No ratings yet
1.0 VB Editor 1.1 Opening The VB Editor: Flash Exercise
9 pages
The Visual Basic Editor in Excel (VBE)
PDF
No ratings yet
The Visual Basic Editor in Excel (VBE)
10 pages
VBA in Excel
PDF
50% (2)
VBA in Excel
64 pages
Excell Macro
PDF
No ratings yet
Excell Macro
69 pages
Excel VB A Introduction
PDF
No ratings yet
Excel VB A Introduction
110 pages
Excel Vba Introduction 1
PDF
100% (1)
Excel Vba Introduction 1
88 pages
Excel Vba Introduction 1
PDF
No ratings yet
Excel Vba Introduction 1
87 pages
Intro To VBA Session-1&2a: Important
PDF
No ratings yet
Intro To VBA Session-1&2a: Important
20 pages
Excel VBA The Ultimate Intermediates Guide To Learn VBA Programming Step by Step by A - 1
PDF
100% (8)
Excel VBA The Ultimate Intermediates Guide To Learn VBA Programming Step by Step by A - 1
183 pages
VBATutorialCode Book
PDF
No ratings yet
VBATutorialCode Book
43 pages
Visual Basic For Applications
PDF
No ratings yet
Visual Basic For Applications
5 pages
Learn Excel VBA in 8 Easy Lessons
PDF
No ratings yet
Learn Excel VBA in 8 Easy Lessons
116 pages
Auto Vba PDF
PDF
No ratings yet
Auto Vba PDF
5 pages
Visual Basic For Applications Fundamentals
PDF
No ratings yet
Visual Basic For Applications Fundamentals
5 pages
Basics of Macro and VBA
PDF
No ratings yet
Basics of Macro and VBA
16 pages
Visual Basics For Applications
PDF
No ratings yet
Visual Basics For Applications
141 pages
Complete VB
PDF
No ratings yet
Complete VB
159 pages
The Visual Basic Editor: School of Construction
PDF
No ratings yet
The Visual Basic Editor: School of Construction
43 pages
Vba Unit 1
PDF
No ratings yet
Vba Unit 1
34 pages
Chapter 1: Introduction To VBA: Common Uses of VBA
PDF
No ratings yet
Chapter 1: Introduction To VBA: Common Uses of VBA
31 pages
MsExcel VBA Course Material
PDF
No ratings yet
MsExcel VBA Course Material
37 pages
VBA Notes
PDF
No ratings yet
VBA Notes
73 pages
VBA - Basics and Advanced
PDF
100% (5)
VBA - Basics and Advanced
136 pages
Excel VBA Tutorial
PDF
No ratings yet
Excel VBA Tutorial
8 pages
Macro Intro
PDF
No ratings yet
Macro Intro
5 pages
Excel VBA For Complete Beginners A Home A - Ken Carney
PDF
100% (2)
Excel VBA For Complete Beginners A Home A - Ken Carney
385 pages
VBA Lecture Notes
PDF
No ratings yet
VBA Lecture Notes
43 pages
VBA Primer
PDF
No ratings yet
VBA Primer
11 pages
VBA Is Short For Visual Basic For Application.: History Hint
PDF
No ratings yet
VBA Is Short For Visual Basic For Application.: History Hint
4 pages
Excel VBA (4)
PDF
No ratings yet
Excel VBA (4)
18 pages
Excel Tutorial 12 Expanding Excel With Visual Basic For Applications
PDF
No ratings yet
Excel Tutorial 12 Expanding Excel With Visual Basic For Applications
27 pages
Intro VBA
PDF
No ratings yet
Intro VBA
39 pages
3.13 How To Clean Your Code
PDF
No ratings yet
3.13 How To Clean Your Code
4 pages
Introduction To Excel VBA Macros
PDF
No ratings yet
Introduction To Excel VBA Macros
39 pages
Visual Basic For Applications
PDF
No ratings yet
Visual Basic For Applications
12 pages
Excel VBA 1
PDF
No ratings yet
Excel VBA 1
30 pages
Introduction To VBA in Excel Week 1: Study Items For This Week
PDF
No ratings yet
Introduction To VBA in Excel Week 1: Study Items For This Week
7 pages
Excel Macros Beginners PDF
PDF
No ratings yet
Excel Macros Beginners PDF
6 pages
Basics of VBA Macro
PDF
No ratings yet
Basics of VBA Macro
12 pages
Excel VBA Made Easy-Liew Voon Kiong
PDF
100% (3)
Excel VBA Made Easy-Liew Voon Kiong
93 pages
VBA Macros
PDF
No ratings yet
VBA Macros
77 pages
E-Learning Excel VBA Programming Lesson 1
PDF
No ratings yet
E-Learning Excel VBA Programming Lesson 1
13 pages
Excel Vba Programminw: Ceaneering
PDF
No ratings yet
Excel Vba Programminw: Ceaneering
71 pages
VBA Tutorial Beginners
PDF
No ratings yet
VBA Tutorial Beginners
24 pages
Using Excel/VBA To Speed-Up Daily Data Processing
PDF
No ratings yet
Using Excel/VBA To Speed-Up Daily Data Processing
39 pages
Excel Vba
PDF
No ratings yet
Excel Vba
106 pages