introduction to excel vba
DESCRIPTION
Introduction to Excel VBA. UNC Charlotte CPE/PDH Series December 17, 2009. Lesson 1 Objectives. Record a Macro Modules Examine the VBE window Project Explorer, Object Browser, Properties Sub Procedures Write a Sub procedure Examine statement code Define objects properties Edit code. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/1.jpg)
Introduction to Excel VBAIntroduction to Excel VBA
UNC CharlotteCPE/PDH Series
December 17, 2009
![Page 2: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/2.jpg)
Lesson 1 ObjectivesLesson 1 ObjectivesRecord a MacroModulesExamine the VBE window
– Project Explorer, Object Browser, PropertiesSub Procedures
– Write a Sub procedureExamine statement code
– Define objects properties– Edit code
![Page 3: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/3.jpg)
Personal Macro WorkbookPersonal Macro Workbook
A hidden workbook that is always open Stores global macros
– Save in the Personal Macro Workbook Use the View tab to unhide the Personal
Workbook
![Page 4: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/4.jpg)
Visual Basic EditorVisual Basic Editor
Project VBA Project
Properties
Sub Procedure in Module 1
Module
![Page 5: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/5.jpg)
Record a MacroRecord a MacroDisplay the Developer Tab
Click on the Macro tool
Proceed through steps to complete the macro
![Page 6: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/6.jpg)
Stop Recording ToolStop Recording ToolClick on the Stop Recording Tool
![Page 7: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/7.jpg)
Edit a MacroEdit a Macro Open the Visual Basic Editor Make the changes needed Save and Close the VBE
![Page 8: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/8.jpg)
Absolute Cell ReferencesAbsolute Cell References
![Page 9: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/9.jpg)
Relative Cell ReferencesRelative Cell References
![Page 10: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/10.jpg)
DebugDebug Open the VBE and Set a Breakpoint Start running the procedure Step through the code
![Page 11: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/11.jpg)
Debug continuedDebug continued
Open the VBE and Set a Breakpoint Start running the procedure Step through the code
Bellerephon Investment AdvisorsStock Prices
Symbol: Open: High: Low: Close: Net Chg: Pct Chg: Port Alloc: Pct Prt Chg:BPAH $ 12.53 $ 12.99 $ 9.23 $ 10.35 $ (2.18) -17% 10% -2%CHJD $ 41.38 $ 46.45 $ 42.23 $ 45.63 $ 4.25 10% 4% 0%CPDH $ 21.34 $ 25.33 $ 18.34 $ 18.60 $ (2.74) -13% 8% -1%EOWH $ 24.32 $ 25.67 $ 21.35 $ 21.45 $ (2.87) -12% 7% -1%OBAH $ 14.32 $ 16.43 $ 13.55 $ 13.63 $ (0.69) -5% 5% 0%PWHG $ 23.45 $ 30.34 $ 28.34 $ 29.53 $ 6.08 26% 5% 1%QLHZ $ 25.65 $ 29.45 $ 26.42 $ 26.95 $ 1.30 5% 5% 0%QUJG $ 20.34 $ 30.21 $ 26.42 $ 27.43 $ 7.09 35% 10% 3%SDJB $ 33.14 $ 36.43 $ 32.65 $ 32.65 $ (0.49) -1% 5% 0%SJCK $ 31.54 $ 35.65 $ 24.56 $ 26.43 $ (5.11) -16% 4% -1%SJGL $ 19.34 $ 25.46 $ 23.64 $ 24.56 $ 5.22 27% 8% 2%SLHW $ 49.03 $ 53.13 $ 42.53 $ 42.53 $ (6.50) -13% 3% 0%WKCH $ 1.56 $ 2.34 $ 1.94 $ 2.13 $ 0.57 37% 10% 4%WQOC $ 13.56 $ 15.43 $ 11.23 $ 14.56 $ 1.00 7% 5% 0%ZXLQ $ 4.05 $ 6.43 $ 3.56 $ 5.25 $ 1.20 30% 6% 2%
![Page 12: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/12.jpg)
Debug continuedDebug continued
Procedure InsertRowsCols– Correction Rows(“1:4”)
Procedure InsertTxt– Corrections A1
Procedure FmtTxt– Correction Columns(“H:J”) … Percent– Move EntireColumn.AutoFit above End Sub
![Page 13: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/13.jpg)
Adding a Tool to the Quick Adding a Tool to the Quick Access ToolbarAccess Toolbar
Click on the Customize button on the Quick Access ToolbarSelect Macros
![Page 14: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/14.jpg)
Adding a Tool to the Quick Adding a Tool to the Quick Access Toolbar continuedAccess Toolbar continued
Click on Create ReportClick Add
![Page 15: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/15.jpg)
Adding a Tool to the Quick Adding a Tool to the Quick Access Toolbar continuedAccess Toolbar continued
Click on Create ReportClick Add and OK
![Page 16: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/16.jpg)
Customize the New ToolCustomize the New Tool Click on the Customize menu Select the Macro Click on the Modify button Change the Tool image Change the Tool name
![Page 17: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/17.jpg)
Lesson 2 ObjectivesLesson 2 ObjectivesObjects
– Range– Selection– Active Sheet
Methods– Select
Properties– CurrentRegion– Sort– Name
![Page 18: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/18.jpg)
Objects, Methods, & PropertiesObjects, Methods, & Properties Objects have properties that govern their
appearance and behavior– Name of worksheet
Methods govern the action the objects perform– Protect a worksheet
Properties govern how the objects respond to a user action– Procedure is executed when the event occurs
![Page 19: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/19.jpg)
VariablesVariables
A variable is a name given to hold data.
When a procedure uses a variable the current ‘value’ is used
![Page 20: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/20.jpg)
Option Explicit and Dim StatementsOption Explicit and Dim Statements
Using Option Explicit in a module helps maintain the integrity of each variable.
Dim statements are a way to define variables in a procedure
Ex. Dim NumberOfEmployees As Integer
![Page 21: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/21.jpg)
LoopsLoops
For Next LoopsIf ThenIf Then ElseCaseDo Loops
– Do While– Do Until
![Page 22: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/22.jpg)
For NextFor Next
Sub ForNext Loop ()For Counter = 1 to 10 [Step 1]
Code is written hereNext Counter – continues to collect iterations as a countEnd Sub
![Page 23: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/23.jpg)
If…Then SyntaxIf…Then Syntax
![Page 24: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/24.jpg)
If…Then…Else SyntaxIf…Then…Else Syntax
![Page 25: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/25.jpg)
Use a Do Until…LoopUse a Do Until…Loop
Sub DoUntilLoop ()Do Until ActiveCell = “”
Code is written here
LoopEnd Sub
![Page 26: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/26.jpg)
Case Is… SyntaxCase Is… Syntax
![Page 27: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/27.jpg)
Find first blank row at the bottom Find first blank row at the bottom of a listof a listOpen the file Our CompanyCreate a Sub procedure named
Database
Sub GoToBottom()
‘ Goes to the first blank row in an Excel list
ActiveSheet.Cells(Rows.Count,1).End(xlUp).Offset(1).Select
End Sub
![Page 28: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/28.jpg)
Offset and Count PropertiesOffset and Count PropertiesActiveCell.Offset(3,2)
(row,column)
Rows.CountThe number of active rows in a worksheet
Cells(Rows.Count,1) 1st column of the last row of the list
End(xlUp.Offset(1)The cell below .. The first blank row in a list
![Page 29: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/29.jpg)
Lesson 3 ObjectivesLesson 3 Objectives
Dialog boxInput boxMessage boxCommand buttons
![Page 30: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/30.jpg)
Dialog box and User InputDialog box and User Input
![Page 31: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/31.jpg)
SortBy ProcedureSortBy Procedure
![Page 32: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/32.jpg)
DateTime ProcedureDateTime Procedure
RepSort ProcedureRepSort Procedure
![Page 33: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/33.jpg)
Lesson 4 ObjectivesLesson 4 Objectives
Insert, Copy and Delete WorksheetsRename worksheetsChange worksheet orderPrint worksheets
![Page 34: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/34.jpg)
More MethodsMore Methods
Add Method– Worksheets.Add
Delete Method– Worksheets(2).Delete
Copy Method– Worksheets(2).Copy After:=Worksheets(2)
![Page 35: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/35.jpg)
Name PropertyName Property
![Page 36: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/36.jpg)
More MethodsMore Methods
PrintPreview– Worksheets(2).PrintPreview
PrintOut– Worksheets(2).Printout– Worksheets(“SE Sales”).PrintOut
![Page 37: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/37.jpg)
Unit 5 ObjectivesUnit 5 ObjectivesCreate a User Defined Function
(UDF)– Declare a variable– Perform a calculation
![Page 38: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/38.jpg)
User Defined FunctionsUser Defined Functions Begin with the keyword Function
– The first line of code includes the function name as well as any arguments
– Arguments are enclosed in parentheses and separated by commas
– The code between the first and last lines perform the mathematical calculation
– The last line of code must return the calculated answer to the name of the function
End with the keywords End Function
![Page 39: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/39.jpg)
Function Commission(Sales)Function Commission(Sales)Function Commission(Sales)If Sales >= 50000 Thenpct = 0.15ElseIf Sales >= 40000 Thenpct = 0.12ElseIf Sales >= 25000 Thenpct = 0.1ElseIf Sales >= 10000 Thenpct = 0.08Elsepct = 0.05End If Commission = Sales * pctEnd Function
![Page 40: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/40.jpg)
PriceLevel FunctionPriceLevel Function
![Page 41: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/41.jpg)
Automate Sum FunctionsAutomate Sum Functions
![Page 42: Introduction to Excel VBA](https://reader033.vdocuments.site/reader033/viewer/2022061500/56816865550346895ddebf8b/html5/thumbnails/42.jpg)
Wishing you a Wonderful Holiday Wishing you a Wonderful Holiday SeasonSeason