Consulting

Results 1 to 9 of 9

Thread: increasing strings or adding strings

  1. #1
    VBAX Newbie
    Joined
    Jun 2005
    Posts
    5
    Location

    increasing strings or adding strings

    Hi, I want to increase some string by 1 in VBA. For example, I have 00sad231f and I want to get 00sad231g. In other languages than VB, you can simply write "00sad231f"+1. Do I have any other options than write comlicated code? Thank you.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by mik
    Hi, I want to increase some string by 1 in VBA. For example, I have 00sad231f and I want to get 00sad231g. In other languages than VB, you can simply write "00sad231f"+1. Do I have any other options than write comlicated code? Thank you.
    Very simply with no bound checking etc.


    sVal = "00sad231f"
        sVal = Left(sVal, Len(sVal) - 1) & Chr(Asc(Right(sVal, 1)) + 1)
        Debug.Print sVal

  3. #3
    VBAX Newbie
    Joined
    Jun 2005
    Posts
    5
    Location
    It works! Thank you! Bound checking would be nice (only A-Z 0-9 are allowed), but this helps anyway.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by mik
    Bound checking would be nice (only A-Z 0-9 are allowed

    Function ChangeLast(val As String)
    Dim sTemp As String
        sTemp = Chr(Asc(Right(val, 1)))
        Select Case Asc(sTemp)
            Case 48 To 56, 65 To 89, 97 To 121:
                sTemp = Chr(Asc(sTemp) + 1)
            Case Else:  'do nothing
        End Select
        ChangeLast = Left(val, Len(val) - 1) & sTemp
    End Function

  5. #5
    VBAX Newbie
    Joined
    Jun 2005
    Posts
    5
    Location
    Thank you once again. The code doesn't work-for strings, that end with z or 9, it returns the same string..

  6. #6
    VBAX Newbie
    Joined
    Jun 2005
    Posts
    5
    Location
    my point is to get from the string 403fjorz 403fjos1, from 403fjor9 403fjora etc.. I know this is quite complicated and has to be made by some cycle..

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by mik
    my point is to get from the string 403fjorz 403fjos1, from 403fjor9 403fjora etc.. I know this is quite complicated and has to be made by some cycle..
    You want my blood too


    Function ChangeLast(val As String)
        Dim sTemp As String
        sTemp = Chr(Asc(Right(val, 1)))
        Select Case Asc(sTemp)
            Case 48 To 56, 65 To 89, 97 To 121:
                sTemp = Chr(Asc(sTemp) + 1)
            Case 57:    sTemp = Chr(65)
            Case 90:    sTemp = Chr(97)
            Case 122:   sTemp = Chr(48)
            Case Else: 'do nothing
        End Select
        ChangeLast = Left(val, Len(val) - 1) & sTemp
    End Function

  8. #8
    VBAX Mentor Sir Babydum GBE's Avatar
    Joined
    Mar 2005
    Location
    Cardiff, UK
    Posts
    499
    Location
    Quote Originally Posted by xld
    You want my blood too
    Thank goodness for that - it's not just me who gives you a hard time...

  9. #9
    VBAX Newbie
    Joined
    Jun 2005
    Posts
    5
    Location
    Still not exactly what I meant-it changes only last char.. Nevermind, I managed to code it myself, but xld> You helped me a lot!! Thank you very much for your effort, time and quickness of your answers.
    here is the code, if anyone is interested:

    Function increaseStr2(s)
    Dim varData(20) As Variant
    'varData = s
    Max = Len(s)
    For n = 0 To (Max - 1)
    varData(Max - n) = Mid(s, Max - n, 1)
    Next
    ' ve vardata mame pole
    For n = 0 To (Max - 1)
    v2 = varData(Max - n)
    lastChDec = Asc(Right(v2, 1))
    If (lastChDec < 57) Then
        lastChDec = lastChDec + 1
        GoTo EOC
    End If
    If (lastChDec > 96 And lastChDec < 122) Then
        lastChDec = lastChDec + 1
        GoTo EOC
    End If
    If (lastChDec = 122) Then
        lastChDec = 48
        varData(Max - n) = Chr(lastChDec)
    End If
    If (lastChDec = 57) Then
        lastChDec = 97
        GoTo EOC
    End If
    Next
    EOC:
    varData(Max - n) = Chr(lastChDec)
    s = ""
    For n = 1 To Max
    s = s & varData(n)
    Next
    increaseStr2 = s
    End Function

Posting Permissions

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