= | equal to |
> | greater than |
>= | greater than or equal to |
< | less than |
>= | less than or equal to |
<> | not equal to |
Remember that text must be placed in quotes; text not in quotes is considered a variable.
Examples:
x = "Smith" | true if the value of variable x is "Smith" (note that text comparison is case-sensitive) |
x > 15 | true if the value of variable x is greater than 15 |
ActiveCell.RowHeight > 15 | true if the height of the row where the active cell is located is greater than 15 |
y < 20 | true if the value of variable y is less than 20 |
x >= ActiveCell.Row | true if the value of variable x is greater than or equal to the row number of the active cell |
y <= x | true if the value of variable y is less than or equal to the value of variable x |
x <> 50 | true if the value of variable x is anything other than 50 |
Len(x) <> 10 | true if the length of the string in variable x is anything other than 10 |
You can use more than one expression in a condition with the operators And and Or. And means both of the expressions are true, e.g., x = 5 And y = 9 is true if x = 5 and y = 9. Or means either of the conditions is true, e.g., x = 5 Or y = 9 is true if either x = 5 or y = 9. For complex expressions, you can use parentheses to ensure that the expression is evaluated correctly, e.g., x = 5 And (y = 3 Or y = 9).
You can construct the negative of a condition with the Not operator; Not x = 5 is true if x is not equal to 5.
When a set of macro commands is to be executed only when a condition is true, the If...Then...End If statement is used. If some commands are to be executed if a condition is true, and another set executed if the condition is false, then the If...Then...Else...End If statement is used. It is good programming practice to indent the commands within the If statement.
For example, to insert "THE END" after the last row of a worksheet and bold that cell, you could use the following code to test if the cell in the first column of the next row is blank:
vRow = ActiveCell.Row
If Cells(vRow + 1, 1).Value = "" ThenCells(vRow + 1, 1).Value = "THE END"End If
Cells(vRow + 1), 1).Font.Bold = True
To do something else if the active cell is not the last row in the spreadsheet:
vRow = ActiveCell.RowThe condition being tested must evaluate to true or false. If for some reason the macro cannot evaluate the condition, you will get an error.
If Cells(vRow + 1, 1).Value = "" ThenCells(vRow + 1, 1).Value = "THE END"Else
Cells(vRow + 1), 1).Font.Bold = Truedo something elseEnd If
If there is just one command to execute, and no Else block, you can put the whole thing on one line, and omit the concluding End If.
If ActiveCell.Value < 100 Then ActiveCell.EntireRow.Delete
You can nest If statements inside one another; just be sure to include an End If statement to conclude each If statement. Indenting for each level will help you keep track.
Select Case expression testedEnd Select The Case Else is optional; its code will be executed if the expression tested does not match any of the other values.Case possible value of the expression testedCase Elsemacro codeCase possible value of the expression testedmacro codeCase possible value of the expression testedmacro codemacro code
As an example, let us say we want to check the value in the cell in column "C", and if it is 1, bold the cell in column "D"; if it is 2, italicize the cell in column "E"; and if it is "none", delete the text in column "F". We could do that with the following code:
x = Cells(2, "C").Value
Select Case xCase 1End SelectCells(2, "D").Font.Bold = TrueCase 2Cells(2, "E").Font.Italic = TrueCase "none"Cells(2, "F").Value = ""
That code works fine for row 2. But we would probably want to have a more robust version that could process all the rows, rather than just one. We will learn how to do that later in this tutorial with loops.
For example, the following code checks if there is no cell that contains "hello"; if so then it sounds a beep, otherwise it activates the first cell that it finds that contains "hello".
Set x = Cells.Find(What:="hello", After:=Range("A1"), LookIn:=xlValues, _We can code this slightly differently, by first checking if there there is any cell containing "hello"; if there is the macro activates the first cell it finds, otherwise it sounds a beep.
LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, _
SearchFormat:=False)
If x Is Nothing Then
Beep
Else
x.Activate
End If
Set x = Cells.Find(What:="hello", After:=Range("A1"), LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, _
SearchFormat:=False)
If Not x Is Nothing Then
x.Activate
Else
Beep
End If