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 Make the font bold ElseIf cell has a value less than 10 Then Make the font italic EndIf Begin with Macro Recorder and then edit
Select Sheet3 and cell D2
Click Record Macro
TypeMacro name: HighlightCells Shortcut key: Ctrl+j
Click OK
Right-click with mouse and select Format Cells
Select Bold Font and click OK
Click Stop Recording
Click Visual Basic
Select Module 2
Excel produces code for all aspects of the font We are only concerned with.FontStyle = “Bold”
Just keep the following lines
We only want Excel to do this when the cell value is greater than 10
If ActiveCell.Value > 10 Then … Endif
Save and Close
Click on cell D3 and press Ctrl+j to run macro
Excel should evaluate the cell and give it a bold font as it has a value above 10
Click Visual Basic
Want to add another condition that if value is less than 10 give it Italic font
ElseIf ActiveCell.Value < 10 Then
Repeat FontStyle code, but change it to Italic
Save and Close
Click Cell E2 and press Ctrl + j to run macro
Font should become Italic
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
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
Click Visual Basic
After the If statement we want to select the cell in the next column
Type ActiveCell.Offset(0,1).Select
Click Save and Close
Select cell C4 and press Ctrl+j to run macro
Excel evaluates the cell and moves to the next column
Click Visual Basic
We want Excel to Do this repeatedly until the next cell is empty
Type Do before the If statement
Type Loop Until ActiveCell.Value = “”
Save and Close
Select cell C5 and press Ctrl+j to run macro
Excel repeats the command and evaluates each cell within the row
Excel stops running the command when it finds an empty cell
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
You now have a Do Loop within a Do Loop
Once the first Do Loop is finished it moves to the next row and keeps going until the next row is empty
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 ( and refer back to them when you have a particular task to performwww.qubexcel.co.uk The only way to get really confident with Excel and VBA is to use them regularly