Presentation is loading. Please wait.

Presentation is loading. Please wait.

VBA Programming Part One. Our Dartmouth Legacy 10 INPUT "What is your name: "; U$ 20 PRINT "Hello "; U$ 25 REM 30 INPUT "How many stars do you want: ";

Similar presentations


Presentation on theme: "VBA Programming Part One. Our Dartmouth Legacy 10 INPUT "What is your name: "; U$ 20 PRINT "Hello "; U$ 25 REM 30 INPUT "How many stars do you want: ";"— Presentation transcript:

1 VBA Programming Part One

2 Our Dartmouth Legacy 10 INPUT "What is your name: "; U$ 20 PRINT "Hello "; U$ 25 REM 30 INPUT "How many stars do you want: "; N 35 S$ = "" 40 FOR I = 1 TO N 50 S$ = S$ + "*" 55 NEXT I 60 PRINT S$ 65 REM

3 Startup Excel and hit… Alt-F11

4

5 First things first: Getting Help

6 Getting More Help

7 Project Explorer Excel objects User Forms Code Modules

8 Properties Window Properties are an object's characteristics, e.g. a sheet's name.

9 Immediate Window

10 Using the Immediate Window

11 Try the Immediate Window  MsgBox("Hello!")  x = 1 + 1  debug.Print x  'this is a comment  x = WorksheetFunction.Sum(1,2)  debug.Print x

12 Insert a module

13 Add some code Sub HelloWorld() MyName = InputBox("What is your name?") MsgBox ("Hi " + MyName) End Sub  Type "HelloWorld" in the Immediate Window to run your program.  Go back to Excel and run it from the Tools|Macro menu

14 Subroutines vs. Functions  Both are procedures: A named sequence of statements executed as a unit. (MS)  Functions return a value  Subroutines don't

15 Procedures take arguments

16 Arguments are variables

17 But what are variables?  "A named storage location that can contain data that can be modified during program execution." (MS) 1. Has name 2. Contains data 3. Can be modified

18 Convert Celsius to Fahrenheit Function Fahrenheit(Celsius) Fahrenheit = (Celsius * 1.8) + 32 End Function

19 Use the Function

20 Back to the Immediate Window x = 1 debug.Print x 1 x = 2 debug.Print x 2 'Set the value of x to 1 'Now set it to 2

21 Making Decisions

22 Decisions: If…Then Function VeryHot(Celsius) If (Celsius > 30) Then VeryHot = True Else VeryHot = False End If End Function

23 Which number is bigger? Function MyMax(x, y) If ( … ) Then … Else … End If End Function  Remember: 'FunctionName = Result'

24 Which number is bigger? (Solution) Function MyMax(x, y) If ( x > y ) Then MyMax = x Else MyMax = y End If End Function

25 Loops: For…Next For Variable = BeginValue to EndValue do something with Variable Next Sub firstLoop() For x = 1 To 10 Debug.Print x Next End Sub

26 Example: Sum of 1…N Function SumToN(N) SumToN = 0 For x = 1 To N SumToN = SumToN + x Next End Function

27 TemperatureChart  Create a sub called TemperatureChart  Use For…Next to loop Celsius from 0 to 40  Use debug.print to output the result of converting Celsius to Fahrenheit using our function

28 TemperatureChart (Solution) Sub TemperatureChart() For Celsius = 0 To 40 Debug.Print Fahrenheit(Celsius) Next End Sub

29 What we have covered so far…  The VBA environment  Inserting a module  Running a macro  Writing a user-defined function  Using the new function in Excel  Variables  If…Then…Else  For…Next


Download ppt "VBA Programming Part One. Our Dartmouth Legacy 10 INPUT "What is your name: "; U$ 20 PRINT "Hello "; U$ 25 REM 30 INPUT "How many stars do you want: ";"

Similar presentations


Ads by Google