Excel:在"kx + m"文本字符串中查找k和m

use*_*776 7 excel vba excel-vba excel-formula

是否有一种聪明的方法使用VBA或公式来找到"k"和"m"变量kx+m string

关于kx + m字符串的外观有几种情况,例如:

312*x+12
12+x*2
-4-x
Run Code Online (Sandbox Code Playgroud)

等等.我很确定我可以通过在Excel中编写非常复杂的公式来解决这个问题,但我想也许有人已经解决了这个和类似的问题.这是我到目前为止最好的拍摄,但它还没有处理所有情况(比如kx + m字符串中有两个缺点:

=TRIM(IF(NOT(ISERROR(SEARCH("~+";F5))); IF(SEARCH("~+";F5)>SEARCH("~*";F5);RIGHT(F5;LEN(F5)-SEARCH("~+";F5));LEFT(F5;SEARCH("~+";F5)-1)); IF(NOT(ISERROR(SEARCH("~-";F5))); IF(SEARCH("~-";F5)>SEARCH("~*";F5);RIGHT(F5;LEN(F5)-SEARCH("~-";F5)+1);LEFT(F5;SEARCH("~*";F5)-1));"")))

bre*_*tdj 5

而不是费心解析LINEST在VBA中运行简单.

StrFunc根据需要更换

Sub Extract()
Dim strFunc As String
Dim X(1 To 2) As Variant
Dim Y(1 To 2) As Variant
Dim C As Variant

X(1) = 0
X(2) = 100

strFunc = "312*x+12"
'strFunc = "12+x*2 "
'strFunc = "-4-X"

Y(1) = Evaluate(Replace(LCase$(strFunc), "x", X(1)))
Y(2) = Evaluate(Replace(LCase$(strFunc), "x", X(2)))
C = Application.WorksheetFunction.LinEst(Y, X)

MsgBox "K is " & C(1) & vbNewLine & "M is " & C(2)

End Sub
Run Code Online (Sandbox Code Playgroud)


Pra*_*mar 4

我相信这会对你有所帮助:)

将此函数放入模块中:

Function FindKXPlusM(ByVal str As String) As String
    Dim K As String, M As String
    Dim regex As Object, matches As Object, sm As Object

    '' remove unwanted spaces from input string (if any)
    str = Replace(str, " ", "")

    '' create an instance of RegEx object.
    '' I'm using late binding here, but you can use early binding too.
    Set regex = CreateObject("VBScript.RegExp")
    regex.IgnoreCase = True
    regex.Global = True

    '' test for kx+m or xk+m types
    regex.Pattern = "^(-?\d*)\*?x([\+-]?\d+)?$|^x\*(-?\d+)([\+-]?\d+)?$"
    Set matches = regex.Execute(str)
    If matches.Count >= 1 Then
        Set sm = matches(0).SubMatches
        K = sm(0)
        M = sm(1)
        If K = "" Then K = sm(2)
        If M = "" Then M = sm(3)
        If K = "-" Or K = "+" Or K = "" Then K = K & "1"
        If M = "" Then M = "0"
    Else
        '' test for m+kx or m+xk types
        regex.Pattern = "^(-?\d+)[\+-]x\*([\+-]?\d+)$|^(-?\d+)([\+-]\d*)\*?x$"
        Set matches = regex.Execute(str)
        If matches.Count >= 1 Then
            Set sm = matches(0).SubMatches
            M = sm(0)
            K = sm(1)
            If M = "" Then M = sm(2)
            If K = "" Then K = sm(3)
            If K = "-" Or K = "+" Or K = "" Then K = K & "1"
            If M = "" Then M = "0"
        End If
    End If
    K = Replace(K, "+", "")
    M = Replace(M, "+", "")

    '' the values found are in K & M.
    '' I output here in this format only for showing sample.
    FindKXPlusM = " K = " & K & "         M = " & M
End Function
Run Code Online (Sandbox Code Playgroud)

然后您可以从宏调用它,例如:

Sub Test()
    Debug.Print FindKXPlusM("x*312+12")
End Sub
Run Code Online (Sandbox Code Playgroud)

或者像公式一样使用它。例如,将其放入单元格中:

=FindKXPlusM(B1)
Run Code Online (Sandbox Code Playgroud)

我喜欢第二种方式(更少的工作:P)

我用各种值对其进行了测试,这是我得到的屏幕截图:

查找KX+M公式截图

希望这可以帮助 :)

  • +1 不错。我喜欢你处理相反情况的方式:) (2认同)