Excel counting seperate values in the same cell - excel

I have a excel sheet in which I need to count diffrent values from one cell.
The ones and zeros vary as well as the amount of <br> that seperated them. I need to count the ones and zeros seperatly. 10000010011<br>10101010101<br>01100111000<br>101101010110 In this example I need four values indicating the amount of ones and zeros. Here it would be: zeros1:7 ones1:4, zeros2: 5 ones2:6, zeros3: 6 ones3: 5, zeros4: 5 ones4: 7. I really would appreciate any suggestions!

Assuming each binary is always 11 digits:
Parse your data Text to Columns/Fixed width with breaks around the angled brackets, then skip the <br> columns and apply formulae such as:
=LEN(A1)-LEN(SUBSTITUTE(A1,1,""))
copied across to suit and repeat with ,0, in place of ,1, or just subtract the above results from 11.

If a VBA answer is ok you could use this function:
With 10000010011<br>10101010101<br>01100111000<br>101101010110 in cell A1 the formula =Count1And0(A1) will return 4:7, 6:5, 5:6, 7:5
Public Function Count1And0(Target As Range) As String
Dim vSplit As Variant
Dim vNum As Variant
Dim sFinal As String
Dim lCount As Long
vSplit = Split(Target, "<br>")
For Each vNum In vSplit
lCount = Len(vNum) - Len(Replace(vNum, "0", ""))
sFinal = sFinal & Len(vNum) - lCount & ":" & lCount & ", "
Next vNum
Count1And0 = Left(sFinal, Len(sFinal) - 2)
End Function

You can use the LEFT and RIGHT functions in combination with SEARCH to split the cell by "<br>". You can put them in a row (into multiple cells), so that the next one takes output of the previous one as the input.
So A1 would be your input and then put =RIGHT(A1,LEN(A1)-FIND("<br>",A1,1) - 3) into A2 (this will remove everything before first "<br>"). Then put the same formula, but with A2 instead of A1 everywhere (that will remove everything before second "<br>"), and so on.
More info: https://support.office.com/en-us/article/split-text-into-different-columns-with-functions-49ec57f9-3d5a-44b2-82da-50dded6e4a68

Dear all thanks for your suggestions! Basically I have deceided to it the simple but more lengthly way in splitting the cell (text in columns).

Related

Extract content of a single cell and separate and rename accordingly?

So I have this large excel file, over 7000 rows, and what I need to do is pick the content from a cell in a column, for example A2, and extract its content onto A3
The thing is that the content are abbreviatures, like PRD for period, or CLS for class, all abbreviature are separated by underscores with no particular order, so for example A2 can just say PRD but B2 would say PRD_CLS_PPRD_ADVAN and then back to CLS_ADV on C2
What I need is to extract the content from each cell an put it on another cell, the abbreviatures must be replaced by it's original word so instead of PRD it should say Period, or Class instead of CLS, when there's an underscore it should be replaced for a slash. So overall when B2 says PRD_CLS_PPRD_ADVAN then B3 should say Period/Class/Pre-Production/Advance
I've given it a lot of tries to solve this, using LEFT, RIGHT, EXTRACT, but to no avail, would appreciate any suggestion on how to solve this
So, slightly different approach using find():
Formula for C2:
IFERROR(IF(FIND($E$2,$A2,1)>0,VLOOKUP($E$2,$E$2:$F$5,2,0),"")&"/","")&IFERROR(IF(FIND($E$3,$A2,1)>0,VLOOKUP($E$3,$E$2:$F$5,2,0),"")&"/","")&IFERROR(IF(FIND($E$4,$A2,1)>0,VLOOKUP($E$4,$E$2:$F$5,2,0),"")&"/","")&IFERROR(IF(FIND($E$5,$A2,1)>0,VLOOKUP($E$5,$E$2:$F$5,2,0),""),"")
Let you think about the trailing "/"...
'something like that should get you started
Public Sub convertCell()
Dim rowmax As Integer
Dim i As Integer
Dim j As Integer
Dim StrArray() As String
Dim wrds As Integer
'replace sheet1 by the name of you sheets
rowmax = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To rowmax
StrArray = Split(Worksheets("Sheet1").Cells(i, 1), "_")
wrds = UBound(StrArray)
For j = 0 To wrds
Worksheets("Sheet1").Cells(i, 2) = Worksheets("Sheet1").Cells(i, 2) & "/" & StrArray(j)
Next j
Next i
End Sub
So basic method using formulae:
Formulae in C2:
VLOOKUP(LEFT(A2,FIND("_",A2,1)-1),$E$2:$F$5,2,0)
Cells E2 to F5 hold the abbreviations, extend the range as needed.
So, update, been playing.
Just done the first two, use mid() for number 3 and right for number 4.

