0

What I'm trying to do is increment number and then loop subtract the first 2 digits by 1 and then increment the rest until the first 2 digits = 0

So I have M201001 would become m191002 and next would be m181003 until m011020

2
  • 2
    Not sure this is on topic here, but if you want help with a macro you're working on, it might be useful to include it in your question ;-) Commented Feb 20, 2013 at 23:47
  • Are you sure the VB.NET tag is correct? VB.NET != VB (or VBA). Commented Feb 20, 2013 at 23:53

2 Answers 2

0

What you'll need to do is break up your value into 3 using left, mid and right functions.

Then do the looping as you require it. I'm trying it out myself so I'll update my answer once I've done so.

Added Code:

Sub Testing()

    Dim myIn As String
    myIn = "M201001"

    Dim myLeft As String
    Dim myMid As Integer, myRight As Integer, i As Integer
    Dim myOut As String
    myLeft = Left(myIn, 1)
    myMid = CInt(Mid(myIn, 2, 2))
    myRight = CInt(Right(myIn, 4))
    myOut = myLeft & Format(myMid, "00") & Format(myRight, "0000")
    i = 0

    Debug.Print "IN:        " & myIn
    Debug.Print "BROKEN UP: " & myOut

    Do Until myMid = -1
        Debug.Print "ITERATION " & Format(i, "00") & ": " & myLeft & Format(myMid, "00") & Format(myRight, "0000")

        myMid = myMid - 1
        myRight = myRight + 1
        myOut = myLeft & Format(myMid, "00") & Format(myRight, "0000")
        i = i + 1
    Loop

End Sub

If you need any explanation please ask. I will be happy to explain.

Sign up to request clarification or add additional context in comments.

2 Comments

thats awesome so beyond my capability! it even calculates it within the macro which i guess is what debug print is and using ITERATION well now got that i need to place it to where it puts those in a sheet which im gona play arround with it and post it up here
I had to post a new question since the rules of the site state to do so which is on this page stackoverflow.com/questions/14996655/… was trying to get it to work almost there
0

Try this

Function GetNextNumber(n As String) As Variant
    ' Validate input
    If Len(n) <> 7 Then
        GetNextNumber = xlErrNum
        Exit Function
    ElseIf Left$(n, 1) <> "M" Then
        GetNextNumber = CVErr(xlErrNum)
        Exit Function
    End If

    GetNextNumber = Left$(n, 1) & Format(val(Mid$(n, 2)) - 9999, "000000")

End Function

Test using this

Sub demo()
    Dim n As Variant

    n = "M201001"
    Debug.Print n
    Do
        n = GetNextNumber(CStr(n))
        If IsError(n) Then Exit Sub
        Debug.Print n
    Loop Until val(Mid$(n, 2)) <= 19999
End Sub

2 Comments

thanks for your help with that but i need it to separate the number like the macro bellow
I had to post a new question since the rules of the site state to do so which is on this page stackoverflow.com/questions/14996655/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.