Count number of comma separated values, divide next cell by that number? - excel

I have a table in which one column occasionally contains a sequence of numbers separated by commas (Units). I'd like to find a script that will count the amount of values in that cell (Values), then divide the adjacent cell by that amount, ultimately replacing the value of that cell (NeedResult, replacing Values).
I've played around with formulas, but my VBA skills are rudimentary... Any help mucho appreciated!
table image here

Unless you really want it, I'm not sure you need a VBA solution for this. The calculation can be done using Excel's formulas: simply subtract the length of the string from the length of it with commas removed. Your cell formula could look something like this:
=C3/(LEN(B3)-LEN(SUBSTITUTE(B3,",",""))+1)

Split the first cell value on the commas and adjust the ubound dimension of the resulting array.
dim i as long
i = ubound(split(range("b2").value, chr(44))) + 1
range("d2") = cdbl(range("c2").value / i)
Without the compiler directive Option Base 1, a split array is zero-based.

You could use a formula as well
In D2 entered as array formula with Ctrl+Shift+Enter
=IF(ISBLANK(B2),"",C2/COUNT(FILTERXML("<t><s>" & SUBSTITUTE(B2,CHAR(44), "</s><s>") & "</s></t>", "//s")))

Related

Count the number of existence of a specific number

I have multiple cells which have number separated by columns and want to count the number of existence of a specific number. How may I count the number. what will be the formula.
I have data like in picture and want to count the existence of 1 or any other number in all highlighted cell. I used the countif formula to count the number 1 but it also counts it for 11 or 12.
If anyone have any Solution.
Thanks
enter image description here
I would try and restructure your data so it's easier to work with. Otherwise, here's a dirty solution:
You could first add a comma before and after all your cells with "," & cell & "," so all numbers are separated by a comma, including the last one. Then use COUNTIF to count numbers, by looking for eg. *,1,* or *,12,*:
Orginal
="," & A1 &","
=COUNTIF([your cell range];"*,1,*")
1,2,10,11,12
,1,2,10,11,12,
4
1,2,10,11,12
,1,2,10,11,12,
1,2,10,11,12
,1,2,10,11,12,
1,2,10,11,12
,1,2,10,11,12,

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

match the number from comma separated value and count

I have a comma separated value in A2 and same numbers in different cells B1, C1, D1.... I want to match them from comma separated value and find out the count in B2, C2, D2. Please see the image attached you will get the context.
Can we achieve this by formula or macro in excel?
Tried formula:
=LEN(TRIM($A$2))-LEN(SUBSTITUTE(TRIM($A$2),C1,","""))
Also, I have two data sets where I will be using this formula to find out the count of number from comma-separated value and based on count I want the repeated ones to come in a different cell please refer the image for better understanding.
Probably not the best solution but get the job done. Please note it is case-sensitive and please make sure to press Ctrl+Shift+Enter upon finishing this formula.
{=SUM(--(EXACT("!"&TRIM(MID(SUBSTITUTE($A2,",",REPT(" ",100)),(ROW(INDIRECT("1:"&LEN($A2)))-1)*100+1,100)),"!"&B1)))}
You can replace ! in the above formula with a unique symbol that will never appear in the text string to be safer.
The logic is to SUBSTITUTE the comma , with and long string of blanks, then use MID to find each value in the text string and return the result as an array, then use EXACT to match each value in the array with the look up value and return a new array of TRUE and FALSE, then SUM up all TRUE which will give the count of the look up value.
UPDATE #2
As requested by OP, here is one way of solving the second query which is to match the same value with the same occurrence from two text strings separated by comma ,.
The formula in Cell C2 is from the original solution which is used to find the occurrence of a given value in a text string;
The formula for Range C6:K6 is an array formula as shown below. I used a helper row to layout the matching values, and excluding the one that has 0 count for both data set;
{=IFERROR(INDEX($C$1:$K$1,,AGGREGATE(15,7,COLUMN(INDIRECT("1:"&COLUMNS($C$1:$K$1)))/($C$2:$K$2=$C$3:$K$3)/($C$2:$K$2>0),COLUMN()-2))&",","")}
The formula in Cell L8 is concatenating all values from Range C6:K6 and remove the last comma , from the final text string:
=LEFT(CONCATENATE(C6,D6,E6,F6,G6,H6,I6,J6,K6),LEN(CONCATENATE(C6,D6,E6,F6,G6,H6,I6,J6,K6))-1)
The following worked for me, give it a try:
Formula in B2:
=(LEN(","&SUBSTITUTE($A$2,",",",,")&",")-LEN(SUBSTITUTE(","&SUBSTITUTE($A$2,",",",,")&",",","&B$1&",","")))/LEN(","&B$1&",")
Drag right...
A simpler way of doing it is to simply calculate the difference in the length of the string minus the length of the string when replacing the value searched by nothing and dividing by the length of the string searched
The formula would be:
=(LEN($A$1)+1-LEN(SUBSTITUTE($A$1&",",B1&",","")))/LEN(B1&",")
There is a much simpler solution:
=COUNTIF(SPLIT($A$2, ","), B1)

