Download the best tutorial, website and reference tool on Excel |
Lesson 7: VBA for Excel Code for Statements The statements that I use more often in my VBA Excel macros are: If..Then..End If, Do...Loop, For...Next and Select Case |
If..Then...End If In plain English: if the value of the selected cell is greater than 10 then the value of the cell below is 100 if not do nothing. |
Note:
Tests on strings are
case sensitive so when you test a string of
characters and you don't know if the user will
use upper case or lower case letters, use the
function LCase function within your test and
write the strings in your code in lower case
letters: |
If..Then...End
If (multiple
tiers) In plain English: first check if the value of the selected cell is greater that 10. If it is not do nothing. If it is check if the value of the selected cell is equal to 12. If so set the value of the cell below at 100 else do nothing. |
If..Then...And...End
If In plain English: if the value of the selected cell is greater or equal to 10 and smaller than 20 the value of the cell below is 100 otherwise do nothing. |
If..Then...Or...End
If In plain English: if the value of the selected cell is equal to 10 or equal to 20 then the value of the cell below is 100 otherwise do nothing. |
If..Then...Else...End
If In plain English: if the value of the selected cell is greater than 10 then the value of the cell below is 100 else the value of the cell below is 50. |
If..Then..ElseIf...End
If In plain English: If the value of the selected cell is 1 then the value of the cell below is 10 but if the value of the selected cell is 2 then the value of the cell below is 20 but if the value of the selected cell is 3 then the value of the cell below is 30 but if the value of the selected cell is 4 then the value of the cell below is 40 but if the value of the selected cell is 5 then the value of the cell below is 50 but then if the value of the selected cell is not 1, 2, 3, 4 or 5 do nothing. |
Select Case Let's say that a variable or a cell can take 25 different values and depending on that value 25 different things should happen. You can either build a very long IF statement or use Select Case. |
Do..Loop The Do...Loop statement does pretty much the same thing as the For..Next statement but you don't need to declare a counter because the Loop stops when it encounters a certain condition. Try the following procedure by first entering 1 in cells A1 to A7 or A27 or to as far down as you want to go. Sub proTest() Range("A1").Select End Sub In plain English: starting in cell A1 add 1 to the value of the selected cell and move one cell down. Do this until the value of the selected cell is nothing. Variation on the statement: Do
Until Selection.Value = "" Do
While Selection.Value <> "" Loop
Until Selection.Value = "" Loop While Selection.Value <>"" |
Exit... You may exit a FOR..NEXT, DO...LOOP and even a procedure at any time with the EXIT statement If Selection.Value > 10 Then Exit For If Selection.Value > 10 Then Exit Do If Selection.Value > 10 Then Exit Sub |
With...End With In the old days when computer memory was rare and expensive and computers were not very powerful programmers would us a lot of With..End With statements ebcause is was less requiring on the memory and the capacities of the computer. When you develop in VBA for Excel (very small programs) memory is not really an object and our personnal computers are as powerful as the large computers of yesterday. The macro recorder uses a lot of With..End With statements but I personnaly don't. Anyway here how it works.
Range("A3").Select is the same as writing
Range("A3").Select Both work it's your choice. |
For..Next The FOR...NEXT loop is the one I use the most. It allows you to repeat an action a certain number of times. Sub proTest() Range("A1")
= 10 For
varCounter = 1 To 10 End Sub In plain English: Set the value of cell A1 to 10 then select cell A2. While the counter is going from 1 to 10 (10 times in other words) the value of the selected cell is twice the value of the cell above...move one cell down. Resulting from this procedure, Cell A1=10, A2=20, A3=40.....A11=10,240. If you'd write: Your can
also start at the bottom and go up When you use a For..Next statement
on a set of data it is interesting to count the number of rows
and have your counter move from 1 to the number of rows. |
Download the best tutorial, website and reference tool on Excel |
All rights reserved PLI Consultant Inc.