Help a Beginner Write a Sub Routine or Function

Help a Beginner Write a Sub Routine or Function

jfenter
Enthusiast Enthusiast
399 Views
3 Replies
Message 1 of 4

Help a Beginner Write a Sub Routine or Function

jfenter
Enthusiast
Enthusiast

I have a set of code that more or less repeats itself and I wonder if a simple set of code could be used to get the appropriate results.  I am creating a start part that will have manufacturing limits embedded in the part to prevent the engineer from creating parts outside of these maximums and minimums.

 

So the control variables are:

 

minFlange = thick

maxFlange = 80

minWidth = 150

maxWidth =1500

minLength = 288

maxLength = 1806

 

The parameters I am checking against these maximums and minimums are:

 

left1

left2

left3

right1

right2

right3

width

length

 

The basic code that I am writing to catch these variables is:

 

If left1 < minFlange Then
        MsgBox ("Oops!  That's too small!", "Error Message")
        left1 = minFlange
ElseIf left1 > maxFlange Then
        MsgBox ("Oops!  That's too big!", "Error Message")
        left1 = maxFlange    
End If  

In the code above, I want to check each of the 8 parameters against the minimums and maximums.  Since it clearly looks like the structure of the code will be the same other than the changing variables, I assume a basic routine or function can be written with the different parameters plugged in to get a result for each.  However, I have no idea how to write this.  Is it possible?  How is it done?  I use this basic max/min limit in at least 8 different start parts for more than a dozen variables and it causes my code to be clunky and cumbersome to debug.  Any help at all would be appreciated.

0 Likes
Accepted solutions (1)
400 Views
3 Replies
Replies (3)
Message 2 of 4

Brett.G
Enthusiast
Enthusiast

Something like this maybe:

 

Public Sub test()
    
    Dim minFlange As Long
    minFlange = 100
    
    'value comes from your program
    Dim left1 As Long
        
    'check if left1 is within flange limits
    If WithinLimits(minFlange, maxFlange, left1) Then
        'everything is good!
    Else
        MsgBox ("Oops! value is out of range, try again")
    End If
        
End Sub


Public Function WithinLimits(MinLimit As Long, MaxLimit As Long, value As Long) As Boolean
    'function to determine if value is between MinLimit and MaxLimit
    
    If (value >= MinLimit) And (value <= MaxLimit) Then
        WithinLimits = True
    Else
        WithinLimits = False
    End If
    
End Function
0 Likes
Message 3 of 4

clutsa
Collaborator
Collaborator
Accepted solution

This is the iLogic version of @Brett.G 's code but I return a double so you can maintain the auto-reset to the min or max value. Note: All my limit values are set in UserParameters but you could code the limits in like Brett.G so that it is harder to overwrite the limit. 

Sub Main()
	left1 = WithinLimit(MinFlange,MaxFlange,left1)
	'so on and so forth with your other values
End Sub
Function WithinLimit(minValue As Double, maxValue As Double, checkValue As Double) As Double
	If checkValue < minValue Then 
		MessageBox.Show("value was to low", "Title")
		WithinLimit = minValue
	ElseIf checkValue > maxValue Then
		MessageBox.Show("value was to high", "Title")
		WithinLimit = maxValue
	Else
		WithinLimit = checkValue
	End If
End Function 
If I've helped you, please help me by supporting this idea.
Mass Override for Each Model State

Custom Glyph Icon for iMates

Message 4 of 4

jfenter
Enthusiast
Enthusiast

That is exactly what I was looking for!  Thank you!

0 Likes