View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Adnan Adnan is offline
external usenet poster
 
Posts: 59
Default Math and action via VBA

Bob,

I appreciate your nice detailed explanation. With the help you provided you
got me almost squared away. Just these two little things would fix the issue:

One, the range is fixed range (A1:A20)
This 75% should reflect or be based on quantity of titles not number of rows.

Heres an example of how it should look:

A B C D
1 10 Title 1 No
2 20 Title 2 Yes
3 1 Title 3 Yes
4 Title 4 No
5 Title 5 No
6 1 Title 6 Yes
7 Title 6 No
8 3 Title 6 Yes
11 1 Title 6 No
12 10 Title 6 No
13 1 Title 6 No
9 Title 6 No
10 50 Title 6 Yes
11 1 Title 6 No
12 1 Title 6 No
13 1 Title 6 No

Again, thank you!
Adnan

"Bob Bridges" wrote:

And you want this to be independent of the actual number of rows, right? I
mean, you want it to work whether the rows run A1:A20 or A1:A37?

Well, let's start he There are a number of ways of getting a program to
determine the extent of a range, but if it'll work the one I think simplest
is the .End property of a range. Let's assume that your rows start in row 1
and that there's at least a blank line after the end of that range; in that
case you can find the last row this way:

LastRow = YourSheet.Cells(1, 1).End(xlDown).Row

Cells(1, 1) is synonymous with Range("A1"), of course, and .End(xlDown)
returns the same cell that would be activated if you focused on A1, then hit
<End, <Down. That gets you the cell; I tacked on .Row because all we need
is the row number, not the cell itself.

Your code can then add up the quantities in A1:A<LastRow either by looping
or by using the Sum worksheet function:

SumPerson = 0
For ir = 1 to LastRow
SumPerson = SumPerson + YourSheet.Cells(ir, 1).Value
Next ir

...or just

SumPerson = Application.WorksheetFunction.Sum(Range("A1:A"&Las tRow))

I think WorksheetFunction.Sum is better because it already knows how to
ignore non-numeric cells etc; your code could bomb if you had an unexpected
value in one of the QT cells. (Or maybe that's an advantage.)

Ok, now you have the sum of the QT column. If you don't care WHICH of the
rows get Yesses and Nos, you can just got down the list assigning Yes until
you pass 75%, like this:

SumPerson = SumPerson * 0.75
For ir = 1 to LastRow
if SumPerson 0 then vx = "Yes" Else vx = "No"
YourSheet.Cells(ir, 3) = vx
SumPerson = SumPerson - YourSheet.Cells(ir, 1)
Next ir

This way you take away the count of Yes personnel from SumPerson until
SumPerson is entirely depleted, and then quit the loop. The disadvantage to
this method - if you regard it as a disadvantage - is that Yes will end up
being assigned to somewhat more than 75% of the personnel, unless it just
happens to come out even. But then you cannot be sure of it being exact even
if you do it manually, so you probably needed it to be approximate only. If
you need it to be as close as possible, you'll have to get a little fancier,
starting with a sort of the rows on column 1 descending. But this is enough
to get you started; if you need to get fancier, you can always ask again.

--- "Adnan" wrote:
A1:A20 has quantity of personnel
B1:B20 has their titles
C1:C20 has is blank

I need to know how to put a Yes on 75% of these people and 25% No via VBA on
range C1:C20.