12. visual basic if statements and do loops

46
12. Visual Basic If Statements and Do Loops

Upload: ramona

Post on 22-Mar-2016

46 views

Category:

Documents


0 download

DESCRIPTION

12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm. If statements. We have seen how to use IF statements in formulas in Excel IF statements can also be used in Visual Basic but they have a different format The basic format is: If a cell has a value greater than 10 Then - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 12. Visual  Basic If Statements and Do Loops

12. Visual BasicIf Statements and

Do Loops

Page 2: 12. Visual  Basic If Statements and Do Loops

Open 12b-datastart.xlsm

Page 3: 12. Visual  Basic If Statements and Do Loops

If statements• We have seen how to use IF statements in

formulas in Excel• IF statements can also be used in Visual Basic

but they have a different format• The basic format is:

If a cell has a value greater than 10 ThenMake the font bold

ElseIf cell has a value less than 10 ThenMake the font italic

EndIf

• Begin with Macro Recorder and then edit

Page 4: 12. Visual  Basic If Statements and Do Loops

Select Sheet3 and cell D2

Page 5: 12. Visual  Basic If Statements and Do Loops

Click Record Macro

Page 6: 12. Visual  Basic If Statements and Do Loops

Type Macro name: HighlightCellsShortcut key: Ctrl+j

Page 7: 12. Visual  Basic If Statements and Do Loops

Click OK

Page 8: 12. Visual  Basic If Statements and Do Loops

Right-click with mouse and select Format Cells

Page 9: 12. Visual  Basic If Statements and Do Loops

Select Bold Font and click OK

Page 10: 12. Visual  Basic If Statements and Do Loops

Click Stop Recording

Page 11: 12. Visual  Basic If Statements and Do Loops

Click Visual Basic

Page 12: 12. Visual  Basic If Statements and Do Loops

Select Module 2

Page 13: 12. Visual  Basic If Statements and Do Loops

Excel produces code for all aspects of the fontWe are only concerned with .FontStyle = “Bold”

Page 14: 12. Visual  Basic If Statements and Do Loops

Just keep the following lines

Page 15: 12. Visual  Basic If Statements and Do Loops

We only want Excel to do this when the cell value is greater than 10

Page 16: 12. Visual  Basic If Statements and Do Loops

If ActiveCell.Value > 10 Then …Endif

Page 17: 12. Visual  Basic If Statements and Do Loops

Save and Close

Page 18: 12. Visual  Basic If Statements and Do Loops

Click on cell D3 and press Ctrl+j to run macro

Page 19: 12. Visual  Basic If Statements and Do Loops

Excel should evaluate the cell and give it a bold font as it has a value above 10

Page 20: 12. Visual  Basic If Statements and Do Loops

Click Visual Basic

Page 21: 12. Visual  Basic If Statements and Do Loops

Want to add another condition that if value is less than 10 give it Italic font

Page 22: 12. Visual  Basic If Statements and Do Loops

ElseIf ActiveCell.Value < 10 Then

Page 23: 12. Visual  Basic If Statements and Do Loops

Repeat FontStyle code, but change it to Italic

Page 24: 12. Visual  Basic If Statements and Do Loops

Save and Close

Page 25: 12. Visual  Basic If Statements and Do Loops

Click Cell E2 and press Ctrl + j to run macro

Page 26: 12. Visual  Basic If Statements and Do Loops

Font should become Italic

Page 27: 12. Visual  Basic If Statements and Do Loops

Challenge• Edit the Visual Basic code so that there are

three conditions• If ActiveCell.Value > 20 Then– Font style should be “Bold”

• ElseIf ActiveCell.Value > 15 Then– Font style should be “Italic”

• ElseIf ActiveCell.Value < 15 Then– Font style should be “Bold Italic”

• EndIf

Page 28: 12. Visual  Basic If Statements and Do Loops

Do Loops• We can get Excel to do something repeatedly by

setting up a Do … Loop Until …• Rather than evaluating one cell at a time we

may want to work through all the cells in a row• We can tell Excel to evaluate the cell and move

to the next cell• We then get Excel to repeat this until the next

cell is blank

Page 29: 12. Visual  Basic If Statements and Do Loops

Click Visual Basic

Page 30: 12. Visual  Basic If Statements and Do Loops

After the If statement we want to select the cell in the next column

Page 31: 12. Visual  Basic If Statements and Do Loops

Type ActiveCell.Offset(0,1).Select

Page 32: 12. Visual  Basic If Statements and Do Loops

Click Save and Close

Page 33: 12. Visual  Basic If Statements and Do Loops

Select cell C4 and press Ctrl+j to run macro

Page 34: 12. Visual  Basic If Statements and Do Loops

Excel evaluates the cell and moves to the next column

Page 35: 12. Visual  Basic If Statements and Do Loops

Click Visual Basic

Page 36: 12. Visual  Basic If Statements and Do Loops

We want Excel to Do this repeatedly until the next cell is empty

Page 37: 12. Visual  Basic If Statements and Do Loops

Type Do before the If statement

Page 38: 12. Visual  Basic If Statements and Do Loops

Type Loop Until ActiveCell.Value = “”

Page 39: 12. Visual  Basic If Statements and Do Loops

Save and Close

Page 40: 12. Visual  Basic If Statements and Do Loops

Select cell C5 and press Ctrl+j to run macro

Page 41: 12. Visual  Basic If Statements and Do Loops

Excel repeats the command and evaluates each cell within the row

Page 42: 12. Visual  Basic If Statements and Do Loops

Excel stops running the command when it finds an empty cell

Page 43: 12. Visual  Basic If Statements and Do Loops

Challenge• Edit the Visual Basic code so that once the end

of the row is reached, Excel moves to the beginning of the next row

• After the Do Loop tell Excel to ActiveCell.Offset(1,-4).Select

• Then set up another Do Loop so that Excel keeps doing this until the whole table has been evaluated

Page 44: 12. Visual  Basic If Statements and Do Loops

You now have a Do Loop within a Do Loop

Page 45: 12. Visual  Basic If Statements and Do Loops

Once the first Do Loop is finished it moves to the next row and keeps going until the next row is empty

Page 46: 12. Visual  Basic If Statements and Do Loops

Advice

• Writing programs requires trial and error• Use the macro recorder to get most of the code• Then edit this code to make it do exactly what

you want• Download a copy of all these notes (

www.qubexcel.co.uk) and refer back to them when you have a particular task to perform

• The only way to get really confident with Excel and VBA is to use them regularly