简体   繁体   English

Excel:从文本中提取具有特定格式的数字

[英]Excel: extracting number with specific format out of text

I need to extract a specific number out of multiple cells that contain text and numbers.我需要从包含文本和数字的多个单元格中提取特定数字。 The texts that I need to scan look more or less like that:我需要扫描的文本看起来或多或少是这样的:

"Lorem ipsum dolor sit amet 5, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam 10%, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure 20% dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident 10.54% , sunt in culpa qui officia deserunt mollit anim id est laborum" “Lorem ipsum dolor sat amet 5, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua。Ut enim ad minm veniam 10%, quis nostrud exercitation ullamco labis nisi ut aliquitea %悲中reprehenderit在voluptate velit埃塞cillum dolore欧盟fugiat法无pariatur。Excepteur烧结靶occaecat cupidatat非proident 10.54%,在必须遵守过失魁正式开通了deserunt mollit阿尼姆ID EST laborum”

I need to extract the value in bold, which varies across texts, but is always a percentage, with two digits after the decimal point.我需要以粗体形式提取值,该值因文本而异,但始终是一个百分比,小数点后有两位数字。 There are other numbers and percentages in the text.文中还有其他数字和百分比。 To be specific: I need a way to extract only values with two digits after the decimal point out of text.具体来说:我需要一种方法来仅提取文本中小数点后两位数的值。 Is there any way to achieve that with an excel function?有什么办法可以用excel函数来实现吗?

With normal Excel, I have no idea.使用普通的 Excel,我不知道。 With VBA, you can use an UDF to do this.使用 VBA,您可以使用 UDF 来执行此操作。

This code will get always the last % in text.此代码将始终获得文本中的最后一个 % It will get all digits.它将获得所有数字。 And it will return the % value as string .它会将 % 值作为 string返回。 If you need it as number, then the UDF needs to be modified.如果需要它作为数字,则需要修改 UDF。

Public Function EXTRACT_LAST_PERCENT(ByVal vThisCell As Range) As String

If vThisCell.Count <> 1 Then
    EXTRACT_LAST_PERCENT = "This UDF only works with 1 cell"
    Exit Function
End If


Dim STR As String

STR = vThisCell.Value

Dim MyValues As Variant

MyValues = Split(STR, "%")

Dim i As Byte

'% target will be always the last, according to OP
'so it will be in Ubound-1 always
'we split again using blanks as delimiter

STR = MyValues(UBound(MyValues) - 1)

Erase MyValues

MyValues = Split(STR, " ")

'now % target is in Ubound


EXTRACT_LAST_PERCENT = MyValues(UBound(MyValues))

Erase MyValues

End Function

This is what I get:这就是我得到的:

在此处输入图片说明

Hope you can adapt this to your needs.希望您可以根据自己的需要进行调整。

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

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