简体   繁体   English

Excel 2007在文本字符串中找到最大的数字

[英]Excel 2007 find the largest number in text string

I have Excel 2007. I am trying to find the largest number in a cell that contains something like the following: 我有Excel2007。我试图在包含以下内容的单元格中找到最大的数字:

[[ E:\\DATA\\SQL\\SY0\\ , 19198 ],[ E:\\ , 18872 ],[ E:\\DATA\\SQL\\ST0\\ , 26211 ],[ E:\\DATA\\SQL\\ST1\\ , 26211 ],[ E:\\DATA\\SQL\\SD0\\ , 9861 ],[ E:\\DATA\\SQL\\SD1\\ , 11220 ],[ E:\\DATA\\SQL\\SL0\\ , 3377 ],[ E:\\DATA\\SQL\\SL1\\ , 1707 ],[ E:\\DATA\\SQL_Support\\SS0\\ , 14375 ],[ E:\\DATA\\SQL_Support\\SS1\\ , 30711 ]] [[E:\\ DATA \\ SQL \\ SY0 \\,19198],[E:\\,18872],[E:\\ DATA \\ SQL \\ ST0 \\,26211],[E:\\ DATA \\ SQL \\ ST1 \\,26211] ,[E:\\ DATA \\ SQL \\ SD0 \\,9861],[E:\\ DATA \\ SQL \\ SD1 \\,11220],[E:\\ DATA \\ SQL \\ SL0 \\,3377],[E:\\ DATA \\ SQL \\ SL1 \\,1707],[E:\\ DATA \\ SQL_Support \\ SS0 \\,14375],[E:\\ DATA \\ SQL_Support \\ SS1 \\,30711]]

I am not a coder but I can get by with some basic instructions. 我不是编码人员,但是我可以通过一些基本说明来解决。 If there is a formula that can do this, great! 如果有一个公式可以做到这一点,那就太好了! If the best way to do this is some sort of backend code, just let me know. 如果最好的方法是某种后端代码,请告诉我。 Thank you for your time. 感谢您的时间。

I do have the following formula that almost gets me there: 我的确有以下公式可以将我带到那里:

=SUMPRODUCT(MID(0&A2,LARGE(INDEX(ISNUMBER(--MID(A2,ROW(INDIRECT("1:"&LEN($A$2))),1))*ROW(INDIRECT("1:"&LEN($A$2))),0),ROW(INDIRECT("1:"&LEN($A$2))))+1,1)*10^ROW(INDIRECT("1:"&LEN($A$2)))/10)

With a cell that contains a string like above, it will work. 对于包含上述字符串的单元格,它将起作用。 However, with a string that contains something like: 但是,使用包含以下内容的字符串:

[[ E:\\DATA\\SQL\\SY0\\ , 19198.934678 ],[ E:\\ , 18872.2567 ]] [[E:\\ DATA \\ SQL \\ SY0 \\,19198.934678],[E:\\,18872.2567]]

I would end up with the value of 19198934678 as the largest value. 我最终将19198934678的值作为最大值。

If there is always a space before and after, you can use this formula. 如果前后始终都有空格,则可以使用此公式。 The formula is an array formula and must be confirmed by holding down ctrl + shift while hitting enter 该公式是一个数组公式,必须在按下Enter键的同时按住ctrl + shift来确认

With your string in A1: 将您的字符串放在A1中:

=MAX(IFERROR(--TRIM(MID(SUBSTITUTE(A1," ",REPT(" ",99)),IF(seq=1,1,(seq-1)*99),99)),0))

seq is a defined name that refers to: seq是定义的名称,它表示:

=ROW(INDEX(Sheet1!$1:$65536,1,1):INDEX(Sheet1!$1:$65536,255,1))

If a VBA UDF is preferable, I suggest the following. 如果最好使用VBA UDF,则建议以下。 The Regex will match anything that might be a number. 正则表达式将匹配可能是数字的任何内容。 The number is expected to be in the format of iiii.dddd The integer part and the decimal point are both optional. 该数字应采用iiii.dddd的格式。整数部分和小数点均为可选。

Option Explicit
Function LargestNumberFromString(S As String) As Double
    Dim RE As Object, MC As Object, M As Object
    Dim D As Double
Set RE = CreateObject("vbscript.regexp")
With RE
    .Global = True
    .Pattern = "\b[0-9]*\.?[0-9]+\b"
    If RE.test(S) = True Then
        For Each M In MC
            D = IIf(D > CDbl(M), D, CDbl(M))
        Next M
    End If
End With
End Function

