简体   繁体   English

EXCEL:使用vba或公式从细胞中提取Milliletres和Lite

[英]EXCEL: Extracting Milliletres and Litres from cell using vba or formula

I am trying to extract the Milliliters and Liters from text(Only the value, not the ml or Ltr), but as there is no specific standard, it is difficult to do a formula. 我试图从文本中提取Milliliters和Lite(只有值,而不是ml或Ltr),但由于没有特定的标准,因此很难做出公式。 Below is some examples of the types of descriptions I have: 以下是我所拥有的描述类型的一些示例:

- Settlement 2.5% (Settlement Discount)
- BAGDOL/1 (U-POL DOLPHIN GLAZE Brushable Stopper Bag 440ml)
- P38/5 (ISOPON P38 LIGHTWEIGHT FILLER 3Ltr)
- EGC21TT (EUROPEAN COATINGS PRIMER 4:1 HS 4Ltr TRIPLE TIGHT)
- RLT/1KIT (U-POL RAPTOR TINTABLE 750ml & 250ml STANDARD HARDENER KIT)
- CCWP/AL (U-POL CUSTOM CAN Pregassed Aerosol 400ml (Waterbased))

I have used the formula below which I feel I have over-complicated.: 我使用下面的公式,我觉得我过于复杂:

=IF(LEN(IFERROR(MID(G2,FIND("ml",G2)-3,LEN(G2)),""))=0,IFERROR(SUBSTITUTE(MID(G2,FIND("Ltr",G2)-2,LEN(G2)),"Ltr)","")*1000,""),IFERROR(SUBSTITUTE(MID(G2,FIND("ml",G2)-3,LEN(G2)),"ml)",""),""))

So those with 440ml returns 440 and those that say 3Ltr return 3000 - But this is only if it is at the end of the cells text. 那些440ml的人返回440,而那些说3Ltr的人返回3000 - 但这只是在它位于单元格文本的末尾。

I want to be able to extract the value of the ml or Ltr no matter where it is in the text. 我想能够提取ml或Ltr的值,无论它在文本中的位置。 Those that have more than one set of ml value in it, I want to sum them. 那些有超过一组毫升值的人,我想总结一下。 Example : 750ml & 250ml should be 1000. Settlement 2.5% (Settlement Discount) should return 0. 例如: 750ml & 250ml应为1000. Settlement 2.5% (Settlement Discount)应返回0。

Is there a simple way of doing this ? 有一个简单的方法吗? Id be happier with doing it in VBA, as I think this would be less complicated. 我很高兴在VBA中这样做,因为我认为这不会那么复杂。

Any help would be appreciated! 任何帮助,将不胜感激!

There is probably a far better way of doing this using RegEx, but I'm not practiced in those dark arts so I'd use something like the following to get what you need: 使用RegEx可能有更好的方法,但是我没有在那些黑暗的艺术中练习,所以我会使用类似下面的内容来获得你需要的东西:

Function getvolume(txt As String) As Long
    Dim wrd As Variant, wrds As Variant
    getvolume = 0
    If txt = "" Then Exit Function
    ' strip out characters that will confuse
    txt = Replace(txt, "(", " ")
    txt = Replace(txt, ")", " ")
    ' convert all to lower case
    txt = LCase(txt)
    ' split the text into individual "words"
    wrds = Split(txt, " ")
    For Each wrd In wrds
        If wrd Like "*ml" Then getvolume = getvolume + Val(Replace(wrd, "ml", ""))
        If wrd Like "*ltr" Then getvolume = getvolume + 1000 * Val(Replace(wrd, "ltr", ""))
    Next
End Function

So if your text is in G2 then you'd use a formula of =getvolume(G2) 因此,如果您的文本在G2那么您将使用公式=getvolume(G2)

User Defined Function 用户定义的函数

Option Explicit

Function metricMeasure(str As String)
    Dim n As Long, unit As String, nums() As Variant
    Static rgx As Object, cmat As Object

    'with rgx as static, it only has to be created once; beneficial when filling a long column with this UDF
    If rgx Is Nothing Then
        Set rgx = CreateObject("VBScript.RegExp")
    End If
    metricMeasure = vbNullString

    With rgx
        .Global = True
        .MultiLine = False
        .Pattern = "[0-9]{1,4}m?[lL]t?r?"
        If .Test(str) Then
            Set cmat = .Execute(str)
            'resize the nums array to accept the matches
            ReDim nums(cmat.Count - 1)
            'get measurement unit
            unit = Replace(cmat.Item(0), Val(cmat.Item(0)), vbNullString, 1, vbTextCompare)
            'populate the nums array with the matches
            For n = LBound(nums) To UBound(nums)
                nums(n) = Val(cmat.Item(n))
            Next n
            'convert the nums array to a subtotal with unit suffix
            metricMeasure = Application.Sum(nums) & unit
        End If
    End With
End Function

在此输入图像描述

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

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