New to VBA

New to VBA

Anonymous
Not applicable
1,085 Views
11 Replies
Message 1 of 12

New to VBA

Anonymous
Not applicable

Hi

I have some code that looks for the Min and Max in an array and then removes the Min and Max. My array has always 4 values now what i want to do is when the Min and Max are removed from the array is to use the other 2 values in mathformulas is that possible ? 

 

So lets say P1 is the Min and P4 is the Max so i have P3 and P2 and i want to use P3 and P2 in some mathformulas.

 

'create list of values
Dim MyArrayList As New ArrayList
MyArrayList.add(p1)
MyArrayList.add(p2)
MyArrayList.add(p3)
MyArrayList.add(p4)

'find mininum 
oMin = MinOfMany(p1, p2, p3, p4)
'look at each item in the list and 
'remove if it matches minimum
For Each oItem in MyArrayList 
	If oItem = oMin Then
		MyArrayList.Remove(oItem)
		Exit For
	End If
Next

'find maximum
oMax = MaxOfMany(p1, p2, p3, p4)
'look at each item in the list and 
'remove if it matches maximum
For Each oItem in MyArrayList 
	If oItem = oMax Then
		MyArrayList.Remove(oItem)
		Exit For
	End If
Next

'get user input
oInput = InputListBox("Choose one", MyArrayList, "", "iLogic", "Values")

If oInput = "" Then Return 'do nothing if nothing is selected

'deplay selected value
MessageBox.Show("You selected: " & oInput, "iLogic")

 thanks in advance

0 Likes
1,086 Views
11 Replies
Replies (11)
Message 2 of 12

Owner2229
Advisor
Advisor

Hey, first of, this isn't VBA, it's iLogic. The language used in iLogic is VB.Net.

 

Here's your rule:

 

'Create a list of values
Dim oValues(3) As Double
oValues(0) = p1
oValues(1) = p2
oValues(2) = p3
oValues(3) = p4

'Find min and max
Dim oMin As Double = 1000000000
Dim oMax As Double = -1000000000 For Each oValue As Double In oValues If oValue < oMin Then oMin = oValue
If oValue > oMax Then oMax = oValue Next
'Remove min and max For Each oValue As Double In oValues
Try If oValue = oMin Then oValue = Nothing If oValue = oMax Then oValue = Nothing Catch
End Try Next 'Find the remaining values
Dim rValues(0) As Double
For Each oValue As Double In oValues
If oValue = Nothing Then Continue For
If rValues(rValues.Length - 1) <> Nothing Then
ReDim Preserve rValues(rValues.Length)
End If
rValues(rValues.Length - 1) = oValue
Next

'Sum remaining values
Dim Result As Double = 0
For Each oValue As Double In rValues
Result += oValue
Next

MsgBox(Result)
Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 3 of 12

Anonymous
Not applicable

Thanks for correcting me.

 

But your code doesn't do what i want i gives as answer -2,0.... E-14

 

But i want the 2 points that aren't the min max so when P1 and P4 are the min max then i want to use P2 en P3 in forumula like

Asin((((P2*-1)+P3)/R))

 

0 Likes
Message 4 of 12

Owner2229
Advisor
Advisor

Wait, what? So these are points, ok... You can't calculate with points like with values. Each point has at least two values (X and Y), three if it is 3d point (X, Y, Z), how would you want to calculate that?

 

It's quite interesting it converted them to double without any issue.

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 5 of 12

Anonymous
Not applicable

the points i meant are values from reference parameters and it is with those values that i want to calculate

 

 

0 Likes
Message 6 of 12

Owner2229
Advisor
Advisor

This way you can calculate with the remaining values however you want:

 

'...
'Sum remaining values
'Calculate with remaining values
If rValues.Length < 2 Then Exit Sub

Dim R1 As Double = rValues(0)
Dim R2 As Double = rValues(1)

'Example A
Dim Result As Double = R1 + R2 MsgBox(Result)

'Example B
Dim R As Double = 10 'I don't know where you've got the R from,
'so I've declared it on my own
Result = Asin(((R1 * -1) + R2) / R)
MsgBox(Result)

 

Can you post the source data? Aka. the values of P1, P2, P3, P4

