Excel Macro : Find Max repeatedly in column after value=0 - excel

I would like to repeatedly calculate the MAX value in a column, after the value in that column = 0 twice in a row. See example:
Torque (Lbf_in)
0
0
.827664554
9.673638344
45.82129669
60.63316727
58.07248688
38.35304642
18.0196209
4.054021835
0
0
...Repeat
We're finding the max torque per cycle of a handle using software that dumps it into excel. the double 0 values show us to reset to cycle the handle again, so we would like to capture the max value again after each double 0 value. The spread sheet has 1,800 cycles, so doing this manually is very time consuming. It's not consistently in the same number of rows that the cycle repeats (its based on time).
Thanks!

If you data is in column A, please enter this formula in cell B4 and drag it to the bottom. =IF(AND(A2=0,A3=0),MAX(OFFSET(A4,0,0,MATCH(0,A4:A700,0),1)),"")
Also make sure to add one 0 at the end of your data.

Related

Excel VBA - counting numbers resetting

Please pardon my English. I try to explain my needs in excel.
I am trying to write an excel VBA code for total number counts in a particular column. for example A:A columns are filled with specified numbers. 150,200,150,175,150,150....
with B column I have a code that counts how many 150's and 200's with a code of "COUNTIFS". its an easy code. (=COUNTIFS(A:A,B2))
Now I need every fifty numbers of 150's I should get number 1 in a particular cell ( for example, C2)
then another fifty numbers of 150 (that means hundred 150's) I should get number 2 in a same C2 cell. this will continue..
A reset button needed to reset this C2 value, again it should count every fifty number of 150's after reset start from beginning.
A data entry in a A column is a particular Sweet box number to particular shop. once I get a fifty sweet boxes delivered as per data, I should count 1. then another fifty sweet boxes it should count 2. once I demand a payment, I should reset the counts (not entire counts.). then again it start counting from one.
I think I explain my needs, and I hope you all are understand.
please help with VBA code.
Here is the code:
Write this code in the new module and call the function in the excel cell (Ex: C2).
Public Function COUNTCUSTOM(counted As Integer, rupee As Integer)
'counted - Total number of 150's
'rupee - 50 if 150 count reach 50 i will be 1, if 150 reached 100 i will 2.
Dim i As Long
i = counted \ rupee
COUNTCUSTOM = i
End Function

Excel - dynamically average the top values of a column

I have a column that may contain X number of values. I am looking to get the average of the top Y values, based on the number of X values that are above 0. There are just two rules on how to come to this value.
Y must represent 49% or less of the values greater than 0. If I have 11 total values that are above 0, then I can only take the top 5.39 values that I can take an average on.
Y must be a whole number and rounded down. To take the last piece of criteria further, 5.39 would round down to 5, so I am essentially getting the average of the top 5 values.
I feel like I am getting close with different solutions but have struggled. For instance, the solution at Getting average of top 30% of the values in one column is close, however, it appears to find the values above the 0.7 number, and not necessary the top 30%.
Here is my data,
92.593
88.889
45.679
88.889
0
88.889
87.654
0
69.136
41.975
49.383
0
40.741
50.617
0
Here I have 11 values that are above 0. I know that using the criteria I mentioned above, I can only average the top 5 values. Those top 5 values average out to be 89.383.
I have tried something like this,
=AVERAGE(LARGE(A1:A14), ROW($B$1:$B2)))
Where B1 just has the value 1, and B2 has a formula that calculates the number of values I can have, with no such luck.
I don't know your XL version, but this also works with older versions
=AVERAGE(LARGE(IF(A1:A15>0,A1:A15),ROW(INDIRECT(B1&":"&B2))))
remember to confirm as array formula using CTRL+SHIFT+ENTER
I hope that's what you are looking for,
bye.

Excel - standard deviation where source cells are a count

