+ Reply to Thread
Results 1 to 11 of 11

processing a string by Excel

Hybrid View

  1. #1
    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
    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.

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

    processing a string by Excel

    Many thanks. The programs does what I want it to do.

    Mark.

  3. #3
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    Glad to hear that it solves your problem

+ 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