From 40638ae4192845aa5e1fc43e402f49aeb1e6074a Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Wed, 24 Nov 2021 00:25:02 +0000 Subject: - Added guest contributions by Eric Cheung. --- .../eric-cheung/excel-vba/Challenge_140.xlsm | Bin 0 -> 35022 bytes challenge-140/eric-cheung/excel-vba/ch-1.bas | 28 ++++++++++ challenge-140/eric-cheung/excel-vba/ch-2.bas | 57 +++++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100755 challenge-140/eric-cheung/excel-vba/Challenge_140.xlsm create mode 100755 challenge-140/eric-cheung/excel-vba/ch-1.bas create mode 100755 challenge-140/eric-cheung/excel-vba/ch-2.bas diff --git a/challenge-140/eric-cheung/excel-vba/Challenge_140.xlsm b/challenge-140/eric-cheung/excel-vba/Challenge_140.xlsm new file mode 100755 index 0000000000..6d255dd99b Binary files /dev/null and b/challenge-140/eric-cheung/excel-vba/Challenge_140.xlsm differ diff --git a/challenge-140/eric-cheung/excel-vba/ch-1.bas b/challenge-140/eric-cheung/excel-vba/ch-1.bas new file mode 100755 index 0000000000..b73559dd3f --- /dev/null +++ b/challenge-140/eric-cheung/excel-vba/ch-1.bas @@ -0,0 +1,28 @@ +Attribute VB_Name = "ModTask_01" +Option Explicit + +Public Const strMyTitle As String = "Eric Cheung" + +Function BinaryNumAdd(ByVal strBinNum_01 As String, ByVal strBinNum_02 As String) As String + + Dim wsFunc As Object + + Set wsFunc = Application.WorksheetFunction + + BinaryNumAdd = wsFunc.Dec2Bin(wsFunc.Bin2Dec(strBinNum_01) + wsFunc.Bin2Dec(strBinNum_02)) + + Set wsFunc = Nothing + +End Function + +Sub Task_01() + + Dim strMsg As String + + '' strMsg = BinaryNumAdd("11", "1") '' Example 1 + '' strMsg = BinaryNumAdd("101", "1") '' Example 2 + strMsg = BinaryNumAdd("100", "11") '' Example 3 + + MsgBox strMsg, vbOKOnly, strMyTitle + +End Sub diff --git a/challenge-140/eric-cheung/excel-vba/ch-2.bas b/challenge-140/eric-cheung/excel-vba/ch-2.bas new file mode 100755 index 0000000000..4530e5ce1f --- /dev/null +++ b/challenge-140/eric-cheung/excel-vba/ch-2.bas @@ -0,0 +1,57 @@ +Attribute VB_Name = "ModTask_02" +Option Explicit + +Sub Task_02() + + '' Example 1 +'' Const nNumRow As Integer = 2 +'' Const nNumCol As Integer = 3 +'' Const nElemNum As Integer = 4 + + '' Example 2 + Const nNumRow As Integer = 3 + Const nNumCol As Integer = 3 + Const nElemNum As Integer = 6 + + Dim strMsg As String + Dim nRowLoop As Integer, nColLoop As Integer, nLoop As Integer + Dim nArr(1 To nNumRow * nNumCol) As Integer + + '' Initialize + nLoop = 1 + For nRowLoop = 1 To nNumRow + For nColLoop = 1 To nNumCol + nArr(nLoop) = nRowLoop * nColLoop + nLoop = nLoop + 1 + Next nColLoop + Next nRowLoop + + '' Sort + For nRowLoop = 1 To nNumRow * nNumCol - 1 + For nColLoop = nRowLoop + 1 To nNumRow * nNumCol + If nArr(nRowLoop) > nArr(nColLoop) Then + nLoop = nArr(nRowLoop) + nArr(nRowLoop) = nArr(nColLoop) + nArr(nColLoop) = nLoop + End If + Next nColLoop + Next nRowLoop + + '' Display + strMsg = "Now the " & nElemNum + + If nElemNum = 1 Then + strMsg = strMsg & "st" + ElseIf nElemNum = 2 Then + strMsg = strMsg & "nd" + ElseIf nElemNum = 3 Then + strMsg = strMsg & "rd" + Else + strMsg = strMsg & "th" + End If + + strMsg = strMsg & " element in the table is " & nArr(nElemNum) + + MsgBox strMsg, vbOKOnly, strMyTitle + +End Sub -- cgit