Reliable Spreadsheet Solutions - Call Now 281-379-2000 Microsoft Excel - Visual Basic for Applications Consultant Reliable Spreadsheet Solutions - Call Now 281-379-2000 !
PROFESSIONAL SOFTWARE DEVELOPMENT
Reliable Excel VBA Solutions !
 
Microsoft Office : Excel - Visual Basic for Applications [ VBA ] Software 

Beyond Technology

Insight for Microsoft Excel POWER-Users
  Excel Solutions Letter ® FREE  excel solutions letter
Spreadsheet Power-User Tips
  Enable ease and consistency of data entry with a dropdown list of possible entries for a range  dropdown list in cell
  Designing an array formula that functions similar to a VLOOKUP function with multiple criteria and returns a text entry  mult-criteria text lookup
  How Excel workbooks become bloated and what can be done about it  spreadsheet bloat
  Spreadsheet Power-User Tips  more ...
Getting to Know Microsoft Visual Basic for Applications
  Decision Structure that conditionally executes the embedded code  if...then[...else]
  If you are new to VBA and trying to get a handle on programming, this is a MUST READ article for you!  what are variables ?
  Discover a special, more flexible variety of arrays  dynamic arrays
  Getting to Know Microsoft Visual Basic for Applications  more ...
Microsoft Excel Advanced VBA - Just for Geeks
  MUST SEE!  A named range that expands and contracts automatically based on the number of items in the source data table  dynamic named ranges
  MUST SEE! How to identify the last used row in an Excel worksheet  finding real last cell
  Creating charts that adapt as the size of the source data range varies  charting dynamic data
  Microsoft Excel Advanced VBA - Just for Geeks  more ...
Excel Spreadsheet Tools and Tutorials
  Mortgage & Auto Loan Payment Calculator » Amortization with Excel Spreadsheets  amortization guide
  Breakeven Analysis with Excel Spreadsheets Tutorial  breakeven analysis
  Permutations & Combinations with Excel Spreadsheets  combinatorics
  Standard Deviation in Excel Spreadsheets Tutorial  standard deviation
  Weighted Average in Excel Spreadsheets Tutorial  weighted average


Microsoft Excel VBA Custom Application Development
  Microsoft Excel VBA Custom Application Development  excel vba customization
  Microsoft Excel VBA Professional Development Experience  professional experience
  Microsoft Excel VBA Custom Application Development  project request form
Microsoft Excel VBA Solutions : Real-World Project Profiles
  How deep is your data?  engineering analysis
  Financial reporting can be a challenging endeavor.  financial reporting
  To anticipate customers needs is to be rewarded with their business.  sales forecasting
  Are time-hungry paperwork tasks building up on you?  automated billing
  An Engineer-in-a-Box  engineering design
  Partial List of Beyond Technology Clients - Past & Present  clients - past & present


Reliable Spreadsheet Solutions - Call Now 281-379-2000 !
  Visitors' Comments  gratuitous kudos
  Add to Favorites 
  Reliable Spreadsheet Solutions - Call Now 281-379-2000 !  e-mail contact

20831 Rosehill Church Rd
Tomball, Texas 77377


If...Then...Else Conditional Statements

Rodney POWELL
Microsoft MVP - Excel
need a developer for
your excel project ?
go here
Decision Structures in VBA test conditions and then execute code segments depending on the test's results. This includes a familiar If... statement.

There are various forms of the If... construct. A single-line syntax is often used when only one statement is to be executed upon a successful test condition. It looks like this:

If condition Then statement1 [ Else statement2 ] The example below will set the SalesTax variable to .0825 if the condition is True ( CustomerState is "TX" ); otherwise, the SalesTax variable will be set to 0 (zero).

If CustomerState="TX" Then SalesTax=.0825 Else SalesTax=0
Notice there's no End If used in the single line statement above.

The multi-line (or block) syntax will be easier to read if your decision structure has numerous conditions.

If condition Then statement1 [ ElseIf statement2 ] [ Else statement3 ] End If
Notice this example uses ElseIf to include additional conditions, and it DOES require an End If.

If CustomerState="TX" Then

SalesTax=.0825
ElseIf CustomerState="VA" Then

SalesTax=.0615
Else

SalesTax=0
End If

In this construct, the program goes through the conditions until it finds one that is True. Even if there are actually multiple True conditions, only the FIRST condition that evaluates as True will be executed. If none of the conditions are satisfied, there is an optional Else available as a catch all if you need it.

And, finally, here is one more example that shows how you can also nest conditional statements within other If... statements.

If CustomerState="TX" Then

If CustomerCity="Houston" Then

SalesTax=.0835
ElseIf CustomerCity="Dallas" Then

SalesTax=.0865
Else

SalesTax=.0825
End if

ElseIf CustomerState="VA" Then

If CustomerCity="Virginia Beach" Then

SalesTax=.0675
ElseIf CustomerCity="Vienna" Then

SalesTax=.0685
Else

SalesTax=.0615
End if
Else

SalesTax=0
End If
In an event-driven application, it's important to be able to handle user feedback and make decisions on how to branch program control based on a given condition.

Reliable Spreadsheet Solutions - Call Now 281-379-2000 !

Got a suggestion to make this VBA tip more helpful? Let me know.

Beyond Technology Custom Application Development
  home Microsoft Excel VBA Custom Application Development  professional consulting Insight for Microsoft Excel POWER-Users  developer tips Visitors Comments  visitor kudos Beyond Technology - Microsoft Solution Provider  e-mail  

Your suggestions and comments are greatly appreciated. Please keep them coming.

Mail to:   rodney@beyondtechnology.com

All terms, product designs, and company names used in this site may be trademarks or registered trademarks of their respective owners, and are hereby acknowledged. © 1996 - 2008 Beyond Technology. All rights reserved.