Presentation on theme: "Introduction to VBA. 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."— Presentation transcript:
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
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.
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.
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
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
Here’s the control for the security settings (Windows version) Macro Security control
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
Macro Security (Mac version) Get this window by clicking Preferences under the Excel menu
Be sure that the “warn before opening a file that contains macros” box is checked
Recording a Macro (Windows) There are two macro recording buttons as shown by the arrows. You can push either one to begin recording.
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.
Macro Recording on the Mac Macro recording button Relative references control Note these are on the Developer tab
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
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.
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
You can see the Mac version has basically the same interface as the Windows version
In the middle of recording… Click here when done [On the Mac, just click the record button again to stop]
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
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
Add a Button to Run the Macro (Mac) On the Mac, the controls you can add are in the ribbon
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
Highlight the macro name and click OK BeforeAfter
Click on the button to get a cursor to type a more meaningful text on it
The final product I had to make the button longer to hold the text I wanted
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.
The VBA Window (Windows) Project Window Properties Window Code Window Let’s just look at the code for now
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
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
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
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
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
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.
Saving the Workbook: It Must be Saved as a Macro-Enabled Workbook!
When you reopen, click the Options button and choose Enable this Content
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.
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.)
Column B was Copied! … because I had relative references turned on
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