aboutsummaryrefslogtreecommitdiff
path: root/challenge-134/eric-cheung/excel-vba/ch-2.bas
blob: d9aef24c1842abb64457ca910db6b5f18e8a9a15 (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
Attribute VB_Name = "ModTask_02"
Option Explicit
Public Const strMyTitle As String = "Eric Cheung"

Sub Task_02()

    '' Example 01:
    '' Const nRowMax As Integer = 3
    '' Const nColMax As Integer = 3
    
    '' Example 02:
    Const nRowMax As Integer = 3
    Const nColMax As Integer = 5

    Dim strMsg As String, strUniqCountMsg As String
    Dim nMatrixArr(1 To nRowMax, 1 To nColMax) As Integer
    Dim nRowLoop As Integer, nColLoop As Integer, nUniqCount As Integer
    Dim objDict As Object
    
    Set objDict = CreateObject("Scripting.Dictionary")
    
    nUniqCount = 0
    
    strMsg = "x |"
    
    For nColLoop = 1 To nColMax
        strMsg = strMsg & "  " & nColLoop
    Next nColLoop
    
    strMsg = strMsg & vbNewLine & "--+-----------"
    
    For nRowLoop = 1 To nRowMax
        strMsg = strMsg & vbNewLine & nRowLoop & " |"
        For nColLoop = 1 To nColMax
            nMatrixArr(nRowLoop, nColLoop) = nRowLoop * nColLoop
            strMsg = strMsg & "  " & nRowLoop * nColLoop
            If Not objDict.exists(nRowLoop * nColLoop) Then
                objDict.Add nRowLoop * nColLoop, 1
                If strUniqCountMsg <> "" Then
                    strUniqCountMsg = strUniqCountMsg & ", "
                End If
                strUniqCountMsg = strUniqCountMsg & nRowLoop * nColLoop
                nUniqCount = nUniqCount + 1
            End If
        Next nColLoop
    Next nRowLoop
    
    strUniqCountMsg = "Distinct Terms: " & strUniqCountMsg & vbNewLine & "Count: " & nUniqCount
    
    strMsg = strMsg & vbNewLine & vbNewLine & strUniqCountMsg

    MsgBox strMsg, vbOKOnly, strMyTitle
    
    Set objDict = Nothing

End Sub