aboutsummaryrefslogtreecommitdiff
path: root/challenge-119/eric-cheung/excel-vba/ch-2.bas
blob: 797b71e466e0f3e405860c157a9cfa9ac489ed85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
Attribute VB_Name = "ModTask_02"
Option Explicit
Option Base 1

Function GetSeqNum(nTerm) As Long
    Dim nLoop As Long, nSubLoop As Long, nCntItem As Long, nArrItemCnt As Long
    Dim nNumArr As Variant
    Dim nTmpArr() As Long, nNuTmpArr() As Long
    Dim bFlag As Boolean
    
    If nTerm < 1 Then
        GetSeqNum = 0
        Exit Function
    ElseIf nTerm <= 3 Then
        GetSeqNum = nTerm
        Exit Function
    End If
    
    nCntItem = 3
    nNumArr = Array(1, 2, 3)
    
    ReDim nTmpArr(1 To 3)
    For nLoop = 1 To 3
        nTmpArr(nLoop) = nLoop
    Next nLoop

    Do While True
        Erase nNuTmpArr
        bFlag = True
        
        For nLoop = LBound(nNumArr) To UBound(nNumArr)
            For nSubLoop = LBound(nTmpArr) To UBound(nTmpArr)
                If nNumArr(nLoop) > 1 Or Int(Left(nTmpArr(nSubLoop), 1)) > 1 Then
                    If bFlag Then
                        nArrItemCnt = 0
                        bFlag = False
                    Else
                        nArrItemCnt = UBound(nNuTmpArr) - LBound(nNuTmpArr) + 1
                    End If
                    
                    ReDim Preserve nNuTmpArr(1 To nArrItemCnt + 1)
                    nNuTmpArr(nArrItemCnt + 1) = Val(Str(nNumArr(nLoop)) & Str(nTmpArr(nSubLoop)))
                    nCntItem = nCntItem + 1
                    If nTerm = nCntItem Then
                        GetSeqNum = nNuTmpArr(nArrItemCnt + 1)
                        Exit Function
                    End If
                End If
            Next nSubLoop
        Next nLoop
        
        Erase nTmpArr
        nTmpArr = nNuTmpArr
    Loop

End Function

Sub Task_02()

    Dim varInputNum
    
    Do
        varInputNum = InputBox("Please enter a positive number for the term of the sequence", strMyTitle, 60)
        If varInputNum = "" Then Exit Sub
    Loop Until varInputNum > 0
    
    MsgBox "The answer for the " & varInputNum & "th term of the sequence is: " & GetSeqNum(varInputNum)
    
End Sub