Excel extracting text into columns

Is there a way to extract data from a single cell and split it into columns by headers. For example we have in A1 cell text like this:
Name: John
Address: USA, New York
Age: 66
I want to split this text into columns with headers Name, Address, Age and extract data to the following columns. I'd be grateful for tips.
This is little bit tricky but will work on all version of excel. As per below screenshot Put Name, Address, Age to B1,C1 & D1 cell then put below formula to B2 cell then drag down and right as needed.
=SUBSTITUTE(TRIM(MID(SUBSTITUTE($A2,CHAR(10),REPT(" ",100)),((COLUMN(A$2)-1)*100)+1,COLUMN($A$2)*100)),B1&": ","")
If you wouldn't mind using formulas instead of VBA:
With Excel O365:
Formula in B2:
=TRANSPOSE(TRIM(FILTERXML("<t><s>"&SUBSTITUTE(SUBSTITUTE(A2,":",CHAR(10)),CHAR(10),"</s><s>")&"</s></t>","//s[position() mod 2 = 0]")))
With Excel 2013 or higher, other than O365:
=INDEX(TRIM(FILTERXML("<t><s>"&SUBSTITUTE(SUBSTITUTE($A2,":",CHAR(10)),CHAR(10),"</s><s>")&"</s></t>","//s[position() mod 2 = 0]")),COLUMN(A1))
And drag over and down...
A VBA array approach
This late post in addition to the valid answers above demonstrates an array approach and a double splitting:
section [1] splits into lines via the vbLf delimiter (equalling Chr(10)),
section [2] restricts splitting to two parts (via ": ")
As it's not so widely known how to use the Split() function by limiting output to 2 tokens only as shown in section [2], have a look at the
Syntax
Split(expression, [ delimiter, [ limit, [ compare ]]])
Option Explicit
Sub SplitIntoTokens()
With Sheet1 ' << change to your project's sheet Code(Name)
'[0] get string content
Dim lastRow As Long
lastRow = .Range("A" & .Rows.Count).End(xlUp).Row
Dim data As Variant
data = .Range("A1:D" & lastRow).Value
Dim i As Long
For i = 2 To UBound(data)
'[1] split into lines
' ~> Name: John|Address: USA, New York|Age: 66
Dim lines: lines = Split(data(i, 1), vbLf)
'[2] split into 2 parts only and take the 2nd one
Dim ii As Long, tmp
For ii = 0 To UBound(lines)
lines(ii) = Split(lines(ii), ": ", 2)(1) ' split via ": "-delimiter, 2nd part via index (1)
data(i, ii + 2) = lines(ii)
Next
'Debug.Print Join(lines, "|") optional (display results in VB Editors Immediate Window
Next
'[3] write array results back to sheet
.Range("A1").Resize(UBound(data), 4) = data ' write data
.Range("A1:D1") = Split("Data,Name,Address,Age", ",") ' write header (if not existant)
End With
End Sub

VBA number formatting (with commas as decimal separator)