It might help.

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 7 of 12

Anonymous
Not applicable

I tried you're code and modified it a bit.

 

SyntaxEditor Code Snippet

'Create a list of values
Dim oValues(3) As Double
oValues(0) = X_Afstand_1_Ruit
oValues(1) = X_Afstand_2_Ruit
oValues(2) = X_Afstand_3_Ruit
oValues(3) = X_Afstand_4_Ruit

'Find min and max
oMin = MinOfMany(X_Afstand_1_Ruit,X_Afstand_2_Ruit,X_Afstand_3_Ruit,X_Afstand_4_Ruit)
oMax = MaxOfMany(X_Afstand_1_Ruit,X_Afstand_2_Ruit,X_Afstand_3_Ruit,X_Afstand_4_Ruit)
For Each oValue As Double In oValues
    If oValue < oMin Then oMin = oValue
    If oValue > oMax Then oMax = oValue
Next

'Remove min and max
For Each oValue As Double In oValues
    Try
        If oValue = oMin Then oValue = Nothing
        If oValue = oMax Then oValue = Nothing
    Catch
    End Try
Next

'Find the remaining values
Dim rValues(0) As Double
For Each oValue As Double In oValues
    If oValue = Nothing Then Continue For
    If rValues(rValues.Length - 1) <> Nothing Then
        ReDim Preserve rValues(rValues.Length)
    End If
    rValues(rValues.Length - 1) = oValue
Next

'Sum remaining values
Dim Result As Double = Nothing
For Each oValue As Double In rValues
    Result += oValue
Next

'MsgBox(Result)

'Calculate with remaining values
If rValues.Length < 2 Then Exit Sub

Dim R1 As Double = rValues(0)
Dim R2 As Double = rValues(1)


'Example B
Dim R As Double = 10 'I don't know where you've got the R from,
                     'so I've declared it on my own
Result = Asin(((R1 * -1) + R2) / R)
MsgBox(Result)

 you gave

Dim oMin As Double = 1000000000
Dim oMax As Double = -1000000000

 i used 

oMin = MinOfMany(X_Afstand_1_Ruit,X_Afstand_2_Ruit,X_Afstand_3_Ruit,X_Afstand_4_Ruit)
oMax = MaxOfMany(X_Afstand_1_Ruit,X_Afstand_2_Ruit,X_Afstand_3_Ruit,X_Afstand_4_Ruit)

because it has to be the Min and Max from these 4 values 

X_Afstand_1_Ruit,X_Afstand_2_Ruit,X_Afstand_3_Ruit,X_Afstand_4_Ruit

 So when X_Afstand_1 is the minimum and X_Afstand_4 is the Maximum.

 

I want to use X_Afstand_2 and X_Afstand_3

R is a predefined parameter

 

Asin(((X_Afstand_2 * -1) + X_Afstand_3) / R)

 

When i run the code now it says NaN (not a number) 

 

0 Likes
Message 8 of 12

Anonymous
Not applicable

i modiefied my code and it works until i try to use the Asin function

 

the Values are -28.5/28.5/-16.5/16.5 and A1=95

 

when i put in

Asin=((B1+H1)/A1)

I get this error : Het argument is niet opgegeven voor de parameter d van Public Shared Function Asin(d As Double) As Double

 

Translation: The argument is not specified for the parameter d of Public Shared Function Asin (d As Double) As Double

 

SyntaxEditor Code Snippet

'Create a list of values
Dim oValues(3) As Double
oValues(0) = X_Afstand_1_Ruit
oValues(1) = X_Afstand_2_Ruit
oValues(2) = X_Afstand_3_Ruit
oValues(3) = X_Afstand_4_Ruit

'Find min and max
oMin = MinOfMany(X_Afstand_1_Ruit,X_Afstand_2_Ruit,X_Afstand_3_Ruit,X_Afstand_4_Ruit)
oMax = MaxOfMany(X_Afstand_1_Ruit,X_Afstand_2_Ruit,X_Afstand_3_Ruit,X_Afstand_4_Ruit)
For Each oValue As Double In oValues
    If oValue < oMin Then oMin = oValue
    If oValue > oMax Then oMax = oValue
