' change log: ' 2023-09-20, update criteria and procedure ' 2023-09-19, update error handling ' 2023-09-18, update for "################" when zoom out Option Explicit Dim first_row As Integer Dim current_row As Integer Dim last_check_row As Integer ' Dim amount_in_func As String Dim starting_cell As String Dim grand_total_curr As Currency Dim grand_total_str As String Dim amount_in_func_curr As Currency Dim amount_in_func_str As String Dim pcco_str As String Dim ISIN_CODE_ESTD_COL As String Dim BOOK_COL As String Dim BUY_COL As String Dim SELL_COL As String Dim GRANDTOTAL_COL As String Dim PCCO_COL As String Dim AMOUNT_IN_FUNC_COL As String Dim BMF_ID_COL As String Dim ACCOUNT_COL As String Dim DEBIT_COL As String Dim CREDIT_COL As String Dim REMARKS_COL As String Dim PCEC_HEADER_CELL As String Dim HEADER_ROW As Integer Dim PCEC_COL As String Dim PCEC_COL_LABEL As String Dim ADJUSTMENT_WORKSHEET As String Dim ADJUSTMENT_TEST_WORKSHEET As String Dim AC_1020 As String Dim AC_8600 As String Dim AC_8601 As String Dim AC_8700 As String Dim AC_8701 As String Dim AC_1311 As String Dim AC_1375 As String Dim AC_2020 As String Dim PCEC_302210 As String Dim PCEC_921810 As String Dim PCEC_922910 As String Dim PCEC_921910 As String Dim PCEC_302410 As String Dim PCEC_922810 As String Dim PCCO_A1311000 As String Dim PCCO_92100100 As String Dim PCCO_98900100 As String Dim PCCO_98900200 As String Dim PCCO_P1375000 As String Dim PCCO_92200100 As String Dim current_row_value_valid As Boolean Dim check_next_row_ready As Boolean Dim check_row_format_valid As Boolean Sub init() On Error GoTo eh HEADER_ROW = 6 first_row = 7 current_row = first_row last_check_row = getLastCheckRow(current_row) ' amount_in_func = "J" ISIN_CODE_ESTD_COL = "C" BOOK_COL = "D" BUY_COL = "E" SELL_COL = "F" GRANDTOTAL_COL = "G" PCEC_COL = "I" ' after insert PCCO_COL = "J" AMOUNT_IN_FUNC_COL = "K" BMF_ID_COL = "L" ACCOUNT_COL = "N" DEBIT_COL = "O" CREDIT_COL = "P" REMARKS_COL = "R" PCEC_HEADER_CELL = PCEC_COL & CStr(HEADER_ROW) starting_cell = GRANDTOTAL_COL & first_row AC_1020 = "1020" AC_8600 = "8600" AC_8601 = "8601" AC_8700 = "8700" AC_8701 = "8701" AC_1311 = "1311" AC_1375 = "1375" AC_2020 = "2020" PCEC_302210 = "302210" PCEC_921810 = "921810" PCEC_922810 = "922810" PCEC_922910 = "922910" PCEC_921910 = "921910" PCEC_302410 = "302410" PCCO_A1311000 = "A1311000" PCCO_92100100 = "92100100" PCCO_98900100 = "98900100" PCCO_98900200 = "98900200" PCCO_P1375000 = "P1375000" PCCO_92200100 = "92200100" 'ADJUSTMENT_WORKSHEET = "Adjustment_TEST" ADJUSTMENT_WORKSHEET = "Adjustment" PCEC_COL_LABEL = "PCEC" Done: Exit Sub eh: Debug.Print "init:Error: " & Err.Description End Sub Function fillRemarksLightBlue(current_row As Integer) On Error GoTo eh Worksheets(ADJUSTMENT_WORKSHEET).Range(REMARKS_COL & CStr(current_row)).Interior.Color = rgb(9, 132, 227) Worksheets(ADJUSTMENT_WORKSHEET).Range(REMARKS_COL & CStr(current_row)).Font.Color = vbWhite Done: Exit Function eh: Debug.Print "fillRemarksLightBlue:Error: " & Err.Description End Function Function fillRemarksLightGreen(current_row As Integer) On Error GoTo eh Worksheets(ADJUSTMENT_WORKSHEET).Range(REMARKS_COL & CStr(current_row)).Interior.Color = rgb(39, 174, 96) Worksheets(ADJUSTMENT_WORKSHEET).Range(REMARKS_COL & CStr(current_row)).Font.Color = vbWhite Done: Exit Function eh: Debug.Print "fillRemarksLightGreen:Error: " & Err.Description End Function Function fillRemarksLightRed(current_row As Integer) On Error GoTo eh Worksheets(ADJUSTMENT_WORKSHEET).Range(REMARKS_COL & CStr(current_row)).Interior.Color = rgb(214, 48, 49) Worksheets(ADJUSTMENT_WORKSHEET).Range(REMARKS_COL & CStr(current_row)).Font.Color = vbWhite Done: Exit Function eh: Debug.Print "fillRemarksLightRed:Error: " & Err.Description End Function Function fillRemarksLightPurple(current_row As Integer) On Error GoTo eh Worksheets(ADJUSTMENT_WORKSHEET).Range(REMARKS_COL & CStr(current_row)).Interior.Color = rgb(108, 92, 231) Worksheets(ADJUSTMENT_WORKSHEET).Range(REMARKS_COL & CStr(current_row)).Font.Color = vbWhite Done: Exit Function eh: Debug.Print "fillRemarksLightPurple:Error: " & Err.Description End Function Function fillRemarksLightYellow(current_row As Integer) On Error GoTo eh Worksheets(ADJUSTMENT_WORKSHEET).Range(REMARKS_COL & CStr(current_row)).Interior.Color = rgb(253, 203, 110) Worksheets(ADJUSTMENT_WORKSHEET).Range(REMARKS_COL & CStr(current_row)).Font.Color = vbWhite Done: Exit Function eh: Debug.Print "fillRemarksLightYellow:Error: " & Err.Description End Function Function fillRemarksLightCyan(current_row As Integer) On Error GoTo eh Worksheets(ADJUSTMENT_WORKSHEET).Range(REMARKS_COL & CStr(current_row)).Interior.Color = rgb(0, 206, 201) Worksheets(ADJUSTMENT_WORKSHEET).Range(REMARKS_COL & CStr(current_row)).Font.Color = vbWhite Done: Exit Function eh: Debug.Print "fillRemarksLightCyan:Error: " & Err.Description End Function Function fillRemarksLightPink(current_row As Integer) On Error GoTo eh Worksheets(ADJUSTMENT_WORKSHEET).Range(REMARKS_COL & CStr(current_row)).Interior.Color = rgb(232, 67, 147) Worksheets(ADJUSTMENT_WORKSHEET).Range(REMARKS_COL & CStr(current_row)).Font.Color = vbWhite Done: Exit Function eh: Debug.Print "fillRemarksLightPink:Error: " & Err.Description End Function Function writeRemarks(current_row As Integer, remarks As String) On Error GoTo eh Worksheets(ADJUSTMENT_WORKSHEET).Range(REMARKS_COL & CStr(current_row)).Value = remarks Done: Exit Function eh: Debug.Print "writeRemarks:Error: " & Err.Description End Function Function appendRemarks(current_row As Integer, remarks As String) On Error GoTo eh Dim original_string As String original_string = Worksheets(ADJUSTMENT_WORKSHEET).Range(REMARKS_COL & CStr(current_row)).Value Worksheets(ADJUSTMENT_WORKSHEET).Range(REMARKS_COL & CStr(current_row)).Value = remarks & "," & original_string Done: Exit Function eh: Debug.Print "appendRemarks:Error: " & Err.Description End Function Function writeCredit(current_row As Integer, credit As Variant) On Error GoTo eh Worksheets(ADJUSTMENT_WORKSHEET).Range(CREDIT_COL & CStr(current_row)).Value = Abs(credit) Worksheets(ADJUSTMENT_WORKSHEET).Range(CREDIT_COL & CStr(current_row)).NumberFormat = "#,##0.00" Done: Exit Function eh: Debug.Print "writeCredit:Error: " & Err.Description End Function Function writeDebit(current_row As Integer, debit As Variant) On Error GoTo eh Worksheets(ADJUSTMENT_WORKSHEET).Range(DEBIT_COL & CStr(current_row)).Value = Abs(debit) Worksheets(ADJUSTMENT_WORKSHEET).Range(DEBIT_COL & CStr(current_row)).NumberFormat = "#,##0.00" Done: Exit Function eh: Debug.Print "writeDebit:Error: " & Err.Description End Function Function writePCCO(current_row As Integer, pcec As String) On Error GoTo eh Worksheets(ADJUSTMENT_WORKSHEET).Range(PCCO_COL & CStr(current_row)).Value = pcec Done: Exit Function eh: Debug.Print "writePCCO:Error: " & Err.Description End Function Function writePCEC(current_row As Integer, pcec As String) On Error GoTo eh Worksheets(ADJUSTMENT_WORKSHEET).Range(PCEC_COL & CStr(current_row)).Value = pcec Done: Exit Function eh: Debug.Print "writePCEC:Error: " & Err.Description End Function Function writeAccount(current_row As Integer, account As String) On Error GoTo eh Worksheets(ADJUSTMENT_WORKSHEET).Range(ACCOUNT_COL & CStr(current_row)).Value = account Done: Exit Function eh: Debug.Print "writeAccount:Error: " & Err.Description End Function Function getPCCO(current_row As Integer) On Error GoTo eh Dim output As String output = Worksheets(ADJUSTMENT_WORKSHEET).Range(PCCO_COL & CStr(current_row)).Value Done: getPCCO = output Exit Function eh: Debug.Print "getPCCO:Error: " & Err.Description End Function Function getGrandTotal(current_row As Integer) On Error GoTo eh Dim output As String output = Worksheets(ADJUSTMENT_WORKSHEET).Range(GRANDTOTAL_COL & CStr(current_row)).Text If (InStr(output, "###") >= 0) Then output = Worksheets(ADJUSTMENT_WORKSHEET).Range(GRANDTOTAL_COL & CStr(current_row)).Value End If Done: getGrandTotal = output Exit Function eh: Debug.Print "getGrandTotal:Error: " & Err.Description & ":" & CStr(current_row) End Function Function getAccount(current_row As Integer) On Error GoTo eh Dim output As String output = Worksheets(ADJUSTMENT_WORKSHEET).Range(ACCOUNT_COL & CStr(current_row)).Text If (InStr(output, "###") >= 0) Then output = Worksheets(ADJUSTMENT_WORKSHEET).Range(ACCOUNT_COL & CStr(current_row)).Value End If Done: getAccount = output Exit Function eh: Debug.Print "getAccount:Error: " & Err.Description & ":" & CStr(current_row) appendRemarks current_row, "account format error, skipping" End Function Function getAmountInFunc(current_row As Integer) On Error GoTo eh Dim output As String output = Worksheets(ADJUSTMENT_WORKSHEET).Range(AMOUNT_IN_FUNC_COL & CStr(current_row)).Text If (InStr(output, "###") >= 0) Then output = Worksheets(ADJUSTMENT_WORKSHEET).Range(AMOUNT_IN_FUNC_COL & CStr(current_row)).Value End If Done: getAmountInFunc = output Exit Function eh: 'MsgBox "getAmountInFunc:Error: " & Err.Description & ":" & CStr(current_row) appendRemarks current_row, "amount in func format error, skipping" Err.Raise "Error in amount_in_func_format" End Function Function checkOutputCellReady(current_row As Integer) On Error GoTo eh Dim output As Boolean output = True Dim account_cell As Variant account_cell = Worksheets(ADJUSTMENT_WORKSHEET).Range(ACCOUNT_COL & CStr(current_row)) If (IsEmpty(account_cell) = False) Then output = False End If Done: checkOutputCellReady = output Exit Function eh: Debug.Print "checkOutputCellReady:Error: " & Err.Description End Function Function checkCellFormat(test_cell As Variant) On Error GoTo eh Dim Msg As String Msg = "no match" Select Case IsEmpty(test_cell) Case True Msg = "General." Case Else Select Case IsNumeric(test_cell) Case True Msg = "Number." Case Else Select Case IsDate(test_cell) Case True Msg = "Date." Case Else Msg = "Text." End Select End Select End Select ' debug.print "checkCellFormat:" & test_cell.Address & ":" & Msg Done: checkCellFormat = Msg Exit Function eh: Debug.Print "checkCellFormat:Error: " & Err.Description End Function Function checkCurrentRowFormatValid(current_row As Integer) On Error GoTo eh Dim test_cell As Variant Dim output As Boolean output = True test_cell = Worksheets(ADJUSTMENT_WORKSHEET).Range(ISIN_CODE_ESTD_COL & CStr(current_row)) If "Text." = checkCellFormat(test_cell) Then Debug.Print "ISIN_CODE_ESTD_COL:check ok" Else Debug.Print "ISIN_CODE_ESTD_COL:check failed" Debug.Print checkCellFormat(test_cell) output = False End If test_cell = Worksheets(ADJUSTMENT_WORKSHEET).Range(BOOK_COL & CStr(current_row)) If "Text." = checkCellFormat(test_cell) Then Debug.Print "BOOK_COL:check ok" Else Debug.Print "BOOK_COL:check failed" Debug.Print checkCellFormat(test_cell) output = False End If test_cell = Worksheets(ADJUSTMENT_WORKSHEET).Range(BUY_COL & CStr(current_row)) If "Number." = checkCellFormat(test_cell) or "General." = checkCellFormat(test_cell) Then Debug.Print "BUY_COL:check ok" Else Debug.Print "BUY_COL:check failed" appendRemarks current_row, "BUY_COL:check failed" Debug.Print checkCellFormat(test_cell) output = False End If test_cell = Worksheets(ADJUSTMENT_WORKSHEET).Range(GRANDTOTAL_COL & CStr(current_row)) If "Number." = checkCellFormat(test_cell) Then Debug.Print "GRANDTOTAL_COL:check ok" Else Debug.Print "GRANDTOTAL_COL:check failed" Debug.Print checkCellFormat(test_cell) output = False End If test_cell = Worksheets(ADJUSTMENT_WORKSHEET).Range(PCCO_COL & CStr(current_row)) If "Text." = checkCellFormat(test_cell) Then Debug.Print "PCCO_COL:check ok" Else Debug.Print "PCCO_COL:check failed" Debug.Print checkCellFormat(test_cell) output = False End If test_cell = Worksheets(ADJUSTMENT_WORKSHEET).Range(AMOUNT_IN_FUNC_COL & CStr(current_row)) If "Number." = checkCellFormat(test_cell) Then Debug.Print "AMOUNT_IN_FUNC_COL:check ok" Else Debug.Print "AMOUNT_IN_FUNC_COL:check failed" appendRemarks current_row, "AMOUNT_IN_FUNC_COL:check failed" Debug.Print checkCellFormat(test_cell) output = False End If test_cell = Worksheets(ADJUSTMENT_WORKSHEET).Range(BMF_ID_COL & CStr(current_row)) If "Number." = checkCellFormat(test_cell) or "Text." = checkCellFormat(test_cell) Then Debug.Print "BMF_ID_COL:check ok" Else Debug.Print "BMF_ID_COL:check failed" appendRemarks current_row, "BMF_ID_COL:check failed" Debug.Print checkCellFormat(test_cell) output = False End If test_cell = Worksheets(ADJUSTMENT_WORKSHEET).Range(ACCOUNT_COL & CStr(current_row)) If "General." = checkCellFormat(test_cell) Then Debug.Print "ACCOUNT_COL:check ok" Else Debug.Print "ACCOUNT_COL:check failed" Debug.Print checkCellFormat(test_cell) output = False End If test_cell = Worksheets(ADJUSTMENT_WORKSHEET).Range(DEBIT_COL & CStr(current_row)) If "General." = checkCellFormat(test_cell) Then Debug.Print "DEBIT_COL:check ok" Else Debug.Print "DEBIT_COL:check failed" Debug.Print checkCellFormat(test_cell) output = False End If test_cell = Worksheets(ADJUSTMENT_WORKSHEET).Range(CREDIT_COL & CStr(current_row)) If "General." = checkCellFormat(test_cell) Then Debug.Print "CREDIT_COL:check ok" Else Debug.Print "CREDIT_COL:check failed" Debug.Print checkCellFormat(test_cell) output = False End If test_cell = Worksheets(ADJUSTMENT_WORKSHEET).Range(REMARKS_COL & CStr(current_row)) If "General." = checkCellFormat(test_cell) or "Text." = checkCellFormat(test_cell) Then Debug.Print "REMARKS_COL:check ok" Else Debug.Print "REMARKS_COL:check failed" Debug.Print checkCellFormat(test_cell) output = False End If Done: checkCurrentRowFormatValid = output Exit Function eh: Debug.Print "checkCurrentRowFormatValid:Error: " & Err.Description End Function Function checkCurrentRowValueValid(current_row As Integer) On Error GoTo eh Dim output As Boolean output = True If (checkIfGrandTotalRefNotFound(current_row) = True) Then Debug.Print "grand total ref not found" appendRemarks current_row, "grand total reference not found, skipping" output = False End If If (checkIfAmountInFuncRefNotFound(current_row) = True) Then Debug.Print "amount in func ref not found" appendRemarks current_row, "amount in func reference not found, skipping" output = False End If If (checkIfPCCORefNotFound(current_row) = True) Then Debug.Print "PCCO ref not found" appendRemarks current_row, "PCCO reference not found, skipping" output = False End If If output Then Dim grand_total_str As String Dim amount_in_func_str As String Dim pcco_str As String grand_total_str = getGrandTotal(current_row) amount_in_func_str = getAmountInFunc(current_row) pcco_str = getPCCO(current_row) ' Debug.print InStr(pcco_str, "Error") >= 0 ' Debug.print InStr(amount_in_func_str, "N/A") >= 0 ' Debug.print InStr(grand_total_str, "N/A") >= 0 If (Trim(pcco_str) = "" Or InStr(pcco_str, "N/A") > 0 Or InStr(pcco_str, "Error") > 0) Then output = False End If If (Trim(amount_in_func_str) = "" Or InStr(amount_in_func_str, "N/A") > 0 Or InStr(amount_in_func_str, "Error") > 0) Then output = False End If If (Trim(grand_total_str) = "" Or InStr(grand_total_str, "N/A") > 0 Or InStr(grand_total_str, "Error") > 0) Then output = False End If Else Debug.Print "skipping as grand total ref not found" End If Done: checkCurrentRowValueValid = output Exit Function eh: 'MsgBox "checkCurrentRowValueValid:Error: " & Err.Description 'Stop appendRemarks current_row, "row value not valid" checkCurrentRowValueValid = False End Function Function checkFullFillCasePCCOCode_P1375000Situation_1(grand_total As Currency, amount_in_func As Currency) On Error GoTo eh ' ### Special case (if PCCO Code = P1375000): ' ### Situation 1: ' if Grand total > 0 Dim output As Boolean output = (grand_total > 0) Done: checkFullFillCasePCCOCode_P1375000Situation_1 = output Exit Function eh: Debug.Print "checkFullFillCasePCCOCode_P1375000Situation_1:Error: " & Err.Description End Function Function checkFullFillCasePCCOCode_P1375000Situation_2(grand_total As Currency, amount_in_func As Currency) On Error GoTo eh ' ### Special case (if PCCO Code = P1375000): ' ### Situation 2: ' if Grand total < 0 Dim output As Boolean output = (grand_total < 0) Done: checkFullFillCasePCCOCode_P1375000Situation_2 = output Exit Function eh: Debug.Print "checkFullFillCasePCCOCode_P1375000Situation_2:Error: " & Err.Description End Function Function checkFullFillCasePCCOCode_P1375000Situation_3(grand_total As Currency, amount_in_func As Currency) On Error GoTo eh ' ### Special case (if PCCO Code = P1375000): ' ### Situation 3: ' if Grand total = 0 ' - do nothing Dim output As Boolean output = (grand_total = 0) Done: checkFullFillCasePCCOCode_P1375000Situation_3 = output Exit Function eh: Debug.Print "checkFullFillCasePCCOCode_P1375000Situation_3:Error: " & Err.Description End Function Function checkFullFillCaseCommonSituation_1(grand_total As Currency, amount_in_func As Currency) On Error GoTo eh ' ### Situation 1: ' if Grand total > 0 (row 8 ?) Dim output As Boolean output = (grand_total > 0) Done: checkFullFillCaseCommonSituation_1 = output Exit Function eh: Debug.Print "checkFullFillCaseCommonSituation_1:Error: " & Err.Description End Function Function checkFullFillCaseCommonSituation_2(grand_total As Currency, amount_in_func As Currency) On Error GoTo eh ' ### Situation 2: ' Grand total < 0 Dim output As Boolean output = (grand_total < 0) Done: checkFullFillCaseCommonSituation_2 = output Exit Function eh: Debug.Print "checkFullFillCaseCommonSituation_2:Error: " & Err.Description End Function Function checkFullFillCaseCommonSituation_3(grand_total As Currency, amount_in_func As Currency) On Error GoTo eh ' ### Situation 3: ' if Grand total = 0 Dim output As Boolean output = (grand_total = 0) Done: checkFullFillCaseCommonSituation_3 = output Exit Function eh: Debug.Print "checkFullFillCaseCommonSituation_3:Error: " & Err.Description End Function Function checkIfReferenceNotFound(cell As Variant) On Error GoTo eh Dim test_text As Variant test_text = cell.Text Dim output As Boolean output = (InStr(test_text, "#REF!") > 0) Done: checkIfReferenceNotFound = output Exit Function eh: Debug.Print "checkIfReferenceNotFound:Error: " & Err.Description End Function Function checkIfGrandTotalRefNotFound(current_row As Integer) On Error GoTo eh Dim test_cell As Variant test_cell = Worksheets(ADJUSTMENT_WORKSHEET).Range(GRANDTOTAL_COL & CStr(current_row)) Done: checkIfGrandTotalRefNotFound = checkIfReferenceNotFound(Worksheets(ADJUSTMENT_WORKSHEET).Range(GRANDTOTAL_COL & CStr(current_row))) Exit Function eh: Debug.Print "checkIfGrandTotalRefNotFound:Error: " & Err.Description & ":" & GRANDTOTAL_COL & CStr(current_row) End Function Function checkIfPCCORefNotFound(current_row As Integer) On Error GoTo eh Dim test_cell As Variant test_cell = Worksheets(ADJUSTMENT_WORKSHEET).Range(PCCO_COL & CStr(current_row)) Done: checkIfPCCORefNotFound = checkIfReferenceNotFound(Worksheets(ADJUSTMENT_WORKSHEET).Range(PCCO_COL & CStr(current_row))) Exit Function eh: Debug.Print "checkIfPCCORefNotFound:Error: " & Err.Description & ":" & PCCO_COL & CStr(current_row) End Function Function checkIfAmountInFuncRefNotFound(current_row As Integer) On Error GoTo eh Dim test_cell As Variant test_cell = Worksheets(ADJUSTMENT_WORKSHEET).Range(AMOUNT_IN_FUNC_COL & CStr(current_row)) Done: checkIfAmountInFuncRefNotFound = checkIfReferenceNotFound(Worksheets(ADJUSTMENT_WORKSHEET).Range(AMOUNT_IN_FUNC_COL & CStr(current_row))) Exit Function eh: Debug.Print "checkIfAmountInFuncRefNotFound:Error: " & Err.Description & ":" & AMOUNT_IN_FUNC_COL & CStr(current_row) End Function Function applyCommonSituation_1(current_row As Integer) On Error GoTo eh Dim net_var As Variant Dim grand_total_var As Variant Dim amount_in_func_var As Variant grand_total_var = CDec(getGrandTotal(current_row)) amount_in_func_var = CDec(getAmountInFunc(current_row)) net_var = grand_total_var + amount_in_func_var ' current row ' - PCEC(Column I): 302210, PCCO: A1311000, Account column: 1020, debit column: Grand Total writePCEC current_row, PCEC_302210 writePCCO current_row, PCCO_A1311000 writeAccount current_row, AC_1020 writeDebit current_row, grand_total_var ' - add row: insertNewRowBelow current_row current_row = current_row + 1 ' - PCEC: 921810, ' - PCCO: 92100100 ' - account:8600, ' - debit: grand total writePCEC current_row, PCEC_921810 writePCCO current_row, PCCO_92100100 writeAccount current_row, AC_8600 writeDebit current_row, grand_total_var ' - add row: insertNewRowBelow current_row current_row = current_row + 1 ' - PCEC: 922910, ' - PCCO: 98900100 ' - account:8601, ' - Credit: grand total writePCEC current_row, PCEC_922910 writePCCO current_row, PCCO_98900100 writeAccount current_row, AC_8601 writeCredit current_row, grand_total_var ' - add row: ' - account NA8601, ' - credit: grand total + amount in func ' insertNewRowBelow current_row ' current_row = current_row + 1 ' writeAccount current_row, AC_8601 ' writeCredit current_row, net_var Done: applyCommonSituation_1 = True Exit Function eh: Debug.Print "applyCommonSituation_1:Error: " & Err.Description End Function Function applyCommonSituation_2(current_row As Integer) On Error GoTo eh Dim net_var As Variant Dim grand_total_var As Variant Dim amount_in_func_var As Variant grand_total_var = CDec(getGrandTotal(current_row)) amount_in_func_var = CDec(getAmountInFunc(current_row)) net_var = grand_total_var + amount_in_func_var ' - current_row: ' - PCEC(Column I): 302210, ' - PCCO: A1311000, ' - Account column: 1020, ' - credit column: Grand Total writePCEC current_row, PCEC_302210 writePCCO current_row, PCCO_A1311000 writeAccount current_row, AC_1020 writeCredit current_row, grand_total_var ' - add row: insertNewRowBelow current_row current_row = current_row + 1 ' - PCEC: 922810, ' - PCCO: 92200100, ' - account:8700, ' - credit: grand total writePCEC current_row, PCEC_921810 writePCCO current_row, PCCO_92100100 writeAccount current_row, AC_8700 writeCredit current_row, grand_total_var ' - add row: insertNewRowBelow current_row current_row = current_row + 1 ' - PCEC: 921910, ' - PCCO: 98900200, ' - account:8701, ' - debit: grand total writePCEC current_row, PCEC_921910 writePCCO current_row, PCCO_98900200 writeAccount current_row, AC_8701 writeDebit current_row, grand_total_var ' ' - add row: ' ' - account NA8701, ' ' - debit: grand total + amount in func ' insertNewRowBelow current_row ' current_row = current_row + 1 ' writeAccount current_row, AC_8701 ' writeDebit current_row, net_var Done: applyCommonSituation_2 = True Exit Function eh: Debug.Print "applyCommonSituation_2:Error: " & Err.Description End Function Function applyCommonSituation_3(current_row As Integer) On Error GoTo eh ' if Grand total + amount in func = 0 ' - do nothing applyCommonSituation_3 = True Done: applyCommonSituation_3 = True Exit Function eh: Debug.Print "applyCommonSituation_3:Error: " & Err.Description End Function Function applyPcco1375Situation_1(current_row As Integer) On Error GoTo eh Dim net_var As Variant Dim grand_total_var As Variant Dim amount_in_func_var As Variant grand_total_var = CDec(getGrandTotal(current_row)) amount_in_func_var = CDec(getAmountInFunc(current_row)) net_var = grand_total_var + amount_in_func_var ' - current row ' - PCEC(Column I): 302410, ' - PCCO: P1375000, ' - Account column: 2020, ' - debit column: Grand Total writePCEC current_row, PCEC_302210 writePCCO current_row, PCCO_P1375000 writeAccount current_row, AC_2020 writeDebit current_row, grand_total_var ' - add row: insertNewRowBelow current_row current_row = current_row + 1 ' - PCEC: 921810, ' - PCCO: 92100100, ' - account:8600, ' - debit: grand total writePCEC current_row, PCEC_921810 writePCCO current_row, PCCO_92100100 writeAccount current_row, AC_8600 writeDebit current_row, grand_total_var ' - add row: insertNewRowBelow current_row current_row = current_row + 1 ' - PCEC: 922910, ' - PCCO: 98900100, ' - account:8601, ' - credit: grand total writePCEC current_row, PCEC_922910 writePCCO current_row, PCCO_98900100 writeAccount current_row, AC_8601 writeCredit current_row, grand_total_var ' ' - add row: ' ' - account NA8601, ' ' - credit: grand total + amount in func ' insertNewRowBelow current_row ' current_row = current_row + 1 ' writeAccount current_row, AC_8601 ' writeCredit current_row, net_var applyPcco1375Situation_1 = True Done: applyPcco1375Situation_1 = True Exit Function eh: Debug.Print "applyPcco1375Situation_1:Error: " & Err.Description End Function Function applyPcco1375Situation_2(current_row As Integer) On Error GoTo eh Dim net_var As Variant Dim grand_total_var As Variant Dim amount_in_func_var As Variant grand_total_var = CDec(getGrandTotal(current_row)) amount_in_func_var = CDec(getAmountInFunc(current_row)) net_var = grand_total_var + amount_in_func_var ' - Account ' - PCEC(Column I): 302410, ' - PCCO: P1375000, ' - Account column: 2020, ' - credit column: Grand Total writePCEC current_row, PCEC_302410 writePCCO current_row, PCCO_P1375000 writeAccount current_row, AC_2020 writeCredit current_row, grand_total_var ' - add row: insertNewRowBelow current_row current_row = current_row + 1 ' - PCEC: 922810, ' - PCCO: 92200100, ' - account:8700, ' - credit: grand total writePCEC current_row, PCEC_922810 writePCCO current_row, PCCO_92200100 writeAccount current_row, AC_8700 writeCredit current_row, grand_total_var ' - add row: insertNewRowBelow current_row current_row = current_row + 1 ' - PCEC: 921910, ' - PCCO: 98900200, ' - account:8701, ' - debit: grand total writePCEC current_row, PCEC_921910 writePCCO current_row, PCCO_98900200 writeAccount current_row, AC_8701 writeDebit current_row, grand_total_var ' ' - add row: ' ' - account 8701, ' ' - debit: grand total + amount in func ' insertNewRowBelow current_row ' current_row = current_row + 1 ' writeAccount current_row, AC_8701 ' writeDebit current_row, net_var applyPcco1375Situation_2 = True Done: applyPcco1375Situation_2 = True Exit Function eh: Debug.Print "applyPcco1375Situation_2:Error: " & Err.Description End Function Function applyPcco1375Situation_3(current_row As Integer) On Error GoTo eh ' - do nothing applyPcco1375Situation_3 = True Done: applyPcco1375Situation_3 = True Exit Function eh: Debug.Print "applyPcco1375Situation_3:Error: " & Err.Description End Function Sub test() ' Debug.Assert True = isCurrentRowNeedToProcess("7") ' Debug.Assert False = isCurrentRowNeedToProcess("180") ' debug.assert False = isAllDone ("7") ' debug.assert True = isAllDone ("180") ' debug.assert False = checkFullFillCaseCommonSituation_1(-10,1) ' debug.assert False = checkFullFillCaseCommonSituation_1(1,-10) ' debug.assert False = checkFullFillCaseCommonSituation_1(-10,-10) ' debug.assert False = checkFullFillCaseCommonSituation_1(0,0) ' debug.assert True = checkFullFillCaseCommonSituation_1(0,1) ' debug.assert True = checkFullFillCaseCommonSituation_1(1,0) ' debug.assert True = checkFullFillCaseCommonSituation_1(1,1) ' debug.assert True = checkFullFillCaseCommonSituation_2(-10,1) ' debug.assert True = checkFullFillCaseCommonSituation_2(1,-10) ' debug.assert True = checkFullFillCaseCommonSituation_2(-10,-10) ' debug.assert False = checkFullFillCaseCommonSituation_2(0,0) ' debug.assert False = checkFullFillCaseCommonSituation_2(0,1) ' debug.assert False = checkFullFillCaseCommonSituation_2(1,0) ' debug.assert False = checkFullFillCaseCommonSituation_2(1,1) ' debug.assert True = checkFullFillCaseCommonSituation_3(0,0) ' debug.assert False = checkFullFillCaseCommonSituation_3(1,1) ' debug.assert False = checkFullFillCaseCommonSituation_3(-10,-10) ' debug.assert False = checkFullFillCaseCommonSituation_3(1,0) ' debug.assert False = checkFullFillCaseCommonSituation_3(0,1) ' debug.print Worksheets("Adjustment_TEST").Range("G86") init current_row = 29 ' amount_in_func_str = getAmountInFunc(current_row) ' pcco_str = getPCCO(current_row) ' debug.print current_row ' debug.print grand_total_str ' debug.print checkIfGrandTotalRefNotFound(current_row) Debug.Print checkCurrentRowValueValid(current_row) ' current_row_value_valid = False ' current_row_value_valid = checkCurrentRowValueValid(pcco_str, amount_in_func_str, grand_total_str) ' check_next_row_ready = False ' check_next_row_ready = checkOutputCellReady(current_row) End Sub Function getLastCheckRow(first_row As Integer) getLastCheckRow = first_row + 10 End Function Function insertNewRowBelow(current_row As Integer) Range("A" & current_row + 1).EntireRow.Insert End Function Function isAllDone(input_row As Integer) On Error GoTo eh Dim cell_value As Currency Dim output As Boolean Dim i As Integer output = True last_check_row = getLastCheckRow(input_row) For i = input_row To last_check_row If (isCurrentRowNeedToProcess(i) = True) Then output = False Exit For End If Next i isAllDone = output Done: isAllDone = output Exit Function eh: Debug.Print "isAllDone:Error: " & Err.Description End Function Function isCurrentRowNeedToProcess(input_row As Integer) On Error GoTo eh Dim output As Boolean Dim cell_value As Currency Dim input_cell As String input_cell = GRANDTOTAL_COL & input_row Done: isCurrentRowNeedToProcess = Not (IsEmpty(Worksheets(ADJUSTMENT_WORKSHEET).Range(input_cell).Value)) Exit Function eh: Debug.Print "isCurrentRowNeedToProcess:Error: " & Err.Description End Function Sub Main() On Error GoTo eh Debug.Print "start a new run" init If (checkPCECColumnExist) Then writeRemarks HEADER_ROW, "PCEC col exist" Else createPCECCol "hello" writeRemarks HEADER_ROW, "PCEC col created" End If While isAllDone(current_row) = False ' While current_row < 9 Debug.Print "processing row: " & current_row Debug.Print checkCurrentRowFormatValid(current_row) current_row_value_valid = False current_row_value_valid = checkCurrentRowValueValid(current_row) check_next_row_ready = False check_next_row_ready = checkOutputCellReady(current_row) check_row_format_valid = False check_row_format_valid = checkCurrentRowFormatValid(current_row) If (current_row_value_valid And check_next_row_ready And check_row_format_valid) Then ' input considered valid after this line Dim grand_total_str As String Dim amount_in_func_str As String Dim pcco_str As String grand_total_str = getGrandTotal(current_row) amount_in_func_str = getAmountInFunc(current_row) pcco_str = getPCCO(current_row) Debug.Print "before" Debug.Print grand_total_str Debug.Print "pass" grand_total_curr = CCur(grand_total_str) amount_in_func_curr = CCur(amount_in_func_str) If (pcco_str = "P1375000") Then If (checkFullFillCasePCCOCode_P1375000Situation_1(grand_total_curr, amount_in_func_curr) = True) Then writeRemarks current_row, "P1375000 common sit 1" fillRemarksLightPurple current_row applyPcco1375Situation_1 (current_row) ElseIf (checkFullFillCasePCCOCode_P1375000Situation_2(grand_total_curr, amount_in_func_curr) = True) Then writeRemarks current_row, "P1375000 common sit 2" fillRemarksLightYellow current_row applyPcco1375Situation_2 (current_row) ElseIf (checkFullFillCasePCCOCode_P1375000Situation_3(grand_total_curr, amount_in_func_curr) = True) Then writeRemarks current_row, "P1375000 common sit 3" fillRemarksLightCyan current_row applyPcco1375Situation_3 (current_row) Else Debug.Print "missing pcco 1375 situation" End If ElseIf (pcco_str = "N/A") Then Debug.Print "pcco value is N/A" Else If (checkFullFillCaseCommonSituation_1(grand_total_curr, amount_in_func_curr) = True) Then writeRemarks current_row, "common sit 1" fillRemarksLightBlue current_row applyCommonSituation_1 (current_row) ElseIf (checkFullFillCaseCommonSituation_2(grand_total_curr, amount_in_func_curr) = True) Then writeRemarks current_row, "common sit 2" fillRemarksLightGreen current_row applyCommonSituation_2 (current_row) ElseIf (checkFullFillCaseCommonSituation_3(grand_total_curr, amount_in_func_curr) = True) Then writeRemarks current_row, "common sit 3" fillRemarksLightPink current_row applyCommonSituation_3 (current_row) Else Debug.Print "missing situation" End If End If Else If getAccount(current_row) = AC_1020 Then appendRemarks current_row, "output row" ElseIf getAccount(current_row) = AC_8600 Then appendRemarks current_row, "output row" ElseIf getAccount(current_row) = AC_8601 Then appendRemarks current_row, "output row" ElseIf getAccount(current_row) = AC_8700 Then appendRemarks current_row, "output row" ElseIf getAccount(current_row) = AC_8701 Then appendRemarks current_row, "output row" ElseIf getAccount(current_row) = AC_1311 Then appendRemarks current_row, "output row" ElseIf getAccount(current_row) = AC_1375 Then appendRemarks current_row, "output row" ElseIf getAccount(current_row) = AC_2020 Then appendRemarks current_row, "output row" Else Debug.Print "input is not valid, skipping row" fillRemarksLightRed current_row appendRemarks current_row, "input is not valid, skipping row" If (checkOutputCellReady(current_row)) Then If (checkCurrentRowValueValid(current_row)) Then Else ' appendRemarks current_row, "input is not valid" End If End If End If End If current_row = current_row + 1 Wend Done: Debug.Print "done" Exit Sub eh: writeRemarks current_row, "error found" 'MsgBox "Main:Error: " & Err.Description 'Stop End Sub Function createPCECCol(test) On Error GoTo eh Dim temp As String 'temp = Worksheets(ADJUSTMENT_WORKSHEET).Range(PCEC_HEADER_CELL).Text Worksheets(ADJUSTMENT_WORKSHEET).Range(PCEC_COL & ":" & PCEC_COL).EntireColumn.Insert Worksheets(ADJUSTMENT_WORKSHEET).Range(PCEC_HEADER_CELL).Value = PCEC_COL_LABEL Done: writeRemarks HEADER_ROW, "PCEC col created" Debug.Print "createPCECCol:done" Exit Function eh: writeRemarks 1, "createPCECCol:error found" 'MsgBox "Main:Error: " & Err.Description 'Stop End Function Function checkPCECColumnExist() On Error GoTo eh Dim temp As String Dim output As Boolean temp = Worksheets(ADJUSTMENT_WORKSHEET).Range(PCEC_HEADER_CELL).Text output = False If (temp = "PCEC") Then output = True End If checkPCECColumnExist = output Done: Debug.Print "checkPCECColumnExist:done" Exit Function eh: writeRemarks 1, "checkPCECColumnExist:error found" 'MsgBox "Main:Error: " & Err.Description 'Stop End Function Sub helloworld() ' debug.print isCurrentRowNeedToProcess ("7") ' debug.print isCurrentRowNeedToProcess ("180") ' debug.print isAllDone ("7") ' debug.print isAllDone ("180") End Sub Sub ResetExcelTest() Worksheets("Sheet1").Activate Columns("B:Q").Copy Worksheets("Adjustment").Activate Range("B1").Select ActiveSheet.Paste Range("B1").Select Range("R:Z").Delete End Sub