VBA Code: Average Columns to Right (Variable # of Columns) - excel

I am writing a code that will average all values to the left in data sets that have varying numbers of columns. For example, if my data set is A1:AC1, then I used the code
ActiveCell.FormulaR1C1 = "=AVERAGE(RC[-28]:RC[-2])"
This code works for finding the average of cells B1:AB1 which is exactly what I want in this instance, but if my next data set has say 40 columns, this code will only average the 26 cells referenced. I have researched everywhere and found that I should somehow first count the columns to the left and then use that in the average function, but I am not sure how exactly to accomplish this. How can I write a code that will average all values to the left of my active cell regardless of how many columns it is?

You're close, just:
Don't use relative references for your starting range.
Keep the ending range relative.
Try this:
ActiveCell.FormulaR1C1 = "=AVERAGE(RC2:RC[-2])"
This will always start in column B and end 2 columns before the current cell, for example.
Hope that makes sense / helps.

Related

Excel ultra-dynamic ranges with OFFSET(...COLUMN()) to convert to non-volatile

I think I made a breakthrough with "ultra-dynamic" ranges. That's how I call them because not only they are dynamic; but also they refer to different ranges based on the cell address in which they are written in! Now I need to advance this breakthrough even further. Those of you using dynamic ranges in Excel, especially the dynamic-range-gurus, will be thrilled to read below and can possibly help in this advancement:
Disclaimer: If you are not familiar with dynamic ranges do not attempt to read below!
Background:
Our sheet has cells with calculations on top and a pivot table below.
Each cell above refers to the pivot table cells below in the same column.
The first column of the pivot table (titled "Row Labels" by default) is sorted from top to bottom in descending order. Each of the next columns has the result of a different test.
At one point somewhere in the middle rows of the pivot table there is a "marker line" that separates the top part of the pivot from the bottom one.
Let's call the top part "Uppers" and the bottom part "Downers".
Let's call both parts together "Population". Population is a non-contiguous range because the "marker line" that separates the Uppers from the Downers intervenes.
For each cell above the pivot there are calculations for the pivot column exactly below that need to refer to the Uppers or Downers or Population of the column itself.
Previously, my formulas in all of the cells above were a repetition of something like this below:
= MAX( OFFSET($A$79,$B$5+1,COLUMN()-1,$B$6,1) )
This example gives the maximum of the area of the Downers in the same column below.
Where:
A79 is the top left corner of the pivot table,
B5 has the row number of the marker line (relative to A79) to signify that our column's Downers area starts from the first row below that marker row (hence the "+1"). The formula of that cell has an XMATCH formula to produce the resulting row number. For example if the Uppers are positive numbers and the Downers are negative numbers, all sorted in descending order, the XMATCH is searching for the 0 row that separates the two parts of the population.
COLUMN()-1 returns the current column's number for offsetting to the right and the -1 is necessary because the first column is 0 rather than 1 offset to the right, and
B6 has the number of rows ("height") to the last row number of the pivot (relative to B5) where our Downers end. The cell contains the usual COUNTA function that counts the number of rows in the pivot table and from this number it subtracts the number of the marker row in B5 to get the "height" of the Downers range.
The OFFSET... part of the formula when copied to any cell with calculations above the pivot will always give the Downer's area in the same column.
And here we come to the "ultra"-dynamic part: I tested (and it works!) naming a range as:
ColDowners: =OFFSET($A$79,$B$5+1,COLUMN()-1,$B$6,1)
And I substituted all the cells with calculations that refer to the Downers with something like:
= MAX( ColDowners )
Miraculously, all cells with this named ultra-dynamic range, are calculated with the correct values for the Downers exactly below them in the same column! Sometimes when the Workbook is initially loaded, the cells appear with 0 values, but immediately when you hit F9 to Calculate they get the correct value (which of course is not a problem).
So this range is "dynamic" in two ways: (1) because start and end rows are dynamic (as usual) and (2) because it results to a different range depending on where it's written!!
And now for the advancement. Any dynamic-range-guru's input would be extremely valuable:
OFFSET is a volatile function with the known performance issues. Can we replace it with two non-volatile INDEX functions separated by a colon (":")? I know how to name a range that starts from a specific cell and ends in a different one every time (eg. =A$155:INDEX(...COLUMN()...)). But can BOTH the start and end of the range be INDEXified [sic]? i.e. can it be something like =INDEX(...COLUMN()...):INDEX(...COLUMN()...). And, consequently, if we name a range with this formula, will it work?
Answers will have to exclude volatile functions like INDIRECT and will have to be as simple as possible. The resulting range of the formula of the ultra-dynamic range will have to be different depending on which COLUMN() it's written in in the sheet (like the OFFSET one above) and will have to be "able" to start from a specific number of rows below A79 (the number written in B5 in the example above) and end in the one written in B6.
The correct formula that defines an ultra-dynamic-range for replacing the 'OFFSET' formula:
=OFFSET($A$79,$B$5+1,COLUMN()-1,$B$6,1)
Is:
=INDEX($A:$XX,ROW($A$79)+$B$5+1,COLUMN()):INDEX($A:$XX,ROW($A$79)+$B$7,COLUMN())
Where in B7 is the number of the last row of the pivot. (=Something like B5+B6 plus/minus 1 or 2 - test it for your case)
If you Define a Name of a Dynamic Range with this ultra-dynamic formula, it will adapt to give you different ranges depending on the location of the cell where you copied it to!! It will always give you the same parallel range in your column and will produce different results in different columns! I tested it and it works perfectly, plus it made my calculations lightning faster since INDEX is non-volatile (as opposed to OFFSET).
One more tip for the example above: I also tried nested range names and they work! For example I Defined the Range for ColPopulation as:
=(ColUppers,ColDowners)
Note that this is an ultra-dynamic-nested-range-name! Of course, it can work only for simple functions (such as =MAX(ColPopulation)) and will not work in functions such as SUMPRODUCT that need contiguous ranges. Still, it is a very useful thing to know that you can Define Ranges by adding other range names with commas!
Great help from all involved! Thanks a lot!

IF + AND + Date range formula

I have a problem to create a formula so I want to ask you for your help.
Excel sheet has 150 000 rows and with this formula I want to safe a time.
I have a Date, Name and Status and I need to see in other cell which Name was 4x or more time in consecutive GOOD or OK
Example of input:
https://imgur.com/aRALd9S
I think IF + AND + DATE Range it’s enough, but I don’t know how to put it together.
Thanks a lot for your suggestions !
Here is what i have so far: https://imgur.com/Y5WAov5
=COUNTIFS($D$2:$D$15;D2;$C$2:$C$15;"OK";$D$2:$D$15;D2;$E$2:$E$15;">="&E2;$E$2:$E$15;"<="&E2+7)+(COUNTIFS($D$2:$D$15;D2;$C$2:$C$15;"GOOD"))
With this i'm able to count how many times i have a Name, which is OK or GOOD and is in one week range, but i still don't know, what i have to change, that i will stop to count when false is there
Well here's something you could try. You could do it in one formula with an array formula, but with 150K rows it seems much better to try and avoid array formulas and use helper columns where necessary.
The first helper column just contains the person's ID if the row contains FALSE:
=IF(H2=FALSE,I2,"")
The second helper column contains the offset from the current row to the next FALSE for the same person:
=IFERROR(MATCH(I2,K2:K$15,0)-1,16-ROW())
So now you can use basically your same COUNTIFS formula but replacing each range with an INDEX which specifies how many rows you should count:
=IF(H2=FALSE,0,COUNTIFS(I2:INDEX(I2:I$15,L2),I2,H2:INDEX(H2:H$15,L2),"GOOD",J2:INDEX(J2:J$15,L2),">="&J2,J2:INDEX(J2:J$15,L2),"<="&J2+7))+
IF(H2=FALSE,0,COUNTIFS(I2:INDEX(I2:I$15,L2),I2,H2:INDEX(H2:H$15,L2),"OK",J2:INDEX(J2:J$15,L2),">="&J2,J2:INDEX(J2:J$15,L2),"<="&J2+7))
Note 1
The 16 in the second equation is to allow for the case where there are no more rows labelled FALSE after the current row, so the MATCH fails. This makes the Countifs count everything from the current rows to the end of the data.
Note 2 - expanding to a larger range of data
You should be able replace the figure 16 with countA(I:I), the size of the data plus headers.
There shouldn't be a problem with using a larger range for the Index e.g.
=IF(H2=FALSE,0,COUNTIFS(I2:INDEX(I2:I$150000,L2),I2,H2:INDEX(H2:H$150000,L2),"GOOD",J2:INDEX(J2:J$150000,L2),">="&J2,J2:INDEX(J2:J$150000,L2),"<="&J2+7))+
IF(H2=FALSE,0,COUNTIFS(I2:INDEX(I2:I$150000,L2),I2,H2:INDEX(H2:H$150000,L2),"OK",J2:INDEX(J2:J$150000,L2),">="&J2,J2:INDEX(J2:J$150000,L2),"<="&J2+7))
but increasing the search range in the MATCH to 150K rows in the second formula does seriously affect performance when repeated 150K times. The only solution I can think of at the moment is to see if a maximum can be placed on the distance from any occurrence of a name to the next occurrence of the name with FALSE next to it.

Excel formula to lookup the last value in a column and return the value of the adjacent cell

I have the following formula to return the value of the last value in a column:
=LOOKUP(2,1/(D:D<>""),D:D)
What I need now is to return the value of the cell adjacent to it as well. (It will not necessarily be the last value in that column and the info in Column D could have duplicates.
If your data looks like this:
A 1
A 2
A 3
B 4
B 5
B 6
C 7
To get last value this will do the trick:
=INDIRECT("B"&COUNTA(A:A))
And to get last where value is A:
=INDIRECT("B"&MATCH("A",A1:A7,0)+COUNTIF(A1:A7,"A")-1)
Just use next column:
=LOOKUP(2,1/(D:D<>""),E:E)
Ok, So I have found an answer by playing around with array formulas.
The problem was that this is a stock control sheet where there are changes made at multiple times, each recorded in the next available row. There is always a date (Column E) but not necessarily a Supplier, as it might be stock moving out. When a Supplier delivers, the Supplier name is recorded in Column D. In D1 the last supplier is then shown with the following formula.
=LOOKUP(2,1/(D:D<>""),D:D)
I want to then see what date it was last received. The formula I found that works is as follows (Array Formula):
=INDEX(E:E,MAX(IF(D:D=D1,ROW(D:D)-ROW(INDEX(D:D,1,1))+1)))
This is generally how I do it:
=XMATCH(FALSE,ISBLANK(A:A),0,-1)
This is what each part does:
Parameter
Explanation
FALSE
Instructs Excel to find the first instance of FALSE that it finds
ISBLANK(A:A)
Takes in the column A:A and notionally assigns a value to every item in the column
0
Means we want an exact match. Probably not necessary to put in, but I think it's good practice anyway
-1
Instructs Excel to start the search at the bottom/right of the range and work up/left. If you change this to 1 (the default), Excel will begin the search at the top/left and work down/right
So, taken together, this will search from the bottom of the column A:A, until Excel finds the first cell that is not blank, and return that cell.
Also, yes, this equation can be changed to a row format (e.g. 1:1), and can take a smaller range (e.g. A1:A20), but it cannot take a 2-dimensional range (e.g. A1:B20).
As a practical matter, this approach is much faster than other approaches (and much faster than you'd think, given it's evaluating against every row/column in the range), and won't get fooled by columns that have empty spaces in them (like with a COUNTA style approach).

HLOOKUP with wildcard and SUM more than one column

I need help with the following:
I have a worksheet containing some data. Row 1 is header and from row 2 downward is the data. At the end there is total for all the data above. This worksheet is dynamic, i.e., if week 1 has 200 rows of data, then week 2 could have 250 or 190 rows of data.
Likewise, the columns across, change every week. This week I have 18 columns and next week I could have 20 columns.
Within row # 1, the header, I have two headings "CTAEO1P" and "CTAEO2P".
On another worksheet, I want to add the "totals" of both of those columns i.e., Individual totals of CTAEO1P = 32.98 + CTAEO2P = 46.25 = 79.23
I am using named ranges and named the whole of the worksheet with data as "MT". The range is whole of the worksheet so when next week I copy the data over from another worksheet, I should not have to adjust the range.
I am using the following formula, courtesy of another expert on this forum:
=HLOOKUP("CT*",MT,MATCH(9^99,INDEX(MT,0,MATCH("CT*",INDEX(MT,1,0),0))),0)
This formula look for any column that starts with "CT" and then "Match(9^99" and "index" finds the last number within that column (the total in this case) and then return that value on the worksheet. In this case this formula is returning "32.98" only, as this is the first occurrence.
I think I can use "Sumproduct" formula here but then a) I would have to create more than one named range, one for the header row and another for the "Total" row, b) every week I would have to adjust the range for "Total" row. Unless, if I can nest "Match(9^99..." part within "SUMPRODUCT" function.
I want to use "MT" range alone and want to add the totals of all the columns that start with "CT".
I hope I have been able to explain my problem better enough to make some sense, however, if you need any further information, then please let me know.
Regards
Tariq
I will forget about the MT range, as long as your data starts in A1 this will work
=SUMPRODUCT(ISNUMBER(SEARCH("CT*";OFFSET(A1;0;0;1;MATCH(9^99;2:2))))*OFFSET(A1;MATCH(9^99;A:A)-1;0;1;MATCH(9^99;2:2)))
Depending on your regional settings you may need to replace field separator ";" by ","
I think you can use a relatively simple SUMPRODUCT solution like this
=SUMPRODUCT((LEFT(INDEX(MT,1,0),2)="CT")*ISNUMBER(MT),MT)/2
SUMPRODUCT will total all values in the relevant columns, including the totals so divison by 2 will ensure you get the correct count
If you don't like that approach then assuming first column of MT always has data and that the totals for each column will all be in the same row you can use SUMIF like this
=SUMIF(INDEX(MT,1,0),"CT*",INDEX(MT,MATCH(9^99,INDEX(MT,0,1)),0))
That should be more efficient than the first version

