VBA To Determine Which Variable of A Group Of Variables Holds the Lowest Value

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have 4 variables: hpe1_st, hpe2_st, hpl1_st, hpl2_st.

I am looking for a vba solution to:
1) of the four, which one has the lowest value
2) return the variable name (eg hpe1_st, hpe2_st etc.) as another string variable (rtn_str) value. Ideally, I would just like to return the leading 4 characters)

Example
hpe1_st = 5
hpe2_st = 7
hpl1_st = 1
hpl2_st = 3

rtn_str = hpl1

Should multiple variables hold the samr value, it's ok to return the any of the matching variable.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Curiosity is forcing me to ask why you would want to try and do it this way?

A quick google search suggests that it is possible, but looking at the methods of doing so, I would look for a more practical alternative first.
 
Upvote 0
I have 4 variables: hpe1_st, hpe2_st, hpl1_st, hpl2_st.
Before attempting to answer this question, I have a question of my own... do you really only have four total variable to process or is this a highly simplified example? If a simplified example, how many variables do you actually have?
 
Upvote 0
Curiosity is forcing me to ask why you would want to try and do it this way?

A quick google search suggests that it is possible, but looking at the methods of doing so, I would look for a more practical alternative first.
I know I could do it using a series of if/thens or select case, but I thought there may be something more efficient. Arrays, min and combination of. A Google search is only as good as the terms you enter to search for, which clearly you know better than I do.

Before attempting to answer this question, I have a question of my own... do you really only have four total variable to process or is this a highly simplified example? If a simplified example, how many variables do you actually have?

Just these four. Their values are a result of a calculation. If they were in a range, it would be as simple as using the min function. Sure, I could put these values in a range and do it that way, but if there is a more efficient way to do it, I;m here to learn is all.
 
Upvote 0
Personally for the small number of variables involved, I would go with a select case method. Glancing over this (which was the first thing that I found when I did a quick search earlier), it appears that whilst not impossible to do what you ask it would most likely be a less practical method.

That said, I would suggest waiting to see if @Rick Rothstein has any neat tricks in reserve first.
 
Upvote 0

Forum statistics

Threads
1,215,012
Messages
6,122,682
Members
449,091
Latest member
peppernaut

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top