Excel Average - Counting the number of variables - excel

I'm using the average function excel to get the average of a series of hotel prices in various European cities.
=average(21,42,63,84,105)
I'd like to be able to count the number of variables in each average function (for example, in the above example there's 5). The data is scrubbed from websites which is why it's in the format above rather than placed into separate cells.
Is there a way to do this without taking out the variables, putting them into a cell and then separating out the cells using Text to Columns?
Thanks!

You can turn your equation into a string using FORMULATEXT() and then deduce the number of values being averaged by counting the instances of commas in your string (which relates to your final answer by Total Commas + 1 = Total Values
The first portion of the equation counts the character length with commas. The second portion counts the character length without commas. The difference is simply the number of commas present. We then add one since your last value is not followed by a comma
=LEN(FORMULATEXT(A1))-LEN(SUBSTITUTE(FORMULATEXT(A1),",",""))+1
Assumes your average formula is in cell A1

I am a fan of using UDFs, so here's an alternate method.
You can create a custom User Defined Function (UDF) for this. Just split the function by the deliminator and get your number from the UBOUND().
Public function getNumArgs(inputRng as range) as long
'First check that you are actually looking at a formula
If Left(inputRng.Formula, 1) <> "=" Then
getNumArgs = False
exit function
End If
getnumargs = ubound(split(inputrng.formula, ",")) + 1
end function
You will add 1 because VBA uses Base 0.
You will then use your custom UDF the same way you do any other worksheet formula:
=getNumArgs(A1)
The largest benefit of using a UDF is that you do not have to remember a complex formula.

Related

Find common text within a range of cells(range containing blanks as well)

This is the problem i am facing in Excel formula
enter image description here
In column F, i want to find the common text across A2 to E2 (containing Blanks)
My Question:
Is there a simple way to get the result without VB?
Any help is appreciated,thanks
I found that google sheets has some really cool functions.
If you put the formula =SPLIT(A1, ",", TRUE,FALSE) in the cell after your row of common text (or probably even in a different sheet - "probably because hadn't tried it, though it should), the next x cells (where x is the number of "," in A1 - because "," is the delimitator) will be the text.
then you can put the code =IF(SUM(ARRAYFORMULA(if(REGEXMATCH($A$1:$D$1,F1),1,0)))=COUNTA($A$1:$D$1),F1,"") into an equal number of cells after that (probably should just put into the max number), and =CONCATENATE(I1:L1) into the last cell.
Ok. So to tweak this for yourself: I found that ARRAYFORMULA lets you put an array in place of a single cell in a function inside. how it exactly works I read its like a for loop. but I can't really vouch for that. but here it lets you have REGEXMATCH (which is a Boolean check on the cell you give it for if it contains the given REGEX) check each cell in the array.
the sum will add them up, and the if will match against the COUNTA to find if the number of cells in the array that contain this string is equal to the number of non-empty cells.
the concatenate at the end adds all the cells (containing the regex function) together, and since the only non-empty cells will be the one with the string, that is what this cell will return (no spaces).
code:
results:
the test data:
If you need in specifically Excel... this won't help.
We can use power query to achieve the desired result.
Unpivot the columns in Power query
Split all the columns by Comma delimiter
Create a custom column to see if the first column records exist in the remaining columns.
Use the functionText.contains.
Sample function: =Text.Contains([column.1],[column.1]&[column.2]&[column.3])
If the above function returns TRUE then get the first column result(This is the expected result) and load the data back to your excel

comparing the content of two cells in VBA

I need to compare the content of two cells in macro
RC1=2.0
RC2=2
I am using if(RC1=RC2,"''","x") for this which prints x for the above input but 2.0 and 2 are same, I need it to be '' in this case, please suggest
Your failing 'code' suggests you're merely using a formula:
if(RC1=RC2,"''","x")
If you are happy using a formula to test them, you could use:
IF(VALUE(RC1)=VALUE(RC2),"''","x")
Be aware though, a blank cell has a VALUE of 0 (zero) so an empty cell would match a cell containing 0.00
First and foremost
You are trying to compare two different data types, let's say consider that one is an integer, which does not have decimal values, and the other one a double, with a decimal.
I would suggest you to have a better look at data types in VBA.
Hence we will need to convert the types to one that is the same for both cells with CDbl() to convert them to double. To put it simply, since integers are a simpler form of doubles we will use the more complete type of both.
Code to compare the types
Sub COMPARING_TYPES()
Dim a As Double, b As Double
' Let's assume the both values are numeric
a = CDbl(Cells(1, 1))
b = CDbl(Cells(2, 1))
If a = b Then
' Other code to execute when the values are matching
End If
End Sub
This is all considering you want to compare the numbers in a VBA code.

How do I return the intersecting value from 2 partial match lookups? Index/Match

I have two tables in an excel worksheet. I'm trying to gather product info from data on another table in the same workbook. The first table is the product data feed I'm building with the product part numbers. Those part numbers include the variables of the product (in this case the length and the width). On the other sheet, I have partial part numbers in the header column and the rough dimensions in the header row. The intersection gives the final dimensions which is the data I'm trying to gather on sheet 1. I've been trying to use and Index/Match formula to solve the problem, but since there are only partial part numbers on the 2nd sheet the lookup is inconclusive. I know the lookup value supports wildcards, but it seems I would need some sort of wildcard search within the lookup array instead.
Example product names on sheet 1 column A "EXP81285-150-11 x 14-Flat"
Example of product names on sheet 2 column A "EXP81285-150"
Example of rough dimensions on sheet 2 row 1 "11 x 14"
Here is what I have so far:
=INDEX('sheet 2'!$A$1:$L$87,MATCH($A3,'sheet 2'!$A:$A,0),MATCH($A3,'sheet 2'!$1:$1,0))
Sheet 1
Sheet 2
Any help is greatly appreciated!
Asuming its always like string1-string2-unused and string2 and unused doesn't contain "-" you can get the first string with:
*updated due to misunderstanding*
=MID(A3,4,FIND("|",SUBSTITUTE(A3,"-","|",LEN(A3)-LEN(SUBSTITUTE(A3,"-",""))-1))-4)
While the string2 one is a hell of a formula:
=MID(A3,FIND("|",SUBSTITUTE(A3,"-","|",LEN(A3)-LEN(SUBSTITUTE(A3,"-",""))-1))+1,FIND("|",SUBSTITUTE(A3,"-","|",LEN(A3)-LEN(SUBSTITUTE(A3,"-",""))))-FIND("|",SUBSTITUTE(A3,"-","|",LEN(A3)-LEN(SUBSTITUTE(A3,"-",""))-1))-1)
Asuming the last part is allways in Q3 then:
=MID(SUBSTITUTE($A3,"-"&$Q3,""),FIND("|",SUBSTITUTE($A3,"-","|",LEN($A3)-LEN(SUBSTITUTE($A3,"-",""))-1))+1,99)
You may also use an arrayformula for the second part like:
=MID(SUBSTITUTE($A3,"-"&$Q3,""),LARGE((MID($A3,ROW($1:$99),1)="-")*ROW($1:$99),2)+1,99)
This is an array formula and must be confirmed with Ctrl+Shift+Enter.
(the second formula may work faster)
You could use array formulas in a reverse Match however... having lots of entrys even one formula will slow down the calculation by ~2-5 seconds.
You better use VBA like:
(in Module)
Public Function MATCH2(str As String, rng As Range) As Long
Dim i As Long, var1 As Variant
i = 0
For Each var1 In rng
i = i + 1
If InStr(str, var1.Value) Then MATCH2 = i: Exit Function
Next
End Function
And then use your formula as followed:
=INDEX('sheet 2'!$A$1:$L$87,MATCH2($A3,'sheet 2'!$A:$A,0),MATCH2($A3,'sheet 2'!$1:$1,0))
EDIT 2015-11-19
OK... some small problems:
some sizes doesnt exist (like 6 x 9)
size 7 x 12 was bugged (a space at the end > fixed it)
the function needs to be in a module (also fixed that)
also some items doesn't exist like 600823-002
a misunderstanding regarding the formulas (doesn't matter at the VBA-version) > all asumed the A:A-searchstring starts at the 1st character (but it is the 4th, no EXP)
Also there will be an error at each "header" (the ones without the * x * but that should be ok)
You can download the updated workbook here
If you still have questions, just ask :)
Here is one using vlookup:
=VLOOKUP(LEFT(A2,FIND("-",A2,10)-1),Sheet2!A:L,MATCH(MID(A2,FIND("-",A2,10)+1,(FIND("-",A2,15))-(FIND("-",A2,10)+1)),Sheet2!A1:L1,0),FALSE)
But I agree with Dirk, This could be done faster and probably more accurate with vba.
Edit, I realized that my dictating of 10 and 15 in the formula would not work, I have fixed it, but it is based on the part number has 1 and only 1 "-" in the part name. Warning it is quite long.
=VLOOKUP(LEFT(A2,FIND("-",A2,FIND("-",A2,1)+1)-1),Sheet2!A:L,MATCH(MID(A2,FIND("-",A2,FIND("-",A2,1)+1)+1,(FIND("-",A2,FIND("-",A2,FIND("-",A2,FIND("-",A2,1)+1))+1))-(FIND("-",A2,FIND("-",A2,1)+1)+1)),Sheet2!A1:L1,0),FALSE)

using array formula to average if

i have some cells d1:d10. Some have numbers, others contain "". The "" is the result of an =iferror(,"") function to leave a blank cell.
I am trying to average d1:d10 but only including the cells that are not "".
I have =AVERAGE(IF(D12:D51<>"",D12:D51)) followed by ctrl+shft+enter but it is still taking the average of all the cells (essentially taking the sum and dividing by 10, where I want it to take the sum and divide by less than 10 depending on the number of "" cells)
I couldn't reproduce your problem in Excel 2013.
Normally, Excel's average function ignores text, empty cells and logical values. So, the following formula should do what you are trying to do.
=AVERAGE(D1:D10)
The if clause in your function returns either some numbers or FALSE. Again, normally, Excel's average function ignores FALSE values so it shouldn't behave like you said. If it somehow is converting boolean values to numeric values based on Excel's version (FALSE to zero) you can just give a string instead of a boolean value so it has to ignore those values:
=AVERAGE(IF(D1:D10<>"", D1:D10, "s"))
Alternatively you can calculate the average without the average function:
=SUM(IF(D1:D10<>"", D1:D10))/COUNT(IF(D1:D10<>"", D1:D10))

Counting Numbers within a cell

Currently I am trying to figure out the number of purchases within one year. In my QTY purchased cell I have a list of quantities (800+2400+2400+2400+2400+2400+2400). This cell shows 7 different purchases.
My question is, what formula will take this cell and count the number of purchases individually and spit out 7 for the number of purchases, without me having to count them manually??
I have tried using both Len and Count formulas, however, they continue to either count the characters or just the cell itself.
Thanks!
Scott
You should probably structure your worksheet differently, as highlighted by Chris in the comments.
If you want to keep it that way, you can use a user-defined function in a VBA module to retrieve the formula text and then use that to count the number of "+" signs.
Public Function FormulaText(rng As Range)
FormulaText = rng.formula
End Function
A counting formula could be:
=len(formulatext(a1))-len(substitute(formulatext(a1),"+",""))+1

Resources