The main problem started when I wanted to "convert to number" by the green triangle (I know I can do it by hand, but there are a lot of cells like that and in the future I only want to use code).
So I wanted to do it by code, and I came across with this code that helps, but I have a problem with the number format which removes the decimal numbers.
Sub Valor3()
Dim LastRow As Long, i As Long
LastRow = Sheets("Hoja3").Range("A" & Rows.Count).End(xlUp).Row
'Sheets("Hoja3").Range("A1:A" & LastRow).NumberFormat = "# ##0,00"
For i = 1 To LastRow
If Val(Sheets("Hoja3").Range("A" & i).Value) <> 0 Then _
Sheets("Hoja3").Range("A" & i).Formula = _
Val(Sheets("Hoja3").Range("A" & i).Value)
Next i
End Sub
I've been trying many formats but none of them seems to help.
It might be because here we use the comma as a decimal separator and there is no miles separator.
What number format would help me?
The issue is that you use Val function in combination with a non-us-english decimal separator, which is not a proper solution to your issue.
The Val function recognizes only the period ( .) as a valid decimal separator. When different decimal separators are used, as in international applications, use CDbl instead to convert a string to a number.
Source: Microsoft documentation Val function.
Since the Val function does not convert a text into a value but extracts
The Val function only works with a dot . as decimal separator.
Example:
Val("2.55") 'will return 2.55 as number
Val("2,55") 'will return 2 as number (because it cuts off all text and the comma is not considered as decimal separator)
To get rid of the green triangle and convert a number that is saved as text into a real number properly, use the following:
Option Explicit
Public Sub ConvertNumberAsTextIntoRealNumber()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Hoja3")
Dim LastRow As Long
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
With ws.Range("A1", "A" & LastRow)
.NumberFormat = "# ##0.00" 'set your desired number format
.Value = .Value 'this will in most cases already convert to real numbers.
End With
'But if your numbers are hard coded to text and begin with a `'` you need the following additionally:
Dim iRow As Long
For iRow = 1 To LastRow
With ws.Cells(iRow, "A")
If IsNumeric(.Value) Then 'can the value be interpreted as a number
If .Value <> 0 Then 'is the value not zero
.Value = CDbl(.Value) 'then convert it into a real number
End If
End If
End With
Next iRow
End Sub
I know you are looking for VBA solution, but here's a small Excel trick that you might find useful:
Enter 1 (numeric value) somewhere in the file and copy it:
Select your range (A1:A6) and go to Paste > Paste Special > select Multiply:
The final result is all your text values being converted to numbers:
The same trick will work with other combinations, e.g. Operation: Add while having 0 copied, etc.

How to split a word whereever there's a number using MS Excel Delimiter

So I have a list of combinations as mentioned below which I needed to be split with an alphabet and a number following it.
I want it to be split as below using MS Excel; Text to Columns Delimit function or any other efficient function-
I tried using Fixed Width which doesn't work if there are 2 or more digits numeric value after the alphabetical character. Is there a way to do this as I have to repeat this for a 13000 combinations?
Since the Text are constant, create a header row of the constants and with your values starting in A1, put this formula in B2 and copy over and down:
=MID($A2,FIND(B$1,$A2),IF(ISNUMBER(--MID($A2,FIND(B$1,$A2)+2,1)),3,2))
Insert a delimiter character where ever the pattern is found then split the modified string into new columns.
Option Explicit
Sub makeSplit()
Dim i As Long, j As Long, str As String, arr As Variant
'set the worksheet
With Worksheets("sheet5")
'step throughj the cells in column A
For i = .Cells(.Rows.Count, "A").End(xlUp).Row To 2 Step -1
'put the cell value into a variable
str = .Cells(i, "A").Value2
'step through the characters in the sting backwards
For j = Len(str) - 1 To 3 Step -1
'is the current character alphabetic and the previous character a digit?
If Not IsNumeric(Mid(str, j, 1)) And IsNumeric(Mid(str, j - 1, 1)) Then
'insert a space
str = Left(str, j - 1) & Space(1) & Mid(str, j)
End If
Next j
'split the string using the space as delimiter
.Cells(i, "A").Resize(1, UBound(Split(str, Space(1))) + 1) = Split(str, Space(1))
Next i
End With
End Sub
I found a way but is going to be resource intensive.
First have in a sheet all your strings in a column, and make sure you have columns free right to that column.
Then make a new, empty, first row, and put in the letters R, C, P, D, O, S, since you said they are always in that order. These are the recognition letters, needed for the formula.
Now under each letter, right of each string, put in the formula
=C$1&MAX(IFERROR(VALUE(MID($A2,SEARCH(C$1,$A2)+1,2)),0),VALUE(MID($A2,SEARCH(C$1,$A2)+1,1)),0)
This works only if the numbers are no longer then 2 digits, for three it would be
=C$1&MAX(IFERROR(VALUE(MID($A2,SEARCH(C$1,$A2)+1,3)),0),IFERROR(VALUE(MID($A2,SEARCH(C$1,$A2)+1,2)),0),VALUE(MID($A2,SEARCH(C$1,$A2)+1,1)),0)
For four, keep on adding an IFERROR() with the )),2) )),3) increasing like above.
Once all the formulas have evaluated, you can copy and paste as values if you want to get rid of the formulas.
Example sheet (Dutch Excel):

Find how many words from cell are found in an array

I have two columns with data. The first one has some terms and the other one contains single words.
what I have
I'm looking for a way to identify which words from each cell from the first column appear in the second, so the result should look something like this (I don't need the commas):
what I need
My question is somehow similar to Excel find cells from range where search value is within the cell but not exactly, because I need to identify which words are appearing in the second column and there can be more than one word.
I also tried =INDEX($D$2:$D$7;MATCH(1=1;INDEX(ISNUMBER(SEARCH($D$2:$D$7;A2));0);))
but it also returns only one word.
If you are willing to use VBA, then you can define a user defined function:
Public Function SearchForWords(strTerm As String, rngWords As Range) As String
Dim cstrDelimiter As String: cstrDelimiter = Chr(1) ' A rarely used character
strTerm = cstrDelimiter & Replace(strTerm, " ", cstrDelimiter) & cstrDelimiter ' replace any other possible delimiter here
SearchForWords = vbNullString
Dim varWords As Variant: varWords = rngWords.Value
Dim i As Long: For i = LBound(varWords, 1) To UBound(varWords, 1)
Dim j As Long: For j = LBound(varWords, 2) To UBound(varWords, 2)
If InStr(1, strTerm, cstrDelimiter & varWords(i, j) & cstrDelimiter) <> 0 Then
SearchForWords = SearchForWords & varWords(i, j) & ", "
End If
Next j
Next i
Dim iLeft As Long: iLeft = Len(SearchForWords) - 2
If 0 < iLeft Then
SearchForWords = Left(SearchForWords, Len(SearchForWords) - 2)
End If
End Function
And you can use it from the Excel table like this:
=SearchForWords(A2;$D$2:$D$7)
I have a partial solution:
=IF(1-ISERROR(SEARCH(" "&D2:D7&" "," "&A2&" ")),D2:D7&", ","")
This formula returns an array of the words contained in the cell (ranges are according to your picture). This array is sparse: it contains empty strings for each missing word. And it assumes that words are always separated by one space (this may be improved if necessary).
However, native Excel functions are not capable of concatenating an array, so I think the rest is not possible with native formulas only.
You would need VBA but if you use VBA you should not bother with the first part at all, since you can do anything.
You can create a table with the words you want to find across the top and use a formula populate the cells below each word if it's found. See screenshot.
[edit] I've noticed that it's incorrectly picking up "board" in "blackboard" but that should be easily fixed.
=IFERROR(IF(FIND(C$1,$A2,1)>0,C$1 & ", "),"")
Simply concatinate the results
=CONCATENATE(C2,D2,E2,F2,G2,H2)
or
=LEFT(CONCATENATE(C2,D2,E2,F2,G2,H2),LEN(CONCATENATE(C2,D2,E2,F2,G2,H2))-2)
to take off the last comma and space
I've edited this to fix the problem with "blackboard"
new formula for C2
=IF(OR(C$1=$A2,ISNUMBER(SEARCH(" "&C$1&" ",$A2,1)),C$1 & " "=LEFT($A2,LEN(C$1)+1)," " & C$1=RIGHT($A2,LEN(C$1)+1)),C$1 & ", ","")
New formula for B2 to catch the error if there are no words
=IFERROR(LEFT(CONCATENATE(C2,D2,E2,F2,G2,H2,I2),LEN(CONCATENATE(C2,D2,E2,F2,G2,H2,I2))-2),"")

Resources