Lesson 1.4 – Conditionals

You need to be logged in to view this content. Please login or become a member.

<<  Prev   |   Index   |   Next  >>

Description

In this lesson we teach you how to use If – Then – Else statements to control which code is run under which conditions.

Notes

If you want to test the object type you can use the TypeOf keyword:

If TypeOf swApp Is SldWorks.SldWorks Then MsgBox "This is an ISldWorks object."

This will be particularly useful when programming with the SolidWorks API if you need to test for an object’s type.

Using Select Case to test for a condition is faster than using several If – Then statements in a row. This is because when using Select Case, the compiler jumps directly to the outcome of the evaluated expression.

Select Case is also limited to evaluating one condition, whereas with ElseIf, multiple conditions can be tested.

Lesson Files

Lesson 1.4 – select case code
Code snippets

Homework

Lesson 1.4 homework solution

2 Comments

  1. jfgauthier

    Keith,

    You’re referring in this lesson that lower case and upper case are differentiated in VBA. Is there a way to change this in particular cases?

    I have a code analysing the file path of files to verify if they are located in specific directories. For reasons I can’t explain, it happens in some cases that the file path string I obtain in VBA are in lower case, even if they are saved in a folder where it is definitely in upper case in our Windows PDM. In these cases, my macro folder validations code is sending me back a failure message even if the file is saved at the right place…

    In fact, I would like to not differentiate lower case vs upper case in that particular part of the code… Any suggestions?

    • String comparisons in VBA are case-sensitive by default. For example “Hello” does not equal “hello”. But you can make string comparisons case-insensitive by putting Option Compare Text at the top of your code window, on its own line. In that case, “Hello” will equal “hello”. Hope this helps.

Questions and Comments

You must be logged in to post a comment.