Results 1 to 8 of 8

Thread: [RESOLVED] string increment

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    73

    Resolved [RESOLVED] string increment

    hi experts
    last time you gave me this code for string increment
    AI/WE/23--+1 increment--->AI/WE/24
    but when i passed
    AI/WE/001--+1 increment--->AI/WE/2
    i required
    AI/WE/002
    plz help me ,what chang i make this function
    thanks in advance

    Public Function IncrementString(ByVal sTxt As String, ByVal nIncrement As
    Long) As String
    Dim sParts() As String
    Dim nUpperIdx As Long
    'Split the string into different parts, use / as the delimiter
    sParts = Split(sTxt, "/")
    'the part to increment is the last number
    nUpperIdx = UBound(sParts)
    'Check that this part is numeric
    If IsNumeric(sParts(nUpperIdx)) Then
    'increment this part with nIncrement
    sParts(nUpperIdx) = CStr(CLng(sParts(nUpperIdx)) + nIncrement)
    End If
    'Join the parts again and return the new string
    IncrementString = Join(sParts, "/")
    End Function

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: string increment

    VB Code:
    1. Public Function IncrementString(ByVal sTxt As String, ByVal nIncrement As Long) As String
    2.     Dim sParts() As String
    3.     Dim nUpperIdx As Long
    4.     'Split the string into different parts, use / as the delimiter
    5.     sParts = Split(sTxt, "/")
    6.     'the part to increment is the last number
    7.     nUpperIdx = UBound(sParts)
    8.     'Check that this part is numeric
    9.     If IsNumeric(sParts(nUpperIdx)) Then
    10.         'increment this part with nIncrement
    11.         sParts(nUpperIdx) = [b]Format$(CLng(sParts(nUpperIdx)) + nIncrement, String(Len(sParts(nUpperIdx)), "0"))[/b]
    12.     End If
    13.     'Join the parts again and return the new string
    14.     IncrementString = Join(sParts, "/")
    15. End Function
    Please use [ vbcode ] and [ /vbcode ] tags (without the spaces) when you post source code.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    73

    Re: string increment

    thanks i will try it

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    73

    Re: string increment

    it will work for AI/WE/23---------->AI/WE/24

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: string increment

    I tried it with: AI/WE/001 -> AI/WE/002

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    73

    Re: string increment

    but i reqired for both 001 and 23

  7. #7
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: string increment

    Yeah, it works for any number.
    AI/WE/23 --> AI/WE/24
    AI/WE/023 --> AI/WE/024

    ..and so on...

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    73

    Re: string increment

    thanks joacim andersson

Posting Permissions

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



Click Here to Expand Forum to Full Width