OberonPlace.com Forums (http://forum.oberonplace.com/index.php)
-   CorelDRAW/Corel DESIGNER VBA (http://forum.oberonplace.com/forumdisplay.php?f=16)
-   -   how to find maximum value of multiple variables (http://forum.oberonplace.com/showthread.php?t=24796)

 aakkaarr 22-06-2017 02:33

how to find maximum value of multiple variables

how to find maximum value of multiple variables
variables x1, x2, x3, x4, x5........xN

how to find the maximum in above variables

 shark 22-06-2017 07:39

There are two ways to solve the problem. First - use arrays instead of regular variables. Instead x1 = 5, x2 = 3, x3 = 10 etc. use Ar(1) = 5, Ar(2) = 3, Ar(3) = 10. Then the maximum value can be calculated as follows:
Code:

```Max = 0 For i = 1 to 3     If Ar(i) > Max Then Max = Ar(i) Next MsgBox "Maximum value is " & Max```
Second way - use function with ParamArray:

Code:

```Private Function Max(ParamArray ar()) Dim z%, Mx%     Mx = 0     For z = 0 To UBound(ar)         If ar(z) > Mx Then Mx = ar(z)     Next     Max = Mx End Function```
Then the maximum value among the other variables can be calculated by calling one function
Code:

```Dim x1%, x2%, x3%, x4%     x1 = 10: x2 = 5: x3 = 20: x4 = 15     MsgBox "Maximum value is " & Max(x1, x2, x3, x4)```

 All times are GMT -5. The time now is 04:01.