This is going to be a rather complex answer for someone with no programming background, so be prepared to spend a lot of time covering and researching this topic if you really wish to achieve a function that finds the largest number in a string in excel. 对于没有编程背景的人来说,这将是一个相当复杂的答案,因此,如果您确实希望实现在excel中找到字符串中最大数字的函数,请准备花费大量时间来研究该主题。

The solution, requires the use of VBA and Regular expressions VBA is used in excel when there is a need for more complex functionality that just can't be achieved with the use of built in spreadsheet functions. 该解决方案需要使用VBA正则表达式 ,当需要使用内置电子表格功能无法实现的更复杂的功能时,在excel中使用VBA。 Regular expressions are a language used to tell programs how to extract useful information from texts, in this case we can extract all the numbers in your text. 正则表达式是一种用来告诉程序如何从文本中提取有用信息的语言,在这种情况下,我们可以提取文本中的所有数字。 with the following regular expression. 具有以下正则表达式。

(\\d+.?\\d*)/g (\\ d +。?\\ d *)/ g

Which roughly means: Match one or more digits with an optional period and subsequent optional digits. 这大致意味着:将一个或多个数字与一个可选的句点和随后的可选数字匹配。 The program that will interpret this will do the following: Look for digits, if you see one, then that's a match, grab all contiguous digits and add them to the match. 将对此进行解释的程序将执行以下操作:查找数字,如果看到一个数字,则表示匹配,抓住所有连续的数字并将其添加到匹配中。 Once you find a character that is not a digit, start looking for new matches. 一旦找到不是数字的字符,就开始寻找新的匹配项。 if at any point you find a dot, add it to the match, but just once, and keep on looking for digits. 如果您在任何时候找到一个点,请将其添加到匹配项中,但只需添加一次,然后继续寻找数字。 Rinse and repeat until the end of the text. 冲洗并重复直到文本结尾。

You can test it here. 您可以在这里进行测试。 In this case, the regex matches 19 numbers. 在这种情况下,正则表达式匹配19个数字。

http://www.regextester.com/ http://www.regextester.com/

Once you have a collection with the 19 matches (See link to regular expressions), all you would need to do is to loop over each of the matches to find out which of the numbers is the highest: 一旦有了一个包含19个匹配项的集合(请参见正则表达式的链接),您要做的就是遍历每个匹配项以找出哪个数字最高:

for each number in matches
    if number > highestNumber then
        highestNumber = number
    end if
next

And highestNumber will be the the result! 和maximumNumber将是结果! In order to have this code run in a simple custom function, you can follow this microsoft tutorial ( https://support.office.com/en-us/article/Create-Custom-Functions-in-Excel-2007-2f06c10b-3622-40d6-a1b2-b6748ae8231f?ui=en-US&rs=en-US&ad=US&fromAR=1 ) 为了使此代码在简单的自定义函数中运行,您可以按照此Microsoft教程( https://support.office.com/en-us/article/Create-Custom-Functions-in-Excel-2007-2f06c10b- 3622-40d6-a1b2-b6748ae8231f?ui = zh-CN&rs = zh-CN&ad = US&fromAR = 1

You can use this UDF: 您可以使用以下UDF:

Function MaxInString(rng As String) As Double
Dim splt() As String
Dim i&

splt = Split(rng)
For i = LBound(splt) To UBound(splt)
    If IsNumeric(splt(i)) Then
        If splt(i) > MaxInString Then
            MaxInString = splt(i)
        End If
    End If
Next i
End Function

Put this in a module attached to the workbook. 将其放在工作簿附带的模块中。 NOT in the worksheet or ThisWorkbook code. 不要在表或ThisWorkbook代码。

Then you can call it like any other formula: 然后,您可以像其他任何公式一样调用它:

=MaxInString(A1) = MaxInString(A1)

在此处输入图片说明

Where c is your string to find max from 其中c是您要从中找到最大值的字符串

Dim qwe() As String
qwe = Split(c, ", ")
maxed = 0
For x = LBound(qwe) To UBound(qwe)
    qwe(x) = Left(qwe(x), InStr(1, qwe(x), " ", vbBinaryCompare))
    On Error Resume Next
    If CLng(qwe(x)) > maxed Then maxed = CLng(qwe(x))
Next x
    MsgBox maxed

The error line is there to ignore when qwe(x) cannot be converted to a LONG number. 当qwe(x)无法转换为LONG数时,错误行将被忽略。

I must say this is very specific to your string format, for a more comprehensive doodad you'd want to have the split delimiter as a variable and possibly use the "IsNumeric" function to scan the entire string. 我必须说这是非常特定于您的字符串格式的,对于更全面的doodad,您希望将分隔定界符作为变量,并可能使用“ IsNumeric”函数扫描整个字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM