+ Reply to Thread
Results 1 to 11 of 11

processing a string by Excel

Hybrid View

  1. #1
    Registered User
    Join Date
    06-08-2007
    Location
    New York
    MS-Off Ver
    2007 and 2010
    Posts
    13

    processing a string by Excel

    Hi Mudraker,
    Thanks a lot for your reply. I appreciate it.
    I have noticed an error in the expression. There is a redundant * sign right before the closing paranthesis in the example in Book1.xls file. It should not there. Also, I have run the script on more examples and noticed that it doesn't do quite what it is supposed to do. One more thing is that I will copy the resultant expression and paste it in Mathcad. Can the program write the result in a cell instead of message box?
    I have attached another Excel file (Book11.xls) to this post including more examples for string. Cell A1 is the same example as you worked on. Cells A2 and A3 are new examples to test the script for.
    The strings have parantesis that grabs the whole expression as well as paranthesis in the string parameters. It requires extra effort in programming to diffrentiate between them. Therefore, I have the option to use some other type of parantesis in the parameters. For example, I can use square brackets "[]" or curly parantesis "{}" in the parameters. I think this may make the programming easier since the program keeps the paranthesis as is () and detects the square brackets (or or curly paranthesis) in the parameters and modify the expression accordingly. Cell A4 in the attached file has an expression that uses square brackets within parameters. The expression in A4 is the same expression as in A3 except the square brackets. Cells A7 to A9 has the modified expressions of the strings in A1 to A3, respectively.

    Thanks in advance.

    Mark Neil
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    06-08-2007
    Location
    New York
    MS-Off Ver
    2007 and 2010
    Posts
    13

    processing a string

    Norm,
    Thanks for your help.
    I have added three more examples of string in the file attached (Book111.xls). The first three are the same as in Book11.xls. But I changed them so that parameters have brackets instead of paranthesis.
    Strings from A4 to A6 are new.
    I have noticed that the parameters in the string A1 have extra characters after a closing bracket "]" while all the parameters in the strings in A2 and A3 end with a bracket. I have a hunch that if you add to the program the intelligence that it ignores rest of the parameter until the next space once it detects a closing bracket, it would work just fine.
    Thanks.

    Mark
    Attached Files Attached Files

  3. #3
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    Mark

    It's taken a while to get all the bugs out.

    The answers you supplied for the last 2 equations, to me appears to be incorrect - You left a ] in both of them & the last one is missing the 1st )

    The extra examples helped me to code the macro to deliver the results you are after

    This macro loops through column A starting at row 1 to the last used row in column A, Places the result in column B of the same row

    Please test as fully as possible.

    Sub ExtractFormula()
       Dim bAdd As Boolean
       
       Dim iTxtLen As Integer
       Dim i4Len As Integer
       
       Dim l4Row As Long
       
       Dim sCval As String
       Dim sFormula As String
       Dim sParam As String
       Dim sOp As String
       
       For l4Row = 1 To Cells(Rows.Count, "a").End(xlUp).Row Step 1
          sCval = Cells(l4Row, "a")
          iTxtLen = Len(sCval)
          
          bAdd = True
          For i4Len = 1 To iTxtLen Step 1
             Select Case Mid(sCval, i4Len, 1)
             Case "(", ")"
                If Len(sOp) = 1 Then
                   sFormula = sFormula & sOp & sParam
                   sOp = vbNullString
                Else
                   If Len(sParam) > 0 Then
                      Select Case sParam
                      Case "-", "+", "/", "*", "^"
                         sFormula = sFormula & sParam
                      Case Else
                         sFormula = sFormula & "*" & sParam
                      End Select
                   End If
                End If
                sParam = vbNullString
                sFormula = sFormula & Mid(sCval, i4Len, 1)
                bAdd = True
             Case " "
                bAdd = True
                If Len(sParam) > 0 Then
                   Select Case sParam
                   Case "-", "+", "/", "*", "^"
                      sOp = sParam
                   Case Else
                      If Len(sOp) = 1 Then
                         sFormula = sFormula & sOp & sParam
                         sOp = vbNullString
                      Else
                         If Right(sFormula, 1) = "(" Then
                            sFormula = sFormula & sParam
                         Else
                            Select Case Left(sParam, 1)
                            Case "-", "+", "/", "*", "^"
                               sFormula = sFormula & sParam
                            Case Else
                               sFormula = sFormula & "*" & sParam
                            End Select
                         End If
                      End If
                   End Select
                End If
                sParam = vbNullString
             Case Else
                Select Case Mid(sCval, i4Len, 1)
                Case "["
                   sParam = vbNullString
                Case "]"
                   bAdd = False
                Case Else
                   If bAdd = True Then
                      sParam = sParam & Mid(sCval, i4Len, 1)
                   End If
                End Select
             End Select
          Next i4Len
          If Len(sParam) > 0 Then
             If Len(sOp) = 1 Then
                sFormula = sFormula & sOp & sParam
             Else
                sFormula = sFormula & "*" & sParam
             End If
          End If
          Cells(l4Row, "b").Value = sFormula
          sFormula = vbNullString
          sOp = vbNullString
          sParam = vbNullString
       Next l4Row
    End Sub
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  4. #4
    Registered User
    Join Date
    06-08-2007
    Location
    New York
    MS-Off Ver
    2007 and 2010
    Posts
    13

    processing a string by Excel

    Hi Norm,
    Thanks a lot for your effort in writing this program. I have tested it in few sample strings and it works just fine. This was great help for me.
    Could you do a minor modification to this program? Instead of writing the result to column B, can you write them to column A in order by skipping one row from the last expression.
    Even if this is not done, the present version does the job for me. Thanks a lot again :-)

    Mark.

  5. #5
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    Mark

    Glad to hear the macro works.

    With the required changes is their already a blank cell under each entry in Column A that the macro can just place the formula into or will the macro need to insert a blank row before it places the formula into column A

  6. #6
    Registered User
    Join Date
    06-08-2007
    Location
    New York
    MS-Off Ver
    2007 and 2010
    Posts
    13

    processing a string by Excel

    Hi Norm,
    The program already finds how many rows populated in column A and writes the results in column B in each respective row. Say, rows from A1 to A4 contain formulas. I prefer to see the results from A6 to A9 in order having A5 empty.
    Thanks a bunch.

    Mark.

  7. #7
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    Mark

    If I understand what you are after correctly this version will work.

    Sub ExtractFormula()
       Dim bAdd As Boolean
       
       Dim iTxtLen As Integer
       Dim i4Len As Integer
       
       Dim l4Row As Long
       Dim lLR As Long
       
       Dim sCval As String
       Dim sFormula As String
       Dim sParam As String
       Dim sOp As String
       
       
       lLR = Cells(Rows.Count, "a").End(xlUp).Row
       
       For l4Row = 1 To lLR Step 1
          sCval = Cells(l4Row, "a")
          iTxtLen = Len(sCval)
          
          bAdd = True
          For i4Len = 1 To iTxtLen Step 1
             Select Case Mid(sCval, i4Len, 1)
             Case "(", ")"
                If Len(sOp) = 1 Then
                   sFormula = sFormula & sOp & sParam
                   sOp = vbNullString
                Else
                   If Len(sParam) > 0 Then
                      Select Case sParam
                      Case "-", "+", "/", "*", "^"
                         sFormula = sFormula & sParam
                      Case Else
                         sFormula = sFormula & "*" & sParam
                      End Select
                   End If
                End If
                sParam = vbNullString
                sFormula = sFormula & Mid(sCval, i4Len, 1)
                bAdd = True
             Case " "
                bAdd = True
                If Len(sParam) > 0 Then
                   Select Case sParam
                   Case "-", "+", "/", "*", "^"
                      sOp = sParam
                   Case Else
                      If Len(sOp) = 1 Then
                         sFormula = sFormula & sOp & sParam
                         sOp = vbNullString
                      Else
                         If Right(sFormula, 1) = "(" Then
                            sFormula = sFormula & sParam
                         Else
                            Select Case Left(sParam, 1)
                            Case "-", "+", "/", "*", "^"
                               sFormula = sFormula & sParam
                            Case Else
                               sFormula = sFormula & "*" & sParam
                            End Select
                         End If
                      End If
                   End Select
                End If
                sParam = vbNullString
             Case Else
                Select Case Mid(sCval, i4Len, 1)
                Case "["
                   sParam = vbNullString
                Case "]"
                   bAdd = False
                Case Else
                   If bAdd = True Then
                      sParam = sParam & Mid(sCval, i4Len, 1)
                   End If
                End Select
             End Select
          Next i4Len
          If Len(sParam) > 0 Then
             If Len(sOp) = 1 Then
                sFormula = sFormula & sOp & sParam
             Else
                sFormula = sFormula & "*" & sParam
             End If
          End If
          Cells(lLR + l4Row + 1, "a").Value = sFormula
          sFormula = vbNullString
          sOp = vbNullString
          sParam = vbNullString
       Next l4Row
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1