+ Reply to Thread
Results 1 to 6 of 6

Pass multiple characters to a string variable

  1. #1
    ExcelMonkey
    Guest

    Pass multiple characters to a string variable

    I have a function that I am using below. I want to be able to pass all the
    operators to the variable Operators (i.e.Operators = "+" & "-" & "/" & "*" &
    "^"). However when I do this, the function fails. It only works when I pass
    one character to the variale. How I do pass more than one character to this
    variable and then have it work proplerly in the Split function? Thanx

    Function functionseparator(str As String) As String
    Dim Operators As String
    Dim temp

    Operators = "+"

    temp = Split(str, Operators)
    functionseparator = temp(UBound(temp) - 1)

    End Function

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525
    What do you want to do?

    What do you expect to be returned for the data like


    1+ 2 / 3 * 5 ^ 12

    ??

  3. #3
    Bob Phillips
    Guest

    Re: Pass multiple characters to a string variable

    I may be mis-understanding, but you seem to want to have multiple separators
    for split.

    Does this do what you want?

    Function functionseparator(str As String) As String
    Dim Operators
    Dim temp
    Dim i As Long
    Dim iPos As Long
    Dim iStart As Long

    Operators = Array("+", "/", "*","^")

    iStart = 1
    ReDim temp(1 To 1)
    For iPos = 1 To Len(str)
    For i = LBound(Operators) To UBound(Operators)
    If Mid(str, iPos, 1) = Operators(i) Then
    itemp = itemp + 1
    ReDim Preserve temp(1 To itemp)
    temp(itemp) = Mid(str, iStart, iPos - iStart)
    iStart = iPos + 1
    Exit For
    End If
    Next i
    Next iPos
    ReDim Preserve temp(1 To itemp + 1)
    temp(itemp + 1) = Mid(str, iStart, iPos - iStart)
    functionseparator = temp(UBound(temp) - 1)

    End Function


    --
    HTH

    Bob Phillips

    "ExcelMonkey" <[email protected]> wrote in message
    news:[email protected]...
    > I have a function that I am using below. I want to be able to pass all

    the
    > operators to the variable Operators (i.e.Operators = "+" & "-" & "/" & "*"

    &
    > "^"). However when I do this, the function fails. It only works when I

    pass
    > one character to the variale. How I do pass more than one character to

    this
    > variable and then have it work proplerly in the Split function? Thanx
    >
    > Function functionseparator(str As String) As String
    > Dim Operators As String
    > Dim temp
    >
    > Operators = "+"
    >
    > temp = Split(str, Operators)
    > functionseparator = temp(UBound(temp) - 1)
    >
    > End Function




  4. #4
    DM Unseen
    Guest

    Re: Pass multiple characters to a string variable

    You need regular expressions not split. see regexp library:

    http://msdn.microsoft.com/library/de...sobjRegExp.asp

    Dm Unseen


  5. #5
    ExcelMonkey
    Guest

    Re: Pass multiple characters to a string variable

    As always Bob, you the man!

    Thanks

    "Bob Phillips" wrote:

    > I may be mis-understanding, but you seem to want to have multiple separators
    > for split.
    >
    > Does this do what you want?
    >
    > Function functionseparator(str As String) As String
    > Dim Operators
    > Dim temp
    > Dim i As Long
    > Dim iPos As Long
    > Dim iStart As Long
    >
    > Operators = Array("+", "/", "*","^")
    >
    > iStart = 1
    > ReDim temp(1 To 1)
    > For iPos = 1 To Len(str)
    > For i = LBound(Operators) To UBound(Operators)
    > If Mid(str, iPos, 1) = Operators(i) Then
    > itemp = itemp + 1
    > ReDim Preserve temp(1 To itemp)
    > temp(itemp) = Mid(str, iStart, iPos - iStart)
    > iStart = iPos + 1
    > Exit For
    > End If
    > Next i
    > Next iPos
    > ReDim Preserve temp(1 To itemp + 1)
    > temp(itemp + 1) = Mid(str, iStart, iPos - iStart)
    > functionseparator = temp(UBound(temp) - 1)
    >
    > End Function
    >
    >
    > --
    > HTH
    >
    > Bob Phillips
    >
    > "ExcelMonkey" <[email protected]> wrote in message
    > news:[email protected]...
    > > I have a function that I am using below. I want to be able to pass all

    > the
    > > operators to the variable Operators (i.e.Operators = "+" & "-" & "/" & "*"

    > &
    > > "^"). However when I do this, the function fails. It only works when I

    > pass
    > > one character to the variale. How I do pass more than one character to

    > this
    > > variable and then have it work proplerly in the Split function? Thanx
    > >
    > > Function functionseparator(str As String) As String
    > > Dim Operators As String
    > > Dim temp
    > >
    > > Operators = "+"
    > >
    > > temp = Split(str, Operators)
    > > functionseparator = temp(UBound(temp) - 1)
    > >
    > > End Function

    >
    >
    >


  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525
    In that case, you don't need Split function
    Please Login or Register  to view this content.

+ 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