0

我要做的是递增数字,然后循环减去第一个2数字1 ,然后增加其余的数字,直到第一个2数字=0

所以我M201001会成为m191002,下一个将是m181003直到m011020

4

2 回答 2

0

您需要做的是使用left,midright函数将您的值分解为 3 个。

然后根据需要进行循环。我正在自己尝试,所以一旦我这样做了,我会更新我的答案。

添加代码:

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

如果您需要任何解释,请询问。我很乐意解释。

于 2013-02-21T02:20:19.417 回答
0

试试这个

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

使用这个进行测试

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
于 2013-02-21T04:54:25.510 回答