Download presentation
Presentation is loading. Please wait.
Published byPhillip Norris Modified over 9 years ago
1
Introduction to VBA
2
This is not Introduction to Excel We’re going to assume you have a basic level of familiarity with Excel If you don’t, or you need a review, you can get started by going through the Introduction to Excel in the first module
3
The Idea of Objects VBA is built on the idea that a workbook is a hierarchy of objects An object can contain other objects; for example, a workbook contains worksheets, and the worksheets contain cells An object can have properties, like the value in a cell or the font used in a cell An object can have methods, which are actions that affect the object.
4
Events Objects can have events: for example, opening a workbook is an event; so is clicking on a cell Our VBA programs will perform tasks when events happen. The spreadsheet or Excel form we are working with will serve as the user interface, and events like clicking a button will trigger the performance of a task.
5
Macros VBA programs are called macros. The simplest way to get started writing macros is to record one. This feature makes VBA write code based on the actions you perform while recording. We’ll go through an example to illustrate this. This is not a very good way to create macros other than really simple ones, especially if formulas are involved. It is good for getting an idea of what kind of code to write to perform a particular task
6
BUT FIRST: A Warning! Macros can be extremely dangerous! They can change files on your computer, send emails to everyone in your address book, and insert spyware; hackers love them You should NEVER run macros from an un- trusted source The default setting on the Macro Security control (in Windows) is a good one: you have to authorize macros before you can run them
7
Here’s the control for the security settings (Windows version) Macro Security control
8
The “Trust Center” Window: Get here by clicking the Macro Security control This default setting is the correct one for us You will have to check a box to enable macros to run whenever you open a workbook with macros in it. You will also have to save your workbook as a “Macro-enabled Workbook” with extension.xlsm
9
Macro Security (Mac version) Get this window by clicking Preferences under the Excel menu
10
Be sure that the “warn before opening a file that contains macros” box is checked
11
Recording a Macro (Windows) There are two macro recording buttons as shown by the arrows. You can push either one to begin recording.
12
Relative References Control The “Use Relative References” button lets you choose whether to use relative or absolute references when recording your macro. The default is to use absolute references.
13
Macro Recording on the Mac Macro recording button Relative references control Note these are on the Developer tab
14
Let’s record a super simple macro This macro is in the workbook CopyCols.xlsm posted on the website Using absolute references, we’ll copy one column to another The example is shown in Windows; doing it on the Mac is quite similar
15
The Worksheet Before We Record I used R1C1 in Row 1 Col 1 etc. so you can see where data comes from when we copy it.
16
Here’s the screen we get when we push the Record Macro Button I gave it a descriptive name. If you give it a shortcut key, make sure it doesn’t conflict with an important one Be sure to describe what it does Our usual option After you push OK, recording begins
17
You can see the Mac version has basically the same interface as the Windows version
18
In the middle of recording… Click here when done [On the Mac, just click the record button again to stop]
19
To run the macro use the Macros button (second from left in Developer ribbon) Highlight the macro you want to run and push the run button
20
Add a Button to Run the Macro (Windows) On the developer tab, under Insert, click the little triangle. The icon for a button is at the upper left under Form Controls
21
Add a Button to Run the Macro (Mac) On the Mac, the controls you can add are in the ribbon
22
Click to put the button on the worksheet Put it about where you want it, but you can always move it later The next step is to associate the button with the macro The screen on the next page will come up automatically
23
Highlight the macro name and click OK BeforeAfter
24
Click on the button to get a cursor to type a more meaningful text on it
25
The final product I had to make the button longer to hold the text I wanted
26
Let’s look at the code! Click the Macros button at the upper left of the developer tab and then click the Edit button. This will take you to the VBA Editor.
27
The VBA Window (Windows) Project Window Properties Window Code Window Let’s just look at the code for now
28
Code Explanation Sub CopyAtoD() ' ' CopyAtoD Macro ' Copy Column A to Column D ' Columns("A:A").Select Selection.Copy Columns("D:D").Select ActiveSheet.Paste End Sub A “Sub” is a working piece of code, a Macro. This is the code for our Macro CopyAtoD A line that starts with a ‘ is a comment. It is not part of the code. VBA used our description for this comment. The editor colors comments green The End Sub closes off the code for this subroutine. Sub and End Sub are key words for the editor; it colors them blue
29
The Code Body (1) Columns("A:A").Select Selection.Copy Columns("D:D").Select ActiveSheet.Paste A range of columns (which here is just one column, A) is an object. It has a method Select. This code activates the method to select that column
30
The Code Body (2) Columns("A:A").Select Selection.Copy Columns("D:D").Select ActiveSheet.Paste A selection is also an object. It has a method called Copy. This line copies the selection (column A) and puts it on the clipboard
31
The Code Body (3) Columns("A:A").Select Selection.Copy Columns("D:D").Select ActiveSheet.Paste Change the selection from Column A to Column D
32
The Code Body (4) Columns("A:A").Select Selection.Copy Columns("D:D").Select ActiveSheet.Paste Here the sheet is the object when we do a paste. This pastes the information on the clipboard to the selected place on the active worksheet
33
What Code Is The code is a series of instructions to Excel The instructions are performed in the order given in the code, so the order is VERY important The idea of coding is to write instructions to make Excel do what you want it to do. This can be much more efficient than doing some boring task over and over again.
34
Saving the Workbook: It Must be Saved as a Macro-Enabled Workbook!
35
When you reopen, click the Options button and choose Enable this Content
36
Let’s do one more macro… We’ll do the same macro, but this time with the Use Relative References option selected I’m naming this one “Copy4Back” So I will click the relative references option, click record macro, and give the macro a name. Then I will select Column D, select Column A, copy Column A to Column D, and stop recording. Then I will make a button for my new macro.
37
Here’s what it looks like
38
After Clicking Copy A toD If I select Column D and click Copy 4 Back, I get the same thing. But if I select Column E and click Copy 4 Back, I get the next picture. (I erased Column D first.)
39
Column B was Copied! … because I had relative references turned on
40
Slight problem… If you look at the code you will see I probably should have named my macro Copy3Back Relative references can do confusing things. So can copying formulas In general, the best use of macro recording is for very simple repetitive tasks, or for finding out what kind of code goes with an action (record a macro, then look at the code) Starting next time, we’ll write our own code
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.