in process stillIF Then ElseConditionally executes a group of statements, depending on the value of an expression
Syntax
All on one line:
If condition Then statements Else elsestatements
Or, you can use the block form(multi-line) syntax:
If condition Then
statements
Else
elsestatements
End If
So some examples, using the Optional ElseIf, would be
Dim Number, Digits, MyString
Number = 53 ' Initialize variable.
If Number < 10 Then
Digits = 1
ElseIf Number < 100 Then
' Condition evaluates to True so the next statement is executed.
Digits = 2
Else
Digits = 3
End If
Select CaseSelect Case allows you to check more conditions without nesting multiple If's
To use Select case, you test a variable against multiple conditions. The first one that resolves True, has its statements executed, and the testing stops there. So if A = 87
Select Case A
Case 23:
'In the case where A = 23 Do something
Case 46
'In the case where A = 46 Do something
Case Else: 'Catch all for any other number that A equals Do something else
End Select
Error CheckingErrors are going to happen, but what do we do with them when they do occur is what is important.
Private Sub CreateLayer(ByRef Lname As String, Optional Lcolor As Integer, Optional Ltype As String)
Dim objLayer As AcadLayer
On Error GoTo ErrorHandler
If DoesLayerExist(Lname) = False Then
Set objLayer = ThisDrawing.Layers.Add(Lname)
objLayer.color = Lcolor
objLayer.Linetype = Ltype
End If
GoTo Clean_Up
ErrorHandler:
Select Case Err
Case -2145320939: 'In the case where the error number is XXX
Err.Clear
MsgBox "No color provided with this layer"
Resume Next
Case -2145386493: 'In the case where the error number is XXX
Err.Clear
MsgBox "No Linetype used W/this layer"
Resume Next
Case -2145386476: 'In the case where the error number is XXX
Err.Clear
MsgBox "You tried to use a Linetype that has not been loaded"
Case Else: 'Catch all for any other number that gets thrown
Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext
End Select
Clean_Up:
Set objLayer = Nothing
Exit Sub
End Sub
Select Case is the best way to error check your code. It allows you to be specific with certain error codes, or very general with the Else case.