Excel: Extract text from cell where text is always #.# - excel

I have a bunch of text in cells but many of the cells contain some text in the format of #.# (where # is actually a number from 0-9).
I'm using this formula which works okay, but sometimes there is junk in the cell that causes the formula to return the wrong information.
=MID(B7,(FIND({"."},B7,1)-1),3)
For instance, sometimes a cell contains: "abc (1st. list) testing 8.7 yay". Thus I end up with t. instead of the desired 8.7.
Any ideas?
Thank you!

Here is a User Defined Function that will return a numeric pattern in the string if and only if it matches the pattern you describe. If the pattern you describe is not exactly representative, you'll need to provide a better example:
Option Explicit
Function reValue(S As String)
Dim RE As Object, MC As Object
Set RE = CreateObject("vbscript.regexp")
With RE
.Global = True
.Pattern = "\b\d\.\d\b"
If .test(S) = True Then
Set MC = .Execute(S)
reValue = CDbl(MC(0))
Else
reValue = ""
End If
End With
End Function

Related

Is it possible to find and delete anything between two specified characters in an excel csv cell?

I have a csv file, where images links are added in one cell for one product. I want to remove text form ? to ,
I write this code:
=MID(A2,1,FIND("?",A2)-1)&MID(A2,FIND(",",A2),LEN(A2))
But its applied only on the first image link.
This is what I have:
/images/image1.jpg?1200x800=new, /images/image2.jpg?1200x800=new,/images/image3.jpg?1200x800=new, /images/image5.jpg?1200x800=new
Result I need:
/images/image1.jpg,/images/image2.jpg,/images/image3.jpg,/images/image5.jpg
If your data is in A1:
=TEXTJOIN(",",,LET(x,TEXTSPLIT(A1,,","), y, LEFT(x,FIND("?",x)-1),y))
If you have Excel 2016 or earlier, which lack both the TEXTJOIN function as well as dynamic arrays, I suggest using a VBA routine to produce your desired output.
I used a regex match method to extract each segment, then joined them together. You could use a regex replace method, but since your original data has zero or one spaces after each comma, that would be the case in your result string also, so not as much under your control.
To enter this User Defined Function (UDF), alt-F11 opens the Visual Basic Editor.
Ensure your project is highlighted in the Project Explorer window.
Then, from the top menu, select Insert/Module and
paste the code below into the window that opens.
To use this User Defined Function (UDF), enter a formula like =Images(cell_ref) in some cell.
Option Explicit
Function Images(S As String) As String
Dim RE As Object, MC As Object, M As Object
Dim AL As Object
Set RE = CreateObject("vbscript.regexp")
With RE
.Pattern = "([^\?, ]*)\?"
.MultiLine = True
.Global = True
If .test(S) Then
Set MC = .Execute(S)
Set AL = CreateObject("System.Collections.ArrayList")
For Each M In MC
AL.Add M.submatches(0)
Next M
End If
End With
Images = Join(AL.toarray, ", ")
End Function

What's the best way to keep regex matches in Excel?

I'm working off of the excellent information provided in "How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops", however I'm running into a wall trying to keep the matched expression, rather than the un-matched portion:
"2022-02-14T13:30:00.000Z" converts to "T13:30:00.000Z" instead of "2022-02-14", when the function is used in a spreadsheet. Listed below is the code which was taken from "How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops". I though a negation of the strPattern2 would work, however I'm still having issues. Any help is greatly appreciated.
Function simpleCellRegex(Myrange As Range) As String
Dim regEx As New RegExp
Dim strPattern As String
Dim strPattern2 As String
Dim strInput As String
Dim strReplace As String
Dim strOutput As String
strPattern = "^T{0-9][0-9][:]{0-9][0-9][:]{0-9][0-9][0-9][Z]"
strPattern2 = "^(19|20)\d\d([- /.])(0[1-9]|1[012])\2(0[1-9]|[12][0-9]|3[01])"
If strPattern2 <> "" Then
strInput = Myrange.Value
strReplace = ""
With regEx
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = strPattern2
End With
If regEx.test(strInput) Then
simpleCellRegex = regEx.Replace(strInput, strReplace)
Else
simpleCellRegex = "Not matched"
End If
End If
End Function
Replace is very powerful, but you need to do two things:
Specify all the characters you want to drop, if your regexp is <myregexp>, then change it to ^.*?(<myregexp>).*$ assuming you only have one date occurrence in your string. The parentheses are called a 'capturing group' and you can refer to them later as part of your replacement pattern. The ^ at the beginning and the $ at the end ensure that you will only match one occurrence of your pattern even if Global=True. I noticed you were already using a capturing group as a back-reference - you need to add one to the back-reference number because we added a capturing group. Setting up the pattern this way, the entire string will participate in the match and we will use the capturing groups to preserve what we want to keep.
Change your strReplace="" to strReplace="$1", indicating you want to replace whatever was matched with the contents of capturing group #1.
Here is a screenprint from Excel using my RegexpReplace User Defined Function to process your example with my suggestions:
I had to fix up your time portion regexp because you used curly brackets three times where you meant square, and you left out the seconds part completely. Notice by adjusting where you start and end your capturing group parentheses you can keep or drop the T & Z at either end of the time string.
Also, if your program is being passed system timestamps from a reliable source then they are already well-formed and you don't need those long, long regular expressions to reject March 32. You can code both parts in one as
([-0-9/.]{10,10})T([0-9:.]{12,12})Z and when you want the date part use $1 and when you want the time part use $2.

