Introduction to VBA. This is not Introduction to Excel We’re going to assume you have a basic...
-
Upload
phillip-norris -
Category
Documents
-
view
218 -
download
0
Transcript of Introduction to VBA. This is not Introduction to Excel We’re going to assume you have a basic...
![Page 1: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/1.jpg)
Introduction to VBA
![Page 2: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/2.jpg)
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
![Page 3: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/3.jpg)
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.
![Page 4: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/4.jpg)
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.
![Page 5: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/5.jpg)
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
![Page 6: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/6.jpg)
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
![Page 7: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/7.jpg)
Here’s the control for the security settings (Windows version)
Macro Security control
![Page 8: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/8.jpg)
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
![Page 9: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/9.jpg)
Macro Security (Mac version)
Get this window by clicking Preferencesunder the Excel menu
![Page 10: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/10.jpg)
Be sure that the “warn before opening a filethat contains macros” box is checked
![Page 11: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/11.jpg)
Recording a Macro (Windows)
There are two macro recording buttons as shown by the arrows. You can push either one to begin recording.
![Page 12: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/12.jpg)
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.
![Page 13: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/13.jpg)
Macro Recording on the Mac
Macro recording button
Relative references control
Note these are on the Developer tab
![Page 14: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/14.jpg)
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
![Page 15: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/15.jpg)
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.
![Page 16: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/16.jpg)
Here’s the screen we get when we push the Record Macro Button
I gave it a descriptivename. If you give it a shortcut key,
make sure it doesn’t conflict with an important one
Be sure todescribe whatit does
Our usualoption
After you push OK, recording begins
![Page 17: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/17.jpg)
You can see the Mac version has basically the same interface as the Windows version
![Page 18: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/18.jpg)
In the middle of recording…
Click herewhen done
[On the Mac, just click the record button againto stop]
![Page 19: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/19.jpg)
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
![Page 20: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/20.jpg)
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
![Page 21: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/21.jpg)
Add a Button to Run the Macro (Mac)
On the Mac, the controls you can addare in the ribbon
![Page 22: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/22.jpg)
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
![Page 23: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/23.jpg)
Highlight the macro name and click OK
Before After
![Page 24: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/24.jpg)
Click on the button to get a cursor to type a more meaningful text on it
![Page 25: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/25.jpg)
The final product
I had to make the button longer to hold the text I wanted
![Page 26: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/26.jpg)
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.
![Page 27: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/27.jpg)
The VBA Window (Windows)
ProjectWindow
PropertiesWindow
Code Window
Let’s just look at the code for now
![Page 28: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/28.jpg)
Code ExplanationSub CopyAtoD()'' CopyAtoD Macro' Copy Column A to Column D'
' Columns("A:A").Select Selection.Copy Columns("D:D").Select ActiveSheet.PasteEnd 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
![Page 29: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/29.jpg)
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
![Page 30: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/30.jpg)
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
![Page 31: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/31.jpg)
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
![Page 32: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/32.jpg)
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
![Page 33: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/33.jpg)
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.
![Page 34: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/34.jpg)
Saving the Workbook: It Must be Saved as a Macro-Enabled Workbook!
![Page 35: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/35.jpg)
When you reopen, click the Options button and choose Enable this Content
![Page 36: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/36.jpg)
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.
![Page 37: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/37.jpg)
Here’s what it looks like
![Page 38: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/38.jpg)
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.)
![Page 39: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/39.jpg)
Column B was Copied!
… because I had relative references turned on
![Page 40: 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.](https://reader035.fdocuments.net/reader035/viewer/2022062516/56649dc45503460f94ab6889/html5/thumbnails/40.jpg)
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