Presentation is loading. Please wait.

Presentation is loading. Please wait.

Visual Basic for Applications. What it does Extends the features and built in functions of Excel – Create and run VB procedures – Some may be easy to.

Similar presentations


Presentation on theme: "Visual Basic for Applications. What it does Extends the features and built in functions of Excel – Create and run VB procedures – Some may be easy to."— Presentation transcript:

1 Visual Basic for Applications

2 What it does Extends the features and built in functions of Excel – Create and run VB procedures – Some may be easy to solve using Excel but you can see how VB works Parts How to start Syntax Commands, variables, loops

3 Parts of VBA Objects – Workbook, sheet, cell, range of cells, chart Properties – What to do with object Open, Select Methods – Action Clear, Print

4 Steps Objects & Properties and Objects & Methods are separated by a period To do anything, have to select (make active) first Add comments using REM or single apostrophe ‘ Can print the VB module using File/Print Sheets(“sheet1”).select objectproperty Sheets(“sheet1”).PrintOut objectmethod

5 How to start Developer tab and click the Visual Basic button in the Code group. Visual Basic Editor – Insert Module Visual Basic statements or commands are contained in SUB procedure and are named – Sub NameYouMakeUp() – first line – End Sub – last line Name should reflect what the module does – Sub AddCells()

6 Some Code Sub AddCells() sheets(“sheet1”).select [d2]=[b2]+[c2] End Sub Three ways to select cell location 1) Cell locations have brackets [c2] 2) By row and column Cells(2,3) =this is used for incrementing 3) With the range keyword Range(“c2”) also range(“b3:d3”).select

7 Testing and Executing In the Visual Basic Editor select run from toolbar In excel select View tab/Macros/View macros/select macro from list then run button You can create a button to run macro – Customize the Quick Access Toolbar and add the button object – Select the button object, create – Select the macro – Change the button text

8 Variables Hold information that can change – Results of a calculation – Or to make comparisons Rules – Must begin with a letter Can contain alpha characters and/or digits – There are some reserved words – Maximum length of name 255 characters

9 Variables (cont.) One variable on left of equal sign Cost = 50 Equal sign means “set” Sub Rev() Sheets(“sheet1”).Select Cost = 50 SellPrice = 90 Revenue = SellPrice – Cost End Sub – only one decimal point allowed, no commas or dollar signs Let Cost = 50 (Let is optional)

10 Text Variables and Parameter Query String data Title = “Hello World” [b7] = title Request data [b2] = InputBox(“title”,”what to enter”) [c2] = InputBox(“title”,”what to enter”) [d2] = [b2] + [c2] – If it is text then it will concatenate result not add

11 Counting and Incrementing The sum of something or how many times Add 1 to a variable – M = M + 1 To move to next column or row use cell reference – Cells(M,2) If you are running a total you will need another variable, initialize to zero

12 Decision Making To continue or what action to take If (logical expression) then (execute) True or False it will continue Decide between two actions If (logical expression) then () else ()

13 Looping Loop is terminated with IF FOR/NEXT DO/WHILE – condition is true DO/UNTIL - while condition is false

14 Other commands Sheets.Add after := sheets(“sheet4”) Sheets(“Sheet1”).Copy before := sheet2 Sheets(“sheet2”).select Activewindow.selectedsheets.Delete Range(Cells(5,3),Cells(9,3)).Value = 0 Range(Cells(13,4),Cells(22,4)).ClearContents

15 Cell Attributes Range(“b3”).font.bold=true Range(“b3”).font.name=“comic sans ms” Range(“b3”).font.italic=true OR With range(“B3”).font.size = 20.font.bold=true.font.name=“comic sans ms”.font.italic=true End With

16 More Cell Attributes Columns(“F”).select Selection.columnwidth=12 OR Columns(“B:H”).select Rows(“2”).select Two decimals Range(“b6:e15”).select Selection.NumberFormat=“0.00”

17 Summary Create and run VB procedures Parts How to start Syntax Commands, variables, decision making and loops


Download ppt "Visual Basic for Applications. What it does Extends the features and built in functions of Excel – Create and run VB procedures – Some may be easy to."

Similar presentations


Ads by Google