English 中文(简体)
Excel: Find k and m in “kx + m” textstring
原标题:Excel: Find k and m in "kx + m" text string

是否使用<条码>VBA或一种公式在<条码>kx+m string中找到“k”和“m”变量?

有若干种假设情景,说明加x+m的扼杀可如何看待,例如:

312*x+12
12+x*2
-4-x

等等。 我确信,我可以通过在Excel撰写非常复杂的公式来解决这一问题,但我想也许有人已经解决了这一和类似的问题。 我是迄今最直射的,但它没有处理所有尚未发生的情况(例如,在X+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));"")))

最佳回答

我确信这将有助于你:

将这一职能纳入一个单元:

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

Then you can either call it from a Macro e.g. like this:

Sub Test()
    Debug.Print FindKXPlusM("x*312+12")
End Sub

Or use it like a formula. e.g. by putting this in a cell:

=FindKXPlusM(B1)

我喜欢第二种方式(无工作:P)

我用各种价值观对它进行了测试,在此放映了我获得的以下信息:

“Screenshot

Hope this helps :)

问题回答

而没有双管齐下,则在VBA中操作一个简单的<代码>LINEST。

Replace StrFunc as needed

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

它比看起来更为复杂。 如果我不错的话,每小时加一米可以有Max7的操作员和1名操作员。 在这种情形下,获得“K”和“M”价值确实变得复杂。 —— Siddharth Rout 33mins before

Building on my comment in duffymo s post

This snapshot shows the different combinations that “kx + m” can have

“entergraph

如前所述,实现你想要的东西非常复杂。 这里是我的feebletries,目前仅提取“K”。 <><>>> 这部法律绝无任何区别。 此外,我没有用不同的假设情景测试该守则,因此可能与其他人失败。 然而,这给你带来了如何解决这一问题的公平想法。 你们将不得不夸张,以取得你想要的准确结果。

http://www.ohchr.org。 (我正在测试该法典中的7个可能的组合。) 它为这7项工作而努力,但可能会/会因他人而失败。

Option Explicit

Sub Sample()
    Dim StrCheck As String
    Dim posStar As Long, posBrk As Long, pos As Long, i As Long
    Dim strK As String, strM As String
    Dim MyArray(6) As String

    MyArray(0) = "-k*(-x)+(-m)*(-2)"
    MyArray(1) = "-k*x+(-m)*(-2)"
    MyArray(2) = "-k(x)+(-m)*(-2)"
    MyArray(3) = "-k(x)+(-m)(-2)"
    MyArray(4) = "-kx+m"
    MyArray(5) = "kx+m"
    MyArray(6) = "k(x)+m"

    For i = 0 To 6
        StrCheck = MyArray(i)
        Select Case Left(Trim(StrCheck), 1)

        Case "+", "-"
            posBrk = InStr(2, StrCheck, "(")
            posStar = InStr(2, StrCheck, "*")

            If posBrk > posStar Then             <~~ "-k*(-x)+(-m)*(-2)"
                pos = InStr(2, StrCheck, "*")
                If pos <> 0 Then
                    strK = Mid(StrCheck, 1, pos - 1)
                Else
                    strK = Mid(StrCheck, 1, posBrk - 1)
                End If
            ElseIf posBrk < posStar Then         <~~ "-k(-x)+(-m)*(-2)"
                pos = InStr(2, StrCheck, "(")
                strK = Mid(StrCheck, 1, pos - 1)
            Else                                 <~~ "-kx+m"
                 ~~> In such a case I am assuming that you will never use
                 ~~> a >=2 letter variable
                strK = Mid(StrCheck, 1, 2)
            End If
        Case Else
            posBrk = InStr(1, StrCheck, "(")
            posStar = InStr(1, StrCheck, "*")

            If posBrk > posStar Then             <~~ "k*(-x)+(-m)*(-2)"
                pos = InStr(1, StrCheck, "*")
                If pos <> 0 Then
                    strK = Mid(StrCheck, 1, pos - 2)
                Else
                    strK = Mid(StrCheck, 1, posBrk - 1)
                End If
            ElseIf posBrk < posStar Then         <~~ "k(-x)+(-m)*(-2)"
                pos = InStr(1, StrCheck, "(")
                strK = Mid(StrCheck, 1, pos - 2)
            Else                                 <~~ "kx+m"
                 ~~> In such a case I am assuming that you will never use
                 ~~> a >=2 letter variable
                strK = Mid(StrCheck, 1, 1)
            End If
        End Select

        Debug.Print "Found " & strK & " in " & MyArray(i)
    Next i
End Sub

<>SNAPSHOT

“enter

这不是一件好事,但我希望这能让你们走上正确的道路。

我在“x”之后,在“+”之后,用一个或数位数进行搜索。

你的榜样显示了愤怒的价值观。 斜坡和拦截是点号? 签字价值? 这比你的例子更为复杂。

我建议,最普遍的解决办法是,为你写一个简单图表的弹性/选择文件。 我不知道VB或VB。 NET向您提供。 南极海生委将在贾瓦兰找到一个解决办法;有《南极海生委》。 NET。

我不相信这一努力会给你带来什么。 你们将做些什么? 我认为,用户在数字类型电池中填充千米和百分数并计算<代码>y = m*x + k时,从这些单位中填满,而不是插入插图和提取。

If your goal is simply to evaluate a String, maybe eval() is your answer:

How to turn a string formula into a "real" formula





相关问题
import of excel in SQL imports NULL lines

I have a stored procedure that imports differently formatted workbooks into a database table, does work on them then drops the table. Here is the populating query. SELECT IDENTITY(INT,1,1) AS ID ...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

Excel date to Unix timestamp

Does anyone know how to convert an Excel date to a correct Unix timestamp?

C# GemBox Excel Import Error

I am trying to import an excel file into a data table using GemBox and I keep getting this error: Invalid data value when extracting to DataTable at SourceRowIndex: 1, and SourceColumnIndex: 1. As ...

Importing from excel "applications" using SSIS

I am looking for any tips or resources on importing from excel into a SQL database, but specifically when the information is NOT in column and row format. I am currently doing some pre-development ...

热门标签