Solved

vba code to determine largest of three variables

Posted on 2013-11-19
6
253 Views
Last Modified: 2013-11-19
excel vba 2010

I have 3 variables:


cn1 = 3
cn2 = 7
cn3 = 2

Is there vba code that help determine which of these 3 variables is the largest ?
mid and them smallest?

so the outsome would be

cn2 = largest
cn1 = mid
cn3 = smallest





Thanks
fordraiders
0
Comment
Question by:fordraiders
  • 3
  • 2
6 Comments
 
LVL 49

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 39658646
Hi,

Pls try

Dim arrVar(1 To 3)
arrVar(1) = cn1
arrVar(2) = cn2
arrVar(3) = cn3

Biggest = WorksheetFunction.Large(arrVar, 1)
Middle = WorksheetFunction.Large(arrVar, 2)
Lowest = WorksheetFunction.Large(arrVar, 3)

Open in new window

Regards
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 39659945
Don't give me any points for this but what the 'Large' function that Rgonzo1971 cleverly used is doing is sorting the values from highest to lowest so with a 4th value you could do this.

Dim arrVar(1 To 4)
arrVar(1) = cn1
arrVar(2) = cn2
arrVar(3) = cn3
arrVar(4) = cn4

Biggest = WorksheetFunction.Large(arrVar, 1)
Middle = WorksheetFunction.Large(arrVar, 2)
Lowest = WorksheetFunction.Large(arrVar, 3)
LowerThanLowest = WorksheetFunction.Large(arrVar, 4)

Open in new window

0
 
LVL 3

Author Comment

by:fordraiders
ID: 39660152
what declaration does
biggest
middle
lowest
lowerthan lowest get..

??

Thanks
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 3

Author Comment

by:fordraiders
ID: 39660162
why would thid not work ?

if  the value of
cn1 = 3
cn2 = 7
cn3 = 2


why would this imple statement not work ?

If cn2 >= cn1 Or cn3 Then

??
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 39660181
what declaration does
biggest
middle
lowest
lowerthan lowest get..
It would depend on the data. For integers Long would be best or otherwise Double or Currency.

If cn2 >= cn1 Or cn3 Then
You would need to use a series of If Else's but you could do this
Select Case True
    Case cn1 > cn2 And cn1 > cn3
        Msgbox "cn1 is the largest"
    Case cn2 > cn1 And cn2 > cn3
        Msgbox "cn2 is the largest"
    Case cn3 > cn2 And cn3 > cn1
        Msgbox "cn3 is the largest"
Ens Select

Open in new window

0
 
LVL 3

Author Closing Comment

by:fordraiders
ID: 39660557
Thanks all, Martinless,  Thanks for the additional help
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now