Thursday, March 28, 2024
Useful Resources for Brilliant Manufacturing, Operational & Industrial Intelligence Technology Enthusiasts!

Automation, Control & Plant Intelligence - Articles, Analysis, Reviews, Interviews & Views

Most Popular & Common Industrial Application Software Scripting

If-Then-Else Statements

VBA Visual Basic for Applications VBA is a powerful scripting tool that is fully integrated into many industrial SCADA and supporting applications to let you quickly and easily automate operator tasks and create automation solutions. VBA scripts can be as simple or complex as you need them to be, allowing you to add custom functionality and extend your SCADA software as needed to accommodate your automation strategy. Its powerful and easy-to-use development environment allows you to reuse existing Visual Basic code.
VBA is an implementation of Microsoft's event-driven programming language Visual Basic 6, which was declared legacy in 2008, and its associated integrated development environment (IDE). Although pre-.NET Visual Basic is no longer supported or updated by Microsoft, the VBA programming language was upgraded in 2010 with the introduction of Visual Basic for Applications 7 in Microsoft Office applications and it is still popular in industrial applications and customizations.


We can use the If-Then-Else statement to run a specific statement or a block of statements, depending on the value of a condition.  If-Then-Else statements can be nested to as many levels as you need.

TIP: However, for readability, you should use Select Case statement rather than multiple levels of nested If...Then...Else statements.



Examples: IF-THEN-ELSE Statements

 

IF-THEN-ELSE Statements : if a Condition is True

 

Use it when only one condition is True, you can use the single-line syntax of the IF-THEN-ELSE statement.

This following example shows the single-line syntax, omitting the Else:

Sub myDate()
    myDate = #1/1/99#
    If myDate < Now Then myDate = Now
End Sub
 To run more than one line of code, you must use the multiple-line syntax. 
This syntax includes the End If statement, as shown in the following example:
Sub AlertUser(value as Long)
   IF value = 0 THEN
        Msgbox “Hello Automation Guru”
        ValveOutput = 43
        Pump3 = True
    END IF
End Sub

 

IF-THEN-ELSE Statements : if a Condition is True and Running Others if It's False

 

You can use an IF-THEN-ELSE statement to define two blocks of executable statements:

one block runs if the condition is True,

the 2nd block runs if the condition is False.

 

Sub AlertUser(value as Long)
    If value = 1 Then
        Valve1Output = 43
        Pump3 = True
    Else
        SystemDown = 1
        SetAlarm21 = True
    End If
End Sub
 

 

IF-THEN-ELSE Statements : Use a Second Condition if the First Condition is False

 

You can add ElseIf statements to an IF-THEN-ELSE statement to test a second condition if the first condition is False.

For example, the following function procedure computes a damper output based on off set.

The statement following the Else statement runs if the conditions in all of the If and ElseIf statements are False.

 
Function UpdateValve(RealValue, offSet)
    
   If performance = 1 Then
        RealValue = offSet * 0.1
    ElseIf performance = 2 Then
        RealValue = offSet * 0.09
    ElseIf performance = 3 Then
        RealValue = offSet * 0.07
    Else
        RealValue = 0
    End If
End Function


Most Viewed
Top 20 - Industry Terms ....



Free OPC Clients   /   Free OPC Servers   /   Free OPC Utilities


OPC Foundation owns all OPC trademarks including the OPC logo used throughout this site. Most of information in OPC Knowledge Base is from OPC foundation.
Free Automation Stuff This information made available on Automation Media, are provided "as is" without any representation or warranty, express or implied, of any kind, including, but not limited to, warranties of merchantability, noninfringement, or fitness for any particular purpose. Automation Meida makes no warranty or representation regarding, and does not endorse, any linked Web sites or the information appearing thereon or any of the products or services described thereon.