I have the following indirect reference:
=AVERAGE(INDIRECT("'" & Pages!D5 & "'!" & Pages!J3))
On 'Pages'!D3 I have another page '1D_3' and on 'Pages'!J3 I have 2:2.
I need to alter the above code to instead find the average of the differences between each successive value. I can only change the value in 'Pages'!J3 (2:2) and I can change the equation above.
From this reference http://www.excelforum.com/excel-general/742194-difference-between-consecutive-number.html I have come up with how I can do the difference between each successive, but only if I have a known range. Ex. For elements A1:G1,
=SUM(A1:F1-B1:G1)
I could then do the following for the average:
=SUM(A1:F1-B1:G1)/(COUNT(1:1)-1)
But I do not know how to do the SUM part if I have an unspecified range. I also am unsure of how to implement this in the INDIRECT function.
Just some additional background info that may clarify this, The values in the array are times from a stopwatch. It was assumed that each value would be a time difference, but instead it is just the time on the stopwatch (Ex. 0.0015, 1.0034, 2.356, etc.). I want to find the average time duration between each value.
Any help would be greatly appreciated.
Before solving the formula you seek, let me just point out that all you need is the (EndValue-StartValue)/NumberOfEntries - no need to calculated the difference of each timing point first, as this will cancel itself out in the final calculation anyway.
Now assuming that Pages!J3 still only contains the row number, i.e. the formula =INDIRECT("'" & Pages!D5 & "'!" & Pages!J3) will return you a reference to this row. In the following formula, I use X as an replacement for the INDIRECT formula, to make it more readable:
=(OFFSET(X,0,0,1,1)-OFFSET(X,0,COUNT(X),1,1))/COUNT(X)
Now you only need to replace X with the above formula - or simply add a named range with the formula. If you combine it, you'll end up with this formula:
=(OFFSET(INDIRECT("'" & Pages!D5 & "'!" & Pages!J3),0,0,1,1)-OFFSET(INDIRECT("'" & Pages!D5 & "'!" & Pages!J3),0,COUNT(X),1,1))/COUNT(INDIRECT("'" & Pages!D5 & "'!" & Pages!J3))
I tried what the other poster suggested and it did not work, even when replacing the remaining 'X'. I worked through a few other examples in Excel to finally come to the following conclusion. To not let this be lost to the sands of the internet, I decided to post my own solution. So I wanted an average of the differences between time values. Essentially, I wanted a sum over the total number of times (mean). The difficulty was in the indirect referencing and INDIRECT referencing within an INDIRECT reference.
So what I had was data on multiple worksheets, pages (Pages on B through E columns) and ranges (Pages on J column) on another worksheet, and the resulting operations on another sheet.
=SUM(INDIRECT("'"&Pages!D5&"'!C2"):INDIRECT("'"&Pages!D5&"'!"&SUBSTITUTE(ADDRESS(2,COUNTA(INDIRECT("'" & Pages!D5 & "'!" & Pages!J3)),4),"1",""))-INDIRECT("'"&Pages!D5&"'!B2"):INDIRECT("'"&Pages!D5&"'!"&SUBSTITUTE(ADDRESS(2,COUNTA(INDIRECT("'" & Pages!D5 & "'!" & Pages!J3))-1,4),"1","")))/(COUNT(INDIRECT("'" & Pages!D5 & "'!" & Pages!J3))-1)
To break this into parts, we have:
=SUM(junk)/COUNT(INDIRECT("'" & Pages!D5 & "'!" & Pages!J3))-1
And the 'junk' part is as follows. Ultimately, it was in the form:
=SUM(A1:F1-B1:G1)
Without INDIRECTs, if we had a page of data named '8D_2FU':
=SUM('8D_2FU'!C2:AW2-'8D_2FU'!B2:AV2)/(COUNT('8D_2FU'!2:2)-1)
Note that for this implementation we pick a maximum column on AW which was fine for all possible datasets.
There is also one more part of interest in the 'junk'. You cannot make an INDIRECT reference within an INDIRECT reference, so to fool Excel, we can use the SUBSTITUTE command and get the same result by converting the address contained in the INDIRECT reference.
SUBSTITUTE(ADDRESS(2,COUNTA(INDIRECT("'" & Pages!D5 & "'!" & Pages!J3)),4),"1",""))
Finally, to use this, you must make this an array operation by instead of typing 'Enter' you must type, CTRL+SHIFT+Enter. The result was also confirmed using a brute force test case.
Related
Not sure if my terminology is correct but the problem is that I have a vba module that I am using to analyze data(count unique values). The formula is fine and works when after I correct excels change.
VBA line to insert formula is
ws_MachineNumber.Range("D2").FormulaLocal = _
"=SUM(IF('Roh Daten_IA'!$B$2:$B$" & lastR_RDi & "= A2, 1/(COUNTIFS('Roh Daten_IA'!$B$2:$B$" & lastR_RDi & ",MachineNumber!A2,'Roh Daten_IA'!$G$2:$G$" & lastR_RDi & ",'Roh Daten_IA'!$G$2:$G$" & lastR_RDi & ")),0))"
Which I am expecting to show up in the cell as
=SUM(IF('Roh Daten_IA'!$B$2:$B$296= A2, 1/(COUNTIFS('Roh Daten_IA'!$B$2:$B$296,MachineNumber!A2,'Roh Daten_IA'!$G$2:$G$296,'Roh Daten_IA'!$G$2:$G$296)),0))
However for a reason unbeknownst to me excel keeps making it
=SUM(IF(#'Roh Daten_IA'!$B$2:$B$296= A2, 1/(COUNTIFS('Roh Daten_IA'!$B$2:$B$296,MachineNumber!A2,'Roh Daten_IA'!$G$2:$G$296,#'Roh Daten_IA'!$G$2:$G$296)),0))
not sure why it keeps referencing it that way. Prior to this I didn't have the abs ranges and figured that was the problem but the # symbol is somehow causing the formula to operate incorrectly and it gives a result of 0 for every occurrence regardless of what the correct evaluation should give.
Thank you
I am trying to enter a "=$M15-$N15" formula to cell A15. However, need to use variable (r) instead of 15 as I don't know that value until later in the code. Trying the below but it is not working. What am I doing wrong?
Range("A" & r).formula = "="$M" & r - "$N" & r"
after the code is run, I need to have a formula ($M15-$N15) in Cell A 15.
I suspect you're looking for the INDIRECT function. An example of this, based on your explanation, would look like:
=INDIRECT("M"&A1)-INDIRECT("N"&A1)
If you're actually looking for VBA, you could use the following:
Range("A" & r).formula = "=$M" & r & "-$N" & r
However, this makes me suspect this is part of some over-arching routine, and, without knowing more, I can only speculate there might be a better or more efficient method(s) for going about it.
I need to use a vlookup function where I would get all the arguments from different functions and use them here.
Eg: ActiveCell.Formula= “=VLOOKUP(B2, ‘sheet 2’!$A:$I, 4, FALSE)
I would like to write this as:
VLOOKUP(element, f_range, col_num, true_false)
Currently, I only want the column to be dynamic, but in the future, I would require all these to be dynamic.
When you write a formula you do it between "" because its a literal string which will be outputed to an excel cell. To use variables you need to write them out of the quotes.
i.e. "=VLOOKUP(" & element & "," & f_range & "," & col_num & "," true_false & ")"
Don't forget the commas because they are aswell on the formula. & is a concatenate operator, it will put together everything you are joining with it.
There is another way if you are willing to have values instead the formulas on your cells:
ActiveCell.Value = Application.VLOOKUP(element, f_range, col_num, true_false)
This will calculate the value and put it to your cell. No need to concatenate anything, just give it the parameters as variables.
With the help of teylyn's answer in my previous question, the lowest value in the row are successfully highlighted. How to get the column letter to be displayed in the last column?
FROM
TO
=Left(if($C4=Min($C4:$I4),$C$3&" & ","")&if($E4=Min($C4:$I4),$E$3&" & ","")&if($G4=Min($C4:$I4),$G$3&" & ","")&if($I4=Min($C4:$I4),$I$3&" & ",""),LEN(if($C4=Min($C4:$I4),$C$3&" & ","")&if($E4=Min($C4:$I4),$E$3&" & ","")&if($G4=Min($C4:$I4),$G$3&" & ","")&if($I4=Min($C4:$I4),$I$3&" & ",""))-3)
Its ugly...really ugly. But it does work. Put that monstrosity in J4 and copy down.
where to begin with the description, I thought ugly and monstrosity covered it! Basically what the whole formula is doing is checking each number in the row to detemine if it is the minimum in the row. When that condition is true, it returns the value in the header row for that number and tacks on a " & " to it. It then moves on to the next column and performs the same check and adds the results to the previous column. When a number is not the minimum it adds "" to the results which is nothing. After going through all the results and building up a string that will end in "&" we pull the left side of the string, by the length of the string minus 3 characters to remove that last " & ".
Proof of concept
Like said in the comments of Forward Ed's answer, you could shorten the LEN-part with COUNTIF which would look like this:
=LEFT(IF($C4=MIN($C4:$I4),$C$3&" & ","")&IF($E4=MIN($C4:$I4),$E$3&" & ","")&IF($G4=MIN($C4:$I4),$G$3&" & ","")&IF($I4=MIN($C4:$I4),$I$3&" & ",""),COUNTIF($C4:$I4,MIN($C4:$I4))*4-3)
Still I would prefer the MID solution to (partially) skip any check of length like this:
=MID(IF($C4=MIN($C4:$I4)," & "&$C$3,"")&IF($E4=MIN($C4:$I4)," & "&$E$3,"")&IF($G4=MIN($C4:$I4)," & "&$G$3,"")&IF($I4=MIN($C4:$I4)," & "&$I$3,""),4,20)
The way it works should be pretty obvious. However, if there are any questions left, just ask :)
I'm having trouble with the usage of Indirect function.
Here's what i'm looking for, I'm trying to create a dynamic vlookup based on the current tab.
=VLOOKUP(B3;'NH BBC'!$E$1:$Z$188;MATCH("Share Outstanding";'NH BBC'!$E$1:$Z$1;0);0)
My plan is to modify the 'NH BBC' by 'NH ' & RIGHT(CELL("filename");3) Supposing that the name of my tab is XXX_BBC.
I've tried to use indirect function but I'm not sure I'm on the good way.
Here's what I've tried:
=VLOOKUP(B3;INDIRECT("'" "NH " & "RIGHT(CELL("'" & "filename" & "'" & ");3)" & "!" & "E1:Z188");MATCH("Share Outstanding";'NH BBC'!$E$1:$Z$1;0);0)
Hope I've been clear.
Thanks in advance !
You are trying to concatenate some text with the results returned from a formula, but you are sticking the formulas in quotes, turning them into text. Furthermore, you are not keeping very good track of your text. There are quotes all over the place. Take this bit by bit in a seperate cell if need, slowly growing your formula from the inside out so you can insure everything is as expected. Right now it's a mess.
INDIRECT("'" "NH " & "RIGHT(CELL("'" & "filename" & "'" & ");3)" & "!" & "E1:Z188")
Should be:
INDIRECT("'NH " & RIGHT(CELL("filename");3) & "'!E1:Z188")
No need for all the complication.
I've finally found and this formula is working perfectly.
VLOOKUP($B3;INDIRECT("'NH "&RIGHT(CELL("filename");3)&"'!$G$1:$ZZ$9999");MATCH("SHARE_OUTSTANDING";INDIRECT("'NH "&RIGHT(CELL("filename");3)&"'!$G$1:$ZZ$1");0))
By the way the issue i've got is that the cell are changing when i'm using the formula in another tab. Is this possible to look the value i've obtained ?
Something like a F9 ?