VISUAL BASIC FOR EXCEL COMMAND LIST CODE
If you have more than one action to perform, you will want to break your code into multiple lines like the following example. If you only have a single action to perform, you can leave all the code on a single line and you do not have to close the statement with an END IF. If Range("B9").Value > 0 Then Range("C9").Value = Range("B9").Value In the Code window, click between the Sub and End Sub commands and enter the following. If the value is >0, we will display the value of cell B9 in cell C9. We want to evaluate the contents of cell B9 to determine if the value is greater than 0 (zero). In the Code window (right panel) type the following and press ENTER. Right-click “This Workbook” in the Project Explorer (upper-left of VBA Editor) and select Insert ⇒ Module. In Excel, open the VBA Editor by pressing F-11 (or press the Visual Basic button on the Developer ribbon.) Once we have the logic correct, we will apply the logic to a range of cells using a looping structure. In this example we will evaluate a single cell.
![visual basic for excel command list visual basic for excel command list](https://image.slidesharecdn.com/autocadshortcuts-160330153900/95/autocad-shortcut-list-1-638.jpg)
Similarly, you can ask several questions and if any single or multiple of questions are true, the action will be performed. Like the AND function, you can ask several questions and all the questions must evaluate to TRUE to perform the action.
![visual basic for excel command list visual basic for excel command list](https://agilemercurial.files.wordpress.com/2018/07/image13.png)
The IF…THEN can also evaluate many conditions. Another test could be to test the value of a cell, such as “Is the cell value greater than 100?” If so, display the message “Great sale!” Otherwise, display the message “Better luck next time.” You give the IF a condition to test, such as “Is the customer a “preferred” customer?” If the customer is classified as “preferred” then calculate a discount amount. The IF…THEN statement is like the IF function in Excel. The IF…THEN statement allows you to build logical thinking inside your macro.
![visual basic for excel command list visual basic for excel command list](https://flylib.com/books/2/54/1/html/2/images/figure21_18.jpg)
The IF…THEN statement is one of the most commonly used and most useful statements in VBA.