46
12. Visual Basic If Statements and Do Loops

12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm

Embed Size (px)

Citation preview

Page 1: 12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm

12. Visual BasicIf Statements and

Do Loops

Page 2: 12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm

Open 12b-datastart.xlsm

Page 3: 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 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. Open 12b-datastart.xlsm

Select Sheet3 and cell D2

Page 5: 12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm

Click Record Macro

Page 6: 12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm

Type Macro name: HighlightCellsShortcut key: Ctrl+j

Page 7: 12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm

Click OK

Page 8: 12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm

Right-click with mouse and select Format Cells

Page 9: 12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm

Select Bold Font and click OK

Page 10: 12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm

Click Stop Recording

Page 11: 12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm

Click Visual Basic

Page 12: 12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm

Select Module 2

Page 13: 12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm

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. Open 12b-datastart.xlsm

Just keep the following lines

Page 15: 12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm

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. Open 12b-datastart.xlsm

If ActiveCell.Value > 10 Then …Endif

Page 17: 12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm

Save and Close

Page 18: 12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm

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

Page 19: 12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm

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. Open 12b-datastart.xlsm

Click Visual Basic

Page 21: 12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm

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. Open 12b-datastart.xlsm

ElseIf ActiveCell.Value < 10 Then

Page 23: 12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm

Repeat FontStyle code, but change it to Italic

Page 24: 12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm

Save and Close

Page 25: 12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm

Click Cell E2 and press Ctrl + j to run macro

Page 26: 12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm

Font should become Italic

Page 27: 12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm

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. Open 12b-datastart.xlsm

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. Open 12b-datastart.xlsm

Click Visual Basic

Page 30: 12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm

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

Page 31: 12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm

Type ActiveCell.Offset(0,1).Select

Page 32: 12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm

Click Save and Close

Page 33: 12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm

Select cell C4 and press Ctrl+j to run macro

Page 34: 12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm

Excel evaluates the cell and moves to the next column

Page 35: 12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm

Click Visual Basic

Page 36: 12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm

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

Page 37: 12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm

Type Do before the If statement

Page 38: 12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm

Type Loop Until ActiveCell.Value = “”

Page 39: 12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm

Save and Close

Page 40: 12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm

Select cell C5 and press Ctrl+j to run macro

Page 41: 12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm

Excel repeats the command and evaluates each cell within the row

Page 42: 12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm

Excel stops running the command when it finds an empty cell

Page 43: 12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm

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. Open 12b-datastart.xlsm

You now have a Do Loop within a Do Loop

Page 45: 12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm

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. Open 12b-datastart.xlsm

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