How to use the IF THEN ELSE ELSEIF END IF statement [VBA]
The picture above demonstrates an IF statement that checks if the value in B3 is smaller than the value in cell D3.
If true the If statement runs the remaining code after the Then statement, in this case, it shows a message box with text Value1 is smaller than Value2.
Sub Macro1() If Range("B3") < Range("D3") Then MsgBox "Value1 is smaller than Value2" End Sub
IF THEN END IF
The IF statement also allows you to execute multiple lines of code, the END IF statement tells the subroutine when to stop.
Sub Macro2() If Range("B3") < Range("D3") Then Range("E3") = 45 MsgBox "Value1 is smaller than Value2" End if End Sub
The subroutine above saves the number 45 to cell E3 if the value in cell B3 is smaller than the value in D3.
The msgbox function then displays Value1 is smaller than Value2.
IF THEN ELSE ENDIF
The ELSE statement allows you to execute code if the logical expression is not met.
Sub Macro3() If Range("B3") < Range("D3") Then MsgBox "Value1 is smaller than Value2" Else MsgBox "Value1 is not smaller than Value2" End If End Sub
IF THEN ELSEIF ELSE ENDIF
The ELSEIF statement lets you create another logical expression, you may have multiple ELSEIFs in the statement.
Sub Macro4() If Range("B3") < Range("D3") Then MsgBox "Value1 is smaller than Value2" ElseIf Range("B3") = Range("D3") Then MsgBox "Value1 is equal to Value2" Else MsgBox "Value1 is larger than Value2" End If End Sub
Download Excel *.xlsm macro-enabled file
This workbook contains two worksheets, one worksheet shows a calendar and the other worksheet is used to store events. The […]
Run a Macro from a Drop Down list [VBA]
This article demonstrates how to execute a VBA macro using a Drop Down list. The Drop Down list contains two […]
Show / hide a picture using a button
This article explains how to hide a specific image in Excel using a shape as a button. If the user […]
Find the most/least consecutive repeated value [VBA]
This post Find the longest/smallest consecutive sequence of a value has a few really big array formulas. Today I would like to […]
How to create a list of comments from a worksheet programmatically
Did you know that you can select all cells containing comments in the current sheet? Press F5, click "Special..." button, […]
One Response to “How to use the IF THEN ELSE ELSEIF END IF statement [VBA]”
Leave a Reply
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.
I am in need of an if/then statement that will run a macro. I created a drop down list with values. If a value is selected, I want the macro to check a checkbox on the next worksheet - which is where the drop down list values came from. I have tried a few things and none of them have worked. Any assistance would be greatly appreciated.