Next
MessageBox.Show(oMin, "Title")
MessageBox.Show(oMax, "Title")

'Remove min and max
For Each oValue As Double In oValues
    Try
        If oValue = oMin Then oValue = Nothing
        If oValue = oMax Then oValue = Nothing
    Catch
    End Try
Next

'Find the remaining values
Dim rValues(0) As Double
For Each oValue As Double In oValues
    If oValue = Nothing Then Continue For
    If rValues(rValues.Length - 1) <> Nothing Then
        ReDim Preserve rValues(rValues.Length)
    End If
    rValues(rValues.Length - 1) = oValue
Next
MessageBox.Show(rValues(0), "Title")
MessageBox.Show(rValues(1), "Title")
'Sum remaining values
Dim Result As Double = Nothing
For Each oValue As Double In rValues
    Result += oValue
Next

'MsgBox(Result)

'Calculate with remaining values'If rValues.Length < 2 Then Exit Sub

'Dim R1 As Double = rValues(0)'Dim R2 As Double = rValues(0)

'Bereken van de hoek


A1=((Binnen_diameter_Ruit/2)+Laagdikte_Ruit)
MessageBox.Show(A1, "A1")
B1=rValues(0)*-1
MessageBox.Show(B1, "B1")
H1=rValues(1)*1
MessageBox.Show(H1, "H1")
G1=B1+H1
MessageBox.Show(G1, "G1")
I1=G1/A1
MessageBox.Show(I1, "I1")
J1=Asin(I1)*PI/180
K1=J1*PI
L1=K1/180
MessageBox.Show(J1, "Title")
Asin=((B1+H1)/A1)
MessageBox.Show(Asin=((B1+H1)/A1), "Title")

 

0 Likes
Message 9 of 12

Owner2229
Advisor
Advisor

How about now? I assume you want the values to always be positive numbers, so I changed your "* -1" to "Abs()". It'll do better.

 

'Create a list of values
Dim oValues(3) As Double
oValues(0) = X_Afstand_1_Ruit
oValues(1) = X_Afstand_2_Ruit
oValues(2) = X_Afstand_3_Ruit
oValues(3) = X_Afstand_4_Ruit

'Find min and max
Dim oMin As Double = MinOfMany(X_Afstand_1_Ruit,X_Afstand_2_Ruit,X_Afstand_3_Ruit,X_Afstand_4_Ruit)
Dim oMax As Double = MaxOfMany(X_Afstand_1_Ruit,X_Afstand_2_Ruit,X_Afstand_3_Ruit,X_Afstand_4_Ruit)

'Remove min and max
For Each oValue As Double In oValues
    Try
        If oValue = oMin Then oValue = Nothing
        If oValue = oMax Then oValue = Nothing
    Catch
    End Try
Next

'Find the remaining values
Dim rValues(0) As Double
For Each oValue As Double In oValues
    If oValue = Nothing Then Continue For
    If rValues(rValues.Length - 1) <> Nothing Then
        ReDim Preserve rValues(rValues.Length)
    End If
    rValues(rValues.Length - 1) = oValue
Next

'Calculate the result Dim B1 As Double = Abs(rValues(0)) Dim H1 As Double = Abs(rValues(1)) Dim A1 As Double = (Binnen_diameter_Ruit / 2) + Laagdikte_Ruit Dim G1 As Double = Asin((B1 + H1) / A1)
MessageBox.Show(G1, "Title")

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 10 of 12

Anonymous
Not applicable

Yes it works thanks

0 Likes
Message 11 of 12

Anonymous
Not applicable

Is there a way to automate this code. What i mean is these values are connected to points that are denfined on a plane so when the plane moves like 1mm forward these values change. Is there a way that code loops and shows the values until that plane has traveld a certain distance ?

0 Likes
Message 12 of 12

Anonymous
Not applicable

I've just seen also When my part is under an angle the value of rValues(0) and rValues(1)  isn't correct, i think because my part is under an angle there no 2 negative and 2 positve values so when the code takes the min and max it cant get the correct one. If you could solve that it would be a big help! 🙂

 

 

thanks in advance

 

in attachement are pictures of what i mean with my part under an angle

0 Likes