Need an Array?

Boozer

Board Regular
Joined
May 20, 2002
Messages
52
Okay, lets see if I can get my issue across clearly...
Lets say I have 10 departments, each with a different number of people in two columns in a spreadsheet. Lets say the name of the columns are "Department Name" and "Number of People". Now lets say you are given a variable "Total_Number_of_People" (TNP from here on out). I need to find the fewest number of departments that, when added together, come closet to the TNP and return which departments they are and the number of people in each department. I wrote code based on if and do statements nested within each other, and have it working by finding up to four departments which come close to the TNP variable, but there has got to be an easier way, because this way will just keep getting nastier :) My friend told me to use an array, but didn't have time to explain it. I used arrays back when I was in college in C++. But can't remember how they worked exactly. Anyone have any suggestions? Please save me from the nasty nested if's and do's :)
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
I did the hypthetical thing because that is just one possible number of departments I could have. The actual number is not decided until run time, when the data is reviewed, then i just pass the variable that contains the number of departments. The max number would be 27.
 
Upvote 0
Not that providing the answers will guarantee a satisfactory solution...
On 2002-11-12 11:32, Boozer wrote:
...come closet to the TNP...
What does that mean? Absolute value of difference? Closest without going over? Closest without going under? Something else altogether?

You might want to explore the Solver add-in that is packaged with XL (Tools | Solver...).
 
Upvote 0
I meant the closest as in nearest to the target number. Which would be the smallest value of the absolute value of the difference.
 
Upvote 0
Don't mean to give you a hard time, but this remains an ambiguously defined problem. I suspect that in your mind it is clearly defined, but, unfortunately, not in this discussion.

You have two objectives that may not necessarily work together. You want to minimize the number of departments and the absolute value of the difference between TNP and actual employees. This needs to be resolved. Suppose there are two solutions. One has 10 departments and an absolute difference of 5. The other has 9 departments and an absolute difference that is greater than 5. Which is the preferred solution? Why? How does one generalize from this example?
 
Upvote 0
Hey, no problem tusharm :) You're not giving me a hard time, I'm just not being as clear as I need to be. Thanks your for your patience :)
 
Upvote 0

Forum statistics

Threads
1,214,826
Messages
6,121,793
Members
449,048
Latest member
greyangel23

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