Vlookup in VBA to work on Numbers in different format

Is there any way to make Vlookup in VBA work when 1 value is a Number and the other is a Number as Text format? I do not want to convert the format of any number.
The problem is I sometimes have reports from SAP and there the text formatted numbers are given. But i need to apply Vlookup in VBA and match in my table where numbers are number formatted. I can't convert all numbers to a number format as it takes time and I don't want to slow my macro.
It is possible with an array formula.
I assume the table from A2 to B100 and the number to match at A1
=VLOOKUP(A1,IFERROR(A2:B100*1,A2:B100),2)
This is an array formula and must be confirmed with ctrl+shift+enter.
If you are working with bigger ranges, it will be better to change to INDEX and MATCH like:
=INDEX(B2:B100,MATCH(A1,IFERROR(A2:A100*1,A2:A100),0))
This is an array formula and must be confirmed with ctrl+shift+enter.
However: if the ranges are very big, or by using whole columns, you will slow down excel a lot.
Is there any way to make Vlookup in VBA work when 1 value is a Number and the other is a Number as Text format?
An INDEX/MATCH function pair is likely the most effective method of lookup. Using IFERROR to pass the processing to an alternate MATCH seems viable.
'if the lookup 'numbers' are predominantly numeric
=index(sheet2!a:a, iferror(match(a2, sheet2!b:b, 0), match(text(a2, "0"), sheet2!b:b, 0))
'if the lookup 'numbers' are predominantly textual
=index(sheet2!a:a, iferror(match(text(a2, "0"), sheet2!b:b, 0), match(a2, sheet2!b:b, 0))
I can't convert all numbers to a number format as it takes time and I don't want to slow my macro.
A Text-to-Columns, Fixed Width, Finish converts full columns of text-that-look-like-numbers to true numbers very quickly.
Your question has no sample data or code. Please supply sample setups in future.
I post this answer as a possible simpler answer than the other two IF the text formatted numbers are the one that you are supplying as the lookup value to the Vlookup function.
Worksheet Function:
=VLOOKUP(VALUE(A1), LookupArray, 2, FALSE)
You mention in 'Vlookup in VBA' and macros, so VBA code:
retVal = Application.VLookup(CLng(lookupVal), LookupRange, 2, False)
I faced the same issue and below helped (I had SAP IDs in column 'B', replace the cell values and last row within range), this converts the SAP ID/number from General/Text/green arrowed format to numbers/values, which then can be looked up with numbers
LR1 = Sheet1.Range("B" & Rows.Count).End(xlUp).Row
With Range("B3:B" & LR1)
.Value = Evaluate(.Address & "*1")
End With
Let me know if it helped!!!

How to combine specific letters and numbers from different cells in Excel?

I have 3 names that I want to combine to create something unique - I've used "CONCATENATE", but to no avail.
Example:
Paper (will always be different)
000001 (This will be sequential, the next row will have 000002)
Plastic (will always be different)
Essentially I want to input all three items in different cells and have the fourth cell output the following:
Pa000001Pl
Thank you.
Using & between objects is an alternative way to concatenate in excel:
=LEFT(A1,2) & B1 & LEFT(C1,2)
See http://fiveminutelessons.com/learn-microsoft-excel/extract-text-cell-excel for info on how to extract text from cells. (Examples: LEFT(), RIGHT(), MID(),FIND().
See https://support.office.com/en-za/article/Combine-the-contents-of-multiple-cells-3a86c317-6b91-4f1d-8781-203320aefdce for info on how to combine text from cells.
If you want to keep the 00000's in the number you need to make sure to format the cells containing the numbers as Text.
You could do this using the TEXT() function on the fly:
=LEFT(A1,2) & TEXT(B1,"000000") & LEFT(C1,2)
The six 0's tell Excel to create a number 6 digits long and replace any 0 with a non-zero number from your cell. So if B1 is 1, TEXT(B1,"000000") will convert it to 000001. If B11 is 11, TEXT(B11,"000000") will convert it to 000011.
If you instead want to ensure that the same number of 0's remain in front of your non-zero numbers, you could use the CONCATENATE() function:
=LEFT(A1,2) & CONCATENATE(B1,"00000") & LEFT(C1,2)
This would always insert five 0's in front of whatever number is listed in B1.
With values in A1, B1, and C1, in another cell enter:
=LEFT(A1,2)&TEXT(B1,"00000")&LEFT(C1,2)
This is needed to preserve the leading zeros if those leading zeros are the result of formatting cell B1
Assuming your values are in the 2nd Row
=LEFT(A2,2) & B2 & LEFT(C2,2)

Resources