+ Reply to Thread
Results 1 to 4 of 4

Shift characters in a value based on a variable

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-17-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    157

    Shift characters in a value based on a variable

    I've been trying to write a macro (unsuccessfully) to shift a series of numeric characters based on a value that I specify.

    I always have a series of 7 numbers and the first number can be a 1 or a 0. The second number can be a 2 or a 0. The third number can be a 3 or a 0, etc. So, I get a report that will give me these numbers and I'm trying to shift the numbers down the sequence.

    For example, my report generates: 1204067
    If I add one to it, I would like: 1230507
    Or if I add two, I would get: 1234060
    Four: 0234507

    So basically, I'm moving the zeros around the sequence. Is there a way to do this with VBA?

    Please note that at least one of the seven characters will be greater than zero, but it's entirely possible to get something like this: 0000060

    I have attached a sample workbook.
    Attached Files Attached Files
    Last edited by Ricker090; 02-08-2012 at 11:00 AM. Reason: Solved

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Shift characters in a value based on a variable

    Hi

    Try this UDF
    Option Base 1
    Function myfunc(startno, mover)
      Dim outarr(7)
      
      For i = 1 To 7
        If Mid(startno, i, 1) = "0" Then
          If i + mover > 7 Then
            posn = i + mover - 7
          Else
            posn = i + mover
          End If
          outarr(posn) = "0"
        End If
          
      Next i
      For i = 1 To 7
        If outarr(i) <> "0" Then
          outarr(i) = i
        End If
      Next i
      For i = 1 To 7
        myfunc = myfunc & outarr(i)
      Next i
      
    End Function
    Using your example file,
    F1: =myfunc($B$1,D1)
    Copy down to F4

    HTH

    rylo
    Last edited by rylo; 02-07-2012 at 06:26 PM. Reason: edited function code

  3. #3
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Shift characters in a value based on a variable

    Another possible UDF
    Function ShiftZeros(Base As Range, ShiftBy As Long) As String
        Dim n As Long, DigitCount As Long
        Dim strTmp As String
        
        If ShiftBy = 0 Then
            ShiftZeros = ""
            Exit Function
        End If
        
        DigitCount = Len(Base)
        If DigitCount > 9 Then
            ShiftZeros = "Digit Limit = 9"
            Exit Function
        End If
        
        ReDim BaseArray(DigitCount)
        For n = 1 To DigitCount
            BaseArray(n) = n
        Next
        
        ReDim tmpArray(DigitCount)
        For n = 1 To DigitCount
            If Mid(Base, n, 1) > 0 Then
                tmpArray(n) = 0
            Else
                If (n + ShiftBy) Mod DigitCount = 0 Then
                    tmpArray(n) = DigitCount
                Else
                    tmpArray(n) = (n + ShiftBy) Mod DigitCount
                End If
            End If
        Next
        
        For n = 1 To DigitCount
            If tmpArray(n) > 0 Then BaseArray(tmpArray(n)) = 0
        Next
        
        For n = 1 To DigitCount
            ShiftZeros = ShiftZeros & BaseArray(n)
        Next
        
    End Function
    A bit longer than rylos' but this will handle text based numbers with 1 to 9 digits.
    Attached Files Attached Files
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  4. #4
    Forum Contributor
    Join Date
    06-17-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    157

    Re: Shift characters in a value based on a variable

    Thanks guys. Both of these solutions work great and I will definitely use them.

+ 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