Results 1 to 4 of 4
  1. #1
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Australia
    Posts
    1,294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    manipulate strings (word97 sr2b)

    hiya i'm having trouble getting my following code to work to manipulate a string.
    An example of the instring will be " 01, 05, 07,45 "
    & the end result I want displayed in the doc is " 1, 5, 7, 45"

    also I dont think I'm understanding the mid function.
    when in the VBA help it appears clear.
    for instance my code:
    NewStr = Mid(strIn, iStartPos, iCharTotal)
    is returning "01, 05, 0" when i thought its supposed to return "1, 05, 07, 45"
    maybe its just my code.
    can you please have a look & let me know where i'm going off the rail.
    many thanks Diana


    Dim FinalStr As String
    Dim RestOfWord As String
    Dim NewStr As String
    Dim lStringLen As Long
    Dim n As Long
    Dim j As Long
    Dim iEndPos As Long
    Dim iStartPos As Long
    Dim bCommaFound As Boolean


    strIn = rngSearch
    strIn = LTrim(strIn)
    strIn = RTrim(strIn)
    iEndPos = Len(strIn)
    lStringLen = Len(strIn)
    iStartPos = 1

    For n = 1 To lStringLen
    commaFound = False
    strTemp = Left(strIn, n)
    iStartPos = n
    iCharTotal = iEndPos - n
    RestOfWord = Right(strIn, lStringLen - n)
    NewStr = Mid(strIn, iStartPos, iCharTotal)

    Select Case Mid$(strIn, n, 1)
    Case ","
    commaFound = True
    If commaFound = True Then
    strClass = CStr(CInt(strTemp))
    FinalStr = strClass & ","
    End If

    Case Else
    End Select
    Next n

    goto bookmark & insert FinalStr

  2. #2
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: manipulate strings (word97 sr2b)

    This works fine in Excel, but alas, Word VBA has no Evaluate function:

    Sub test()
    Dim sStr As String
    Dim varArray As Variant
    Dim lCount As Long
    Dim sNewstr As String
    sNewstr = ""
    sStr = " 01, 05, 07,45 "
    varArray = Evaluate("{" & sStr & "}")
    For lCount = 1 To UBound(varArray)
    sNewstr = sNewstr & varArray(lCount) & ","
    Next
    sNewstr = Left(sNewstr, Len(sNewstr) - 1)
    MsgBox sNewstr
    End Sub
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  3. #3
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: manipulate strings (word97 sr2b)

    This one seems to work in Word 2000:

    Sub test()
    Dim sStr As String
    Dim lArray() As Long
    Dim lCount As Long
    Dim sNewstr As String
    Dim lPos As Long
    Dim lLastPos As Long
    ReDim lArray(1)
    lPos = 0
    lLastPos = 1
    sNewstr = ""
    sStr = " 01, 05, 07,45 "
    lCount = 0
    Do
    lPos = InStr(lPos + 1, sStr, ",")
    If lPos = 0 Then Exit Do
    lCount = lCount + 1
    ReDim Preserve lArray(lCount)
    lArray(lCount) = CLng(Mid(sStr, lLastPos + 1, lPos - 1 - lLastPos))
    lLastPos = lPos
    Loop
    ReDim Preserve lArray(lCount + 1)
    'Add the last one!
    lArray(lCount + 1) = CLng(Mid(Mid(sStr, lLastPos), InStr(Mid(sStr, lLastPos), ",") + 1))
    For lCount = 1 To UBound(lArray)
    sNewstr = sNewstr & lArray(lCount) & ", "
    Next
    sNewstr = Left(sNewstr, Len(sNewstr) - 2)
    MsgBox sNewstr
    End Sub
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  4. #4
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Australia
    Posts
    1,294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: manipulate strings (word97 sr2b)

    HI Jan
    many thanks...that worked! your a star!
    Diana

Posting Permissions

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