I have some data that looks like this
Condition 1
Condition 2
Condition 3
Condition 4
Condition 5
0
0
0
70
0
0
50
10
0
0
120
0
0
5
5
Where the value in each cell is the number of meters of an asset that is the given condition. Or in other words, a count of the number of meters that are a '4'.
How do I calculate a standard deviation for this? Obviously the std.dev would be '0' for the first row, higher for row 2, and fairly low for row 3.
Something similar to REPT, but that repeats a value x times in a formula?
I considered a helper column but the number of meters that there are in total makes this impractical.
I am not a math expert, but I can show you how to "make a range of numbers" based on the criteria shown, using Excel 365.
Suppose your data is in the range B2:F4 as shown below. In cell G2, enter the following formula and drag it down:
=STDEV.P(--FILTERXML("<t><s>"&TEXTJOIN("</s><s>",1,REPT($B$1:$F$1&"</s><s>",$B2:$F2))&"</s></t>","//s[number()=.]"))
The above will calculate the standard deviation using the STDEV.P function, but I am unsure if this is the right function to use as there are many other variations to the original STDEV function.
Regardless, the following part of the formula is able to return a range of numbers as desired:
=--FILTERXML("<t><s>"&TEXTJOIN("</s><s>",1,REPT($B$1:$F$1&"</s><s>",$B2:$F2))&"</s></t>","//s[number()=.]")
You can view this question and the answer by JvdV to understand the use of the FILTERXML function.
Another way of doing it is to use the alternative SD formula
which would give you
=SQRT((SUM(A2:E2*COLUMN(A2:E2)^2)-SUM(A2:E2*COLUMN(A2:E2))^2/SUM(A2:E2))/SUM(A2:E2))
for the population standard deviation.
The Excel 365 version using Let is more readable I think:
=LET(x,COLUMN(A2:E2),
mpy,A2:E2,
n,SUM(mpy),
sumxsq,SUM(mpy*x^2),
sumsqx,SUM(mpy*x)^2,
numerator,sumxsq-sumsqx/n,
SQRT(numerator/n)
)
A bit less obviously, you could get it from the original formula
=SQRT(SUM(A2:E2*(COLUMN(A2:E2)-SUM(A2:E2*COLUMN(A2:E2))/SUM(A2:E2))^2/SUM(A2:E2)))
Again, in Excel 365 you could write this as:
=LET(x,COLUMN(A2:E2),
mpy,A2:E2,
n,SUM(mpy),
xbar,SUM(mpy*x/n),
numerator,SUM(mpy*(x-xbar)^2),
SQRT(numerator/n)
)
Change the denominator to
(SUM(A2:E2)-1)
for the sample standard deviation.
I ended up figuring it out.
I added a column which calculated the average. (Say column F)
I then had a formula like this
=SQRT(SUM(A2*POWER((1-F2),2),B2*POWER((2-F2),2),C2*POWER((3-F2),2),D2*POWER((4-F2),2),E2*POWER((5-F2),2))/SUM(A2:E2))
Essentially this calculated the variance from the mean for each condition value, multiplied by the number of values (e.g. number of meters) of asset that are that particular condition, then did the normal other standard deviation calculations (sum, divide by total, square).

Mileage log Trouble

I am trying to create a formula that finds the monthly mileage for a vehicle. The simple =max(array)-min(array) will not work because there are blanks/0 cell values due to a vehicle not being driven. This causes the minimum to be zero. I have tried to use the small(array,2) but if there are multiple cells, it counts every blank as a separate value. Is there a simple way to express this? All cells are pulling the mileage from a separate workbook so even the blank cells have a formula. I want to write a formula for the following expression, "the cell with the maximum mileage minus the cell with the minimum mileage but the minimum cell can not be zero or blank, if it is skip/do not use".
I know the maximum for the first box is 40532 - minimum is 40285= 247
The maximum for the second box is 13281 - minimum is 13154= 127
You can use MAXIFS()for this: it calculates a maximum, in case of criteria.
MAXIFS() is described in this URL.
Edit for more explanation
I've just created the following worksheet:
A B C
1: 100 200 300
2: 0 100 300
3: 400 50 200
4: 0 100 100
I have entered this formula:
=MINIFS(A1:C4;A1:C4;">0")
The first A1:C4 is the range, where the minimum should be calculated.
The second A1:C4 is the range, where the criterion should be validated.
">0" is the criterion itself.
The result was 50, as it should be.

Convoluted formula using INDIRECT, ADDRESS and MATCH - is there a better way?

I need to get a MAX of a range that is bound by two 1s in a helper column.
The 1s are a variable number of rows apart.
By way of example, here is a sample of what I'm talking about:
1
0 -1.10%
0 0.00%
1
0 1.43%
0 1.15%
0 2.12%
0 2.69%
0 1.32%
0 0.86%
0 -0.69%
1
~
[and so on]
So, for instance, there are two ranges visible that I'm interested in here -- the range between rows 1 and 4 and the range between rows 4 and 12.
In a third column, wherever there is a 1 in the helper column, I want the MAX of the ranges.
I've managed to cobble together this formula that does the job (this is copied directly from the spreadsheet so it's in row 122 and the data currently goes to row 16120, the helper column is column E and the column with the values is F):
=IF(E122=1,MAX(F122:INDIRECT(ADDRESS(ROW()+MATCH(1,E123:$E$16120,0),COLUMN(F122),4))),"")
My basic thinking is to build the bottom of the range by looking for the next 1 down the helper column (using MATCH), add that to the current row (using ROW and COLUMN) wrapped inside an ADDRESS function and then tie it all together using INDIRECT. Finally, it sits inside an IF to only hit the rows with a 1 in the helper column.
Can anyone think of a more elegant, less cumbersome way?
Thanks in advance.
If you have a larger dataset, I'd recommend to use INDEX instead of OFFSET! The latter is volatile, i.e. Excel will recalculate all OFFSET formulas and any dependants every time it does any recalculation. INDEX on the other hand is non-volatile, i.e. only if any of it's predecessors change will Excel recalc the formula.
Therefore, give this formula a try:
=IF(E122=1,MAX(F123:INDEX(F123:$F$16120,MATCH(1,E123:$E$16120,0))),"")
You could use OFFSET:
=IF(E122=1,MAX(OFFSET(E122,1,1,MATCH(1,E123:$E$16120,0)))-1,"")

Resources