From 98c4ecee7e067ccce54e89c83ae6502184f44758 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Wed, 3 Nov 2021 07:32:16 +0000 Subject: - Added guest contributions by Eric Cheung. --- .../eric-cheung/excel-vba/Challenge_137.xlsm | Bin 0 -> 35238 bytes challenge-137/eric-cheung/excel-vba/ch-1.bas | 51 +++++++++++++++++++++ challenge-137/eric-cheung/excel-vba/ch-2.bas | 47 +++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100755 challenge-137/eric-cheung/excel-vba/Challenge_137.xlsm create mode 100755 challenge-137/eric-cheung/excel-vba/ch-1.bas create mode 100755 challenge-137/eric-cheung/excel-vba/ch-2.bas diff --git a/challenge-137/eric-cheung/excel-vba/Challenge_137.xlsm b/challenge-137/eric-cheung/excel-vba/Challenge_137.xlsm new file mode 100755 index 0000000000..31373a147e Binary files /dev/null and b/challenge-137/eric-cheung/excel-vba/Challenge_137.xlsm differ diff --git a/challenge-137/eric-cheung/excel-vba/ch-1.bas b/challenge-137/eric-cheung/excel-vba/ch-1.bas new file mode 100755 index 0000000000..faee7a2cc6 --- /dev/null +++ b/challenge-137/eric-cheung/excel-vba/ch-1.bas @@ -0,0 +1,51 @@ +Attribute VB_Name = "ModTask_01" +Option Explicit + +Public Const strMyTitle As String = "Eric Cheung" + +Function FindWeekNumGivenDate(nInputDate As Long) As Integer + + '' Credit: + '' http://kbase.icbconsulting.com/vba/determine-the-week-number-using-a-date + + Dim nNum_01 As Integer, nNum_02 As Integer, nNum_03 As Long, nNum_04 As Integer + + If nInputDate < 1 Then + FindWeekNumGivenDate = 0 + Exit Function + End If + + nNum_01 = Weekday(nInputDate, vbSunday) + nNum_02 = Year(nInputDate + (8 - nNum_01) Mod 7 - 3) + nNum_03 = DateSerial(nNum_02, 1, 1) + nNum_04 = (Weekday(nNum_03, vbSunday) + 1) Mod 7 + FindWeekNumGivenDate = Int((nInputDate - nNum_03 - 3 + nNum_04) / 7) + 1 + +End Function + +Function FindNumWeekGivenYear(nInputYear As Integer) As Integer + + FindNumWeekGivenYear = FindWeekNumGivenDate(DateSerial(nInputYear, 12, 31)) + +End Function + +Sub Task_01() + + Const nStartYear As Integer = 1900 + Const nStartEnd As Integer = 2100 + + Dim strMsg As String + Dim nLoop As Integer + + For nLoop = nStartYear To nStartEnd + If FindNumWeekGivenYear(nLoop) = 53 Then + If strMsg <> "" Then + strMsg = strMsg & ", " + End If + strMsg = strMsg & nLoop + End If + Next nLoop + + MsgBox strMsg, vbOKOnly, strMyTitle + +End Sub diff --git a/challenge-137/eric-cheung/excel-vba/ch-2.bas b/challenge-137/eric-cheung/excel-vba/ch-2.bas new file mode 100755 index 0000000000..b6e2b6eebc --- /dev/null +++ b/challenge-137/eric-cheung/excel-vba/ch-2.bas @@ -0,0 +1,47 @@ +Attribute VB_Name = "ModTask_02" +Option Explicit + +Function IsPalindromeNum(ByVal strNumCheck As String) As Boolean + + IsPalindromeNum = False + If strNumCheck = StrReverse(strNumCheck) Then + IsPalindromeNum = True + End If + +End Function + +Sub Task_02() + + '' Credit + '' https://datagenetics.com/blog/october12015/index.html + + Const nInput As Integer = 56 '' Example 1 + '' Const nInput As Integer = 57 '' Example 2 + '' Const nInput As Integer = 59 '' Example 3 + + Const nMaxLoop As Integer = 500 + + Dim strMsg As String, strTemp As String + Dim nLoop As Integer + + strTemp = CStr(nInput) + strMsg = "1" + + For nLoop = 1 To nMaxLoop + + If Val(strTemp) > 10000000 Then + Exit For + End If + + If IsPalindromeNum(strTemp) Then + strMsg = "0" + Exit For + End If + + strTemp = CStr(Val(strTemp) + Val(StrReverse(strTemp))) + + Next nLoop + + MsgBox strMsg, vbOKOnly, strMyTitle + +End Sub -- cgit