lec 01 - introduction to microsoft excel and vba
TRANSCRIPT
-
7/31/2019 Lec 01 - Introduction to Microsoft Excel and VBA
1/30
Introduction to Microsoft Excel and VBA
Using Microsoft Excel
Introduction
Microsoft Excel is a spreadsheet application used to create lists, perform calculations, and
analyze numbers. It can be used in business, economics, or accounting, etc.
While the default features of Microsoft Excel should be enough in most scenarios, in some cases
you will want more complex functionality to perform advanced operations. To make this possible,Microsoft Excel is accompanied by Microsoft Visual Basic, a programming environment that allows
you to use the Visual Basic language to enhance the usefulness and functionality of a spreadsheet.
Introduction to Microsoft Excel
To use Microsoft Excel, you can launch like any regular Microsoft Windows application. You can
click Start -> (All) Programs -> Microsoft Office -> Microsoft Office Excel 2007. If you have aMicrosoft Excel document in Windows Explorer, in My Documents, or in an email, etc, you can
double-click it. This would also start Microsoft Excel and would open the document.
The classic way users launch Microsoft Excel is from the Start menu on the task bar. You can also
start the application from a shortcut on the desktop. There are many ways you can create ashortcut on your desktop. To create a Microsoft Excel shortcut on the desktop, do one of the
following:
Practical Learning: Starting Microsoft Excel
To start Microsoft Excel, from the Taskbar, click
Start -> (All) Programs -> Microsoft Office -> Microsoft Office Excel
The Office Button
Introduction
When Microsoft Excel opens, it displays an interface divided in various sections. The top section
displays the title bar which starts on the left side with the Office Button . If you position the
1
http://hhctrl.textpopup%28action%2Cpopfont%2C9%2C9%2C-1%2C-1%29/http://hhctrl.textpopup%28action%2Cpopfont%2C9%2C9%2C-1%2C-1%29/http://hhctrl.textpopup%28action%2Cpopfont%2C9%2C9%2C-1%2C-1%29/http://hhctrl.textpopup%28action%2Cpopfont%2C9%2C9%2C-1%2C-1%29/ -
7/31/2019 Lec 01 - Introduction to Microsoft Excel and VBA
2/30
mouse on it, a tool tip would appear:
The Options of the Office ButtonWhen clicked, the Office Button displays a menu:
2
http://www.functionx.com/vbaexcel/fundamentals/Lesson1.htmhttp://www.functionx.com/vbaexcel/fundamentals/Lesson1.htm -
7/31/2019 Lec 01 - Introduction to Microsoft Excel and VBA
3/30
As you can see, the menu of the Office Button allows you to perform the routine Windowsoperations of a regular application, including creating a new document, opening an existing file, or
saving a document, etc. If you right-click the office button, you would get a short menu:
We will come back to the options on this menu.
3
-
7/31/2019 Lec 01 - Introduction to Microsoft Excel and VBA
4/30
The Quick Access Toolbar
The Quick Access Toolbar is on the right side of the Office Button. It displays afew buttons. If you right-click the Quick Access toolbar, a menu would appear:
To hide the Quick Access toolbar, you can right-click it and click Remove Quick Access Toolbar. Ifyou position the mouse on a button, a tool tip would appear.
In the beginning, the Quick Access toolbar displays only three buttons: Save, Undo, and Redo. If
you want more buttons than that, you can right-click the Quick Access toolbar and click CustomizeQuick Access Toolbar... This would display the Excel Options dialog box:
4
http://www.functionx.com/vbaexcel/fundamentals/Lesson1.htmhttp://www.functionx.com/vbaexcel/fundamentals/Lesson1.htm -
7/31/2019 Lec 01 - Introduction to Microsoft Excel and VBA
5/30
To add a button to the Quick Access toolbar, on the left list of Add, click an option and click Add.After making the selections, click OK.
To remove a button from the Quick Access toolbar, right-click it on the Quick Access toolbar and
click Remove From Quick Access Toolbar.
The Quick Access Button
A button with a down-pointing arrow displays on the right side of the Quick Access toolbar. You can
click or right-click that button to display its menu:
5
http://www.functionx.com/excel/dlgboxes/options2.gif -
7/31/2019 Lec 01 - Introduction to Microsoft Excel and VBA
6/30
-
7/31/2019 Lec 01 - Introduction to Microsoft Excel and VBA
7/30
On the right side of the title bar, there are three system buttons that allow you to minimize,maximize, restore, or close Microsoft Access.
Under the title bar, there is another bar with a Help button on the right side.
The Ribbon
Introduction
Under the title bar, Microsoft Excel displays the Ribbon:
By default, the Ribbon displays completely in the top section of Microsoft Excel under the title bar.
One option is to show it the way the main menu appeared in previous versions of Microsoft Excel.To do this:
Right-click the Office Button, the Quick Access toolbar, or the Ribbon itself, and click Minimize
the Ribbon
Click or right-click the button on the right side of the Quick Access toolbar:
7
http://www.functionx.com/vbaexcel/fundamentals/Lesson1.htmhttp://www.functionx.com/vbaexcel/fundamentals/Lesson1.htm -
7/31/2019 Lec 01 - Introduction to Microsoft Excel and VBA
8/30
This would display the Ribbon like a main menu:
To show the whole Ribbon again:
8
-
7/31/2019 Lec 01 - Introduction to Microsoft Excel and VBA
9/30
Right-click the Office Button, the Quick Access toolbar, or one of the Ribbon menu items, andclick Minimize the Ribbon to remove the check mark on it
Click or right-click the button on the right side of the Quick Access toolbar and click Minimize
the Ribbon to remove the check mark on it
Double-click one of the menu items of the Ribbon
Changing the Location of the Ribbon
By default, the Quick Access toolbar displays on the title bar and the Ribbon displays under it. Ifyou want, you can switch their locations. To do that, right-click the Office Button, the Quick Access
toolbar, or the Ribbon, and click Show Quick Access Toolbar Below the Ribbon:
To put them back to the default locations, right-click the Office Button, the Quick Access toolbar, or
the Ribbon, and click Show Quick Access Toolbar Above the Ribbon.
The Tabs of the Ribbon
The ribbon is a type of property sheet made of various property pages. Each page is representedwith a tab. To access a tab:
9
-
7/31/2019 Lec 01 - Introduction to Microsoft Excel and VBA
10/30
You can click its label or button, such as Home or Create
You can press Alt or F10. This would display the access key of each tab:
To access a tab, you can press its corresponding letter on the keyboard. For example, whenthe access keys display, if you press Home, the Home tab would display
If your mouse has a wheel, you can position the mouse anywhere on the ribbon, and role thewheel (on the mouse). If you role the wheel down, the next tab on the right side would be
selected. If you role the wheel up, the previous tab on the left side would be selected. Youcan keep rolling the wheel until the desired tab is selected
To identify each tab of the Ribbon, we will refer to them by their names.
The Sections of a Tab
Each tab of the ribbon is divided in various sections, each delimited by visible borders of verticallines on the left and right. Each section displays a title in its bottom side. In our lessons, we will
refer to each section by that title. For example, if the title displays Font, we will call that section,"The Font Section".
Some sections of the Ribbon display a button . If you see such a button, you can click it. This
would open a dialog box or a window.
The Buttons of the Ribbon
10
http://www.functionx.com/vbaexcel/fundamentals/Lesson1.htmhttp://www.functionx.com/vbaexcel/fundamentals/Lesson1.htm -
7/31/2019 Lec 01 - Introduction to Microsoft Excel and VBA
11/30
Since there are various buttons and sometimes they are unpredictable, to know what a particular
button is used for, you can position your mouse on it and a tool tip would appear:
You can also use context sensitive help in some cases to get information about an item.
You can add a button from a section of the Ribbon to the Quick Access toolbar. To do that, right-
click the button on the Ribbon and click Add to Quick Access Toolbar:
Remember that, to remove a button from the Quick Access toolbar, you can right-click it on the
Quick Access toolbar and click Remove From Quick Access Toolbar.
The More Buttons of the Ribbon
In some sections of the Ribbon, on the lower-right corner, there is a button:
That button is used to display an intermediary dialog box for some actions.
The Size of the Ribbon
When Microsoft Excel is occupying a big area or the whole area of the monitor, most buttons of theRibbon appear with text. Sometimes you may need to use only part of the screen. That is, you
may need to narrow the Microsoft Excel interface. If you do, some of the buttons may display partof their appearance and some would display only an icon. Consider the difference in the following
three screenshots:
11
-
7/31/2019 Lec 01 - Introduction to Microsoft Excel and VBA
12/30
In this case, when you need to access an object, you can still click it or click its arrow. If the itemis supposed to have many objects, a new window may appear and display those objects:
From this:
To this:
Under the Ribbon, there is a white box displaying a name like A1 (it may not display A1...), that small
box is called the Name Box:
The Insert Function Button
On the right side of the Name box, there is a gray box with an fx button. That fx button is called theInsert Function button.
12
The Name Box
The Work Area
-
7/31/2019 Lec 01 - Introduction to Microsoft Excel and VBA
13/30
The Formula Bar
On the right side of the Insert Function button is a long empty white box or section called the Formula
Bar:
You can hide or show the Formula Bar anytime. To do this, on the Ribbon, click View. In theShow/Hide section:
To hide the Formula Bar, remove the check mark on the Formula Bar check box
To show the Formula Bar, check the Formula Bar check box
The Column Headers
Under the Name Box and the Formula bar, you see the column headers. The columns are labeled A, B,
C, etc:
There are 255 of columns.
The Row Headers
On the left side of the main window, there are small boxes called row headers. Each row header islabeled with a number, starting at 1 on top, then 2, and so on:
The Cells
The main area of Microsoft Excel is made of cells. A cell is the intersection of a column and a row:
13
-
7/31/2019 Lec 01 - Introduction to Microsoft Excel and VBA
14/30
A cell is identified by its name and every cell has a name. By default, Microsoft Excel appends the
name of a row to the name of a column to identify a cell. Therefore, the top-left cell is named A1. Youcan check the name of the cell in the Name Box.
The Scroll Bars
On the right side of the cells area, there is a vertical scroll bar that allows you to scroll up and down in
case your document cannot display everything at a time:
14
-
7/31/2019 Lec 01 - Introduction to Microsoft Excel and VBA
15/30
In the lower right section of the main window, there is a horizontal scroll bar that allows you to scrollleft and right if your worksheet has more items than can be displayed all at once:
Sometimes the horizontal scroll bar will appear too long or too narrow for you. If you want, you can
narrow or enlarge it. To do this, click and drag the button on the left side of the horizontal scroll bar:
15
-
7/31/2019 Lec 01 - Introduction to Microsoft Excel and VBA
16/30
The Sheet Tabs
On the left side of the horizontal scrollbar, there are the worksheet tabs:
By default, Microsoft Excel provides three worksheets to start with. You can work with any of them andswitch to another at any time by clicking its tab.
The Navigation Buttons
On the left side of the worksheet tabs, there are four navigation buttons:
If you happen to use a lot of worksheets or the worksheet names are using too much space, which
would result in some worksheets being hidden under the horizontal scroll bar, you can use the
navigation buttons to move from one worksheet to another.
The Status Bar
16
-
7/31/2019 Lec 01 - Introduction to Microsoft Excel and VBA
17/30
Under the navigation buttons and the worksheet tabs, the Status Bar provides a lot of informationabout the job that is going on.
Microsoft Excel File Operations
Saving a File
A Microsoft Excel file gets saved like any traditional Windows file. To save a file:
You can press Ctrl + S
On the Quick Access Toolbar, you can click the Save button
You can click the Office Button and click Save
Two issues are important. Whenever you decide to save a file for the first time, you need to provide a
file name and a location. The file name helps the computer identify that particular file and register it.
A file name can consist of up to 255 characters, you can include spaces and dashes in a name.
Although there are many characters you can use in a name (such as exclamation points, etc), try toavoid fancy names. Give your file a name that is easily recognizable, a little explicit. For example such
names as Time Sheets, Employee's Time Sheets, GlobalEX First Invoice are explicit enough. Like anyfile of the Microsoft Windows operating systems, a Microsoft Excel file has an extension, which is .xls
but you don't have to type it in the name.
The second important piece of information you should pay attention to when saving your file is the
location. The location is the drive and/or the folder where the file will be saved. By default, MicrosoftExcel saves its files in the My Documents folder. You can change that in the Save As dialog box. Just
click the arrow of the Save In combo box and select the folder you want.
Microsoft Excel allows you to save its files in a type of your choice. To save a file in another format:
Press F12 or Shift + F12
17
-
7/31/2019 Lec 01 - Introduction to Microsoft Excel and VBA
18/30
You can click the Office Button and position the mouse on Save As and select the desired option:
On the Quick Access Toolbar, you can click the Save button . Then, in the Save As dialog box,
click the arrow of the Save As Type combo box and select a format of your choice
There are other things you can do in the Save As dialog box:
18
-
7/31/2019 Lec 01 - Introduction to Microsoft Excel and VBA
19/30
Saving under a Different Name and New Folder
You can save a file under a different name or in another location, this gives you the ability to work on
a copy of the file while the original is intact.
There are two primary techniques you can use to get a file in two names or the same file in two
locations. When the file is not being used by any application, in Windows Explorer (or in My Computer,or in My Network Places, locate the file, right-click it and choose Copy. To save the file in a different
name, right-click in the same folder and choose Paste. The new file will be named Copy Of... You cankeep that name or rename the new file with a different name (recommended). To save the file in a
different location, right-click in the appropriate folder and click Paste; in this case, the file will keep itsname.
In Microsoft Excel, you can use the Save As dialog box to save a file in a different name or save the
file with the same name (or a different name) in another folder. The Save As dialog box also allows
you to create a new folder while you are saving your file (you can even use this technique to create a
19
-
7/31/2019 Lec 01 - Introduction to Microsoft Excel and VBA
20/30
folder from the application even if you are not saving it; all you have to do is create the folder, click
OK to register the folder, and click Cancel on the Save As dialog box).
Opening a File
The files you use could be created by you or someone else. They could be residing on your computer,
on another medium, or on a network. Once one of them is accessible, you can open it in yourapplication.
You can open a document either by double-clicking its icon in Windows Explorer, in My Computer, fromthe Find Files Or Folders window, in My Network Places, or by locating it in the Open dialog box. To
access the open dialog box, on the main menu, click File -> Open... You can also click the Open buttonon the Standard toolbar.
A shortcut to call the Open dialog box is Ctrl + O.
Files Properties
Every file has some characteristics, attributes, and features that make it unique; these are its
properties. You can access a file's properties from three main areas on the computer:
If the file is saved on the desktop and/or it has a shortcut on the desktop, if you open My
Computer, Windows Explorer, or the folder (as a window) where the file is stored, right-click the
file and click Properties. If the file were saved on the desktop, you would see only some of itsproperties, the most you can do there is to assign a Read-Only attribute. In My Computer and
Windows Explorer, you will be able to change the file's properties.Before opening a file or while in the Open dialog box, you can view some of the file's properties
although you won't be able to change them.
When the file is opened in Microsoft Excel, you can click the Office Button, position the mouse onPrepare, and click Properties. This would display some of the most common attributes of the file:
To change an item, you can click its text box and edit or replace the content. To get moreoptions, you can click the Document Properties button and click Advanced Properties...
A file's properties are used for various reasons. For example, you can find out how much size the file is
using, where it is located (the hosting drive and/or folder), who created the file, or who was the lastperson to access or modify it. The Properties dialog box is also a good place to leave messages to
other users of the same file, about anything, whether you work as a team or you simply want to makeyourself and other people aware of a particular issue regarding the file.
Microsoft Visual Basic Fundamentals
Introduction
Microsoft Excel is a spreadsheet application that provides simple to advanced means of creating andmanaging any type of list. To enhance it beyond its default function, it ships with a language called
Microsoft Visual Basic or simply Visual Basic.
Microsoft Visual Basic for Applications (VBA) is a computer language based on Microsoft Visual Basic. It
allows you to write code that can automatically perform actions on a document and/or its content.When using that language, you write pieces of code, using an external environment.
Microsoft Visual Basic is a programming environment that gets automatically installed when you setupMicrosoft Excel. It stays apart because most people would not need or use it. This means that, if you
20
-
7/31/2019 Lec 01 - Introduction to Microsoft Excel and VBA
21/30
want to use the Microsoft Visual Basic programming environment that ships with Microsoft Excel, you
must ask for it, which can be easily done.
Launching Microsoft Visual Basic
In our lessons, we will learn how to use both Microsoft Excel and Microsoft Visual Basic to create and
manage spreadsheets. The Microsoft Visual Basic programming environment we will use depends onMicrosoft Excel. As a result, to use Microsoft Visual Basic, you must first open Microsoft Excel. Then, to
write code, you must open Microsoft Visual Basic. There are various ways you can do this, dependingon your intention.
To launch Microsoft Visual Basic using the default installation of Microsoft Excel and launching from amacro, on the Ribbon, you can click View. In the Macros section, click the arrow under the Macros
button and click Record a Macro:
This would display the Record Macro dialog box:
On the Record Macro dialog box, (do something or) simply click OK. Then, in the Macros section of theRibbon, click the arrow under the Macros button and click Stop Recording. Now, to open Microsoft
Visual Basic, in the Macros section of the Ribbon, click either the Macros button itself or click the arrow
under it and click View Macros:
21
-
7/31/2019 Lec 01 - Introduction to Microsoft Excel and VBA
22/30
22
-
7/31/2019 Lec 01 - Introduction to Microsoft Excel and VBA
23/30
In the combo box, select the name of the macro you created (if you have only one, it should be
selected already) and click Edit.
There is probably the best way for a programmer like you to launch Microsoft Visual Basic. Click the
Office Button and click Excel Options. In the Excel Options dialog box, click the Show Developer tab in
the Ribbon check box and click OK. The Ribbon would become equipped with a new tab:
From the Developer tab of the Ribbon, to launch Microsoft Visual Basic, click the Visual Basic button.
Practical Learning: Starting Microsoft Visual Basic
1. Click the Office Button and click Excel Options
2. In the Excel Options dialog box, click the Show Developer tab in the Ribbon check box:
23
-
7/31/2019 Lec 01 - Introduction to Microsoft Excel and VBA
24/30
3. Click OK
4. In the Code section of the Developer tab of the Ribbon, to launch Microsoft Visual Basic, click
Visual Basic:
24
-
7/31/2019 Lec 01 - Introduction to Microsoft Excel and VBA
25/30
The Microsoft Visual Basic Interface
Introduction
When it opens, like any regular Windows application, Microsoft Visual Basic displays a title bar in thetop section. Under the title bar, the application displays a menu, followed by a Standard toolbar.
25
-
7/31/2019 Lec 01 - Introduction to Microsoft Excel and VBA
26/30
To assist you with your development, Microsoft Visual Basic can display various windows.
The Project Explorer
The Project Explorer window shows a list of the code segments that are available to your worksheet. It
is usually available whenever you open Microsoft Visual Basic. It is usually positioned in the top-leftsection. If it is not present, to display it, on the main menu of Microsoft Visual Basic, you can click
View -> Project Explorer. To close it, you can click its Close button .
You can move the Project Explorer to another section of the interface. To do this, click its title bar and
drag it away it from there:
To put the window back where it was previously, you can double-click its title bar.
The Project Explorer
The Properties window is usually positioned in the bottom-left section of the screen. When it does notappear, to display it, on the main menu, click View -> Properties Window:
26
-
7/31/2019 Lec 01 - Introduction to Microsoft Excel and VBA
27/30
The Properties Window shows the characteristics of an object that is selected. Like any other window,
27
-
7/31/2019 Lec 01 - Introduction to Microsoft Excel and VBA
28/30
to move the Properties window from its position, drag its title bar:
The main area of Microsoft Visual Basic uses a gray background. This area is gray because, in reality,
Microsoft Visual Basic is a multiple document interface (MDI) that can be used to display variouswindows at the same time. At times, this gray area will be occupied with other windows.
Modules
28
-
7/31/2019 Lec 01 - Introduction to Microsoft Excel and VBA
29/30
A module is a blank window that resembles a piece of paper on which you write code. When you use
Microsoft Excel and work on a document, a default module is automatically allocated for it, whetheryou use it or not. You can also create a module that is independent of any worksheet.
To create a module, on the main menu of Visual Basic, you can click Insert -> Module.
Practical Learning: Creating a Module1. On the main menu of Microsoft Visual Basic, click Insert -> Module
2. Notice that a blank window with a blinking caret appears
The Immediate Window
To help you test code, Microsoft Visual Basic provides a special window called the Immediate Window.
To display it, on the main menu of Microsoft Visual Basic, you can click View -> Immediate Window.
Practical Learning: Displaying the Immediate Window
1. To display the Immediate Window, on the main menu of Visual Basic, click View -> ImmediateWindow
2. Notice that a window with an Immediate title bar appears at the bottom with a blinking caret
29
-
7/31/2019 Lec 01 - Introduction to Microsoft Excel and VBA
30/30