Excel Problems- Calculated value as a cell reference

I'm relatively new to excel programming. I'm working on making a spread sheet that shows exponential decay. I have one column (A1:A1000) of 1000 random numbers between 1 & 10 using the TRUNC(RAND()*10,0) in each cell. The next Column (B1:B1000) has a logic mask =IF(A1=0,1,0) , where if the value in the A cell is 0, then the B cell shows a 1. Next, to find the number of 0's in the A column, I have the next column taking the sum of B1:B1000, which returns the number of 0's that showed up in the first column. I'm sure there's an easier way to do that, but this seems to work fine.
Here's my problem, hopefully it's clear what I'm asking:
Next, I want to take the sum of the logic column (B) from B1:B(1000- the value of the sum from (B1:1000)) in the cell below the cell that calculates sum(B1:B1000). Is there a way to to algebra in a cell formula to reference a cell? More simply, if I want to refer to A3, for example, is there a way to input something like A(2+1) to get A3? Does this make sense?
You can very easily do, in VBA:
ActiveCell.Formula = "=A" & (2+1) & "+15"
In Excel:
=INDIRECT(ADDRESS(2+1, COLUMN(A1)))+15
This will set the formula to "=A3+15". Generally, this is best done with variables, so remember to do that.
In Excel, and as pointed out by Eric, you can write the referance to the cells just like normal strings thanks to INDIRECT() function.
Just make sure that the string passed to INDIRECT() is a valid cell reference.
For example :
=SUM(INDIRECT("B" & 2+7*(H2-1)):INDIRECT("B"&(2+7*H2)-1))
Here, I sum 7 lines for each week (H2). It gives the sum of B2:B8, B9:B15, B16:B22, etc.
Hope my example will help you figure out how to use it in real situation.

Resources