Excel VB如何在文本中查找值并返回值

4uqofj5v  于 2023-03-04  发布在  其他
关注(0)|答案(2)|浏览(156)

我遇到了一个问题,我们正在尝试自动生成一个报告,例如在b2列中,我可能有以下字符串。
"(执行100条记录)超过1000 ms,其中200条在目标范围内,800条不在目标范围内。)
我需要获取ms(表示毫秒)附近的数字并将其填充到A1列中,但是字符串可能略有不同,因此我需要几种情况的答案
示例1-我需要在1000ms内返回1000个(执行100个记录),其中200个在目标内,800个不在目标内。
示例2-该示例需要返回152ms,忽略目标中的第一个(ms)(执行100个记录)100小于200ms,然而超过目标700,其中152ms,其中200在目标内,800不在目标内。
示例3-ms有时出现在前面,所以我需要ms200下目标中直接在(执行100条记录)100之后的数字
我在另一个例子中尝试过类似的方法,在VS中的Call一词后获取数字,但在这里提到的其他场景中无法复制
示例代码

Sub Formula_Property_1bracket()
   Dim Lastrow As Integer
Lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row

Range("G2:G" & Lastrow).Select
    ActiveCell.FormulaR1C1 = _
        "=IFERROR(VALUE(MID(RC[1], (FIND(""("",RC[1])+1),FIND("" calls"",RC[1])-(FIND(""("",RC[1])+1))),""ERROR"")"
Range("G2").Select
Selection.AutoFill Destination:=Range("G2:G" & Lastrow), Type:=xlFillDefault
End Sub
wpx232ag

wpx232ag1#

创建“定制UDF”以根据每个示例获取值:

单元格B1中的公式为:=GET_MS(A1)
名为GET_MS的UDF的代码为:

Public Function GET_MS(ByVal rng As Range) As Double
Dim MyString As String

MyString = rng.Value

If InStr(1, MyString, "however", vbTextCompare) > 0 Then
    GET_MS = CDbl(Split(Split(MyString, "over target with ")(1), "ms")(0))
ElseIf InStr(1, MyString, "were within", vbTextCompare) Then
    GET_MS = CDbl(Split(Split(MyString, " ms")(0), "over ")(1))
ElseIf InStr(1, MyString, "in target under", vbTextCompare) Then
    GET_MS = CDbl(Split(MyString, "ms")(1))
Else
    GET_MS = -1 'return zero if there is no match at all!!!
End If

End Function

注意,如果提供的任何示例都不匹配,则返回-1,这意味着您需要更新UDF。
为了提取文本的特定部分,我使用了命令SPLIT。为了检查特定文本是否存在,我使用了INSTR:
Split function
InStr function

8fsztsew

8fsztsew2#

试试这样的方法:

Sub test()
    
    On Error GoTo myEnd
    'check if in string is "ms"
    'i tested only one specific cell where i changed your three posibilities of string
    'you have to chenge it to dynamically move through your data in rows or whereever you have it
    If InStr(ActiveSheet.Cells(3, "C").Value, "ms") > 1 Then
        On Error GoTo 0
        'split string with "ms"
        arr = Split(ActiveSheet.Cells(3, "C").Value, "ms")
        arr1 = Split(Trim(arr(0)), " ")
        arr2 = Split(Trim(arr(1)), " ")
        'case for two "ms"
        If UBound(arr) = 2 Then
            arr3 = Split(Trim(arr(2)), " ")
        End If
        
        'test the string before and after "ms" if they are numeric
        If UBound(arr) = 1 Then
            If IsNumeric(Trim(arr1(UBound(arr1)))) = True Then
                myValue = Trim(arr1(UBound(arr1)))
            ElseIf IsNumeric(Trim(arr2(0))) = True Then
                myValue = Trim(arr2(0))
            End If
            
        'test the string before and after "ms" if they are numeric - case for two "ms" in string
        ElseIf UBound(arr) = 2 Then
            If IsNumeric(Trim(arr2(UBound(arr2)))) = True Then
                myValue = Trim(arr2(UBound(arr2)))
            ElseIf IsNumeric(Trim(arr3(0))) = True Then
                myValue = Trim(arr3(0))
            End If
        End If
    End If
    
    ActiveSheet.Cells(1, 1) = myValue
    myEnd:
    End Sub

相关问题