How to count exact text contain in string [Excel]

I already use these below formula to count exact text contain in string but still formula wrongly counted it. For example, i would like to count "ZIKA" test code in table, the answer should be two. But the formula count ZIKA2 as ZIKA also. How to ignore ZIKA2 from count it?
TEST
HS2, CCAL, EGFR, AFB
ZIKA, AG21
PPB, ZIKA2
ZIKA, AG21
I already try these formulas:
=SUMPRODUCT(--(ISNUMBER(FIND("ZIKA",F:F))))
and also
=COUNTIF(F:F,"ZIKA")
you could count exact zika, and comma-separated vriations
=COUNTIF(F:F,"ZIKA")+COUNTIF(F:F,"ZIKA,*")+COUNTIF(F:F,"*, ZIKA")+COUNTIF(F:F,"*, ZIKA,*")
I assume your data follow this format
xxx, yyy, zzz
space after comma
You may need to split your formula into 3 parts
=COUNTIF(F:F,"ZIKA,*")+COUNTIF(F:F,"*, ZIKA")+COUNTIF(F:F,"ZIKA")
The first part will count those start with ZIKA, second part count those end with ZIKA, last we should count those only with ZIKA
Try this regex, it may need a helpercolumn. I have not tested it that much yet.
Press ALT + F11 to open VBA editor.
Click Insert -> module and copy paste the code below.
Function Regex(Cell, Search)
Dim RE As Object
Set RE = CreateObject("vbscript.regexp")
RE.Pattern = "(\b" & Search & "\b)"
RE.Global = True
RE.IgnoreCase = True
Set Matches = RE.Execute(Cell)
For Each res In Matches
Regex = Regex & "," & res
Next res
Regex = Mid(Regex, 2)
End Function
It will return "ZIKA" if it finds ZIKA in the cell you run it on.
And then you just count the ZIKAs in the helper column.
Updated with a new code that you can change the search in.
Use it with =regex(A1, "ZIKA")

Trying to parse excel string

I am trying to parse a string from teamspeak. I am new to the functions of excel. I have accomplished this with php but I am driving myself nuts excel. This is the string I am trying to parse:
[URL=client://4792/noEto+VRGdhvT9/iV375Ck1ZIfo=~Rizz]Rizz[/URL]
This is what I have accomplished so far:
=TRIM(MID(B22, 15, FIND("=",B22,12) - FIND("//",B22)))
which returns
4792/noEto+VRGdhvT9/iV375Ck1ZIfo=~
I am trying to get it to return:
noEto+VRGdhvT9/iV375Ck1ZIfo=
Any suggestions? I am looked of splitting of strings and the phrasing is just really confusing. Any help would be appriciated.
Paste the URL in A3, then this formula in B3. You can adjust the cell references as needed. It's a lot of nested functions, but it works.
=left(right(A3, len(A3)-find("/",A3,find("//",A3,1)+2)),find("=",right(A3, len(A3)-find("/",A3,find("//",A3,1)+2)),1))
Or you can use a user-defined function in VBA:
Function RegexExtract(myRange As Range) As String
'VBA Editor, menu Tools - References, add reference to Microsoft VBScript Regular Expressions 5.5
Dim regex As New RegExp, allMatches As MatchCollection
With regex
.Global = True
.pattern = "\d+/(.+=)"
End With
Set allMatches = regex.Execute(myRange.Value)
With allMatches
If .Count = 1 Then
RegexExtract = .Item(0).SubMatches(0)
Else
RegexExtract = "N/A"
End If
End With
End Function
Then use it as formula:
=RegexExtract(A1)
I am trying to parse a string
For that:
=MID(A1,20,28)
works.
Now if you have more than one string maybe the others are not of an identical pattern, so the above might not work for them. But in that case if to help you we'd need to know something about the shape of the others wouldn't we.

replace multiple cell value from a group of strings

Hi I want to replace multiple values in one for example :
sunny 91878656 rere
vicky 91864567 gfgf
honey 91941561 ytyt
monika 98887888 hjhj
NOw if I want to replace the following two values together with space:
91941561
98887888
How can I do it ?
I dont want to do simple find and replace as this is just an exmaple I have a list of over 12000 records and the numbers which needs to be replaced are more than 900
the reason i want to replace is they are not valid anymore.
also is it possible to remove whole record like if 91941561 is found whole of the record should be deleted or replaced with space like:
honey 91941561 ytyt
monika 98887888 hjhj
thanks
You may use the Regular expression. Below is a sample code
Sub test()
Dim str_demo As String
str_demo = "monika 98887888 hjhj"
MsgBox getString(str_demo)
End Sub
Function getString(ByVal str As String) As String
Dim objRegEx As Object
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.IgnoreCase = True
objRegEx.Global = True
objRegEx.Pattern = "[a-zA-Z]"
Set allMatches = objRegEx.Execute(str)
For i = 0 To allMatches.Count - 1
result = result & allMatches.Item(i)
Next
getString = result
End Function

Resources