' change log:

Option explicit

Dim first_row As Integer
Dim current_row As Integer
Dim last_check_row As Integer

Dim ADJUSTMENT_WORKSHEET as string
Dim BMF_ADJUSTMENT_WORKSHEET as string

' BALANCE_SHEET
Dim COL_BALANCE_SHEET_NA_1 as string
Dim COL_BALANCE_SHEET_PCCO_1 as string
Dim COL_BALANCE_SHEET_AMOUNT_TO_ADJ_1 as string

Dim COL_BALANCE_SHEET_NA_2 as string
Dim COL_BALANCE_SHEET_PCCO_2 as string
Dim COL_BALANCE_SHEET_AMOUNT_TO_ADJ_2 as string

' OFF_BALANCE_SHEET
Dim COL_OFF_BALANCE_SHEET_NA_1 as string
Dim COL_OFF_BALANCE_SHEET_PCCO_1 as string
Dim COL_OFF_BALANCE_SHEET_AMOUNT_TO_ADJ_1 as string

Dim COL_OFF_BALANCE_SHEET_NA_2 as string
Dim COL_OFF_BALANCE_SHEET_PCCO_2 as string
Dim COL_OFF_BALANCE_SHEET_AMOUNT_TO_ADJ_2 as string

Dim COL_FINAL_BMF_ID as string

Dim COL_BMF_ADJUSTMENT_SHEET_BMF_REFERENCE_ID as string
Dim COL_BMF_ADJUSTMENT_SHEET_LINE_TYPE as string
Dim COL_BMF_ADJUSTMENT_SHEET_ADJUSTMENT_ID as string
Dim COL_BMF_ADJUSTMENT_SHEET_ADJUSTMENT_TYPE as string
Dim COL_BMF_ADJUSTMENT_SHEET_COMMENT as string
Dim COL_BMF_ADJUSTMENT_SHEET_REPORTING_ENTITY as string
Dim COL_BMF_ADJUSTMENT_SHEET_PCCO as string
Dim COL_BMF_ADJUSTMENT_SHEET_PCEC as string
Dim COL_BMF_ADJUSTMENT_SHEET_ACCOUNTING_BALANCE_CURRENCY as string
Dim COL_BMF_ADJUSTMENT_SHEET_AMOUNT_IN_ACCT_BALANCE as string
Dim COL_BMF_ADJUSTMENT_SHEET_AMOUNT_IN_FUNT as string
Dim COL_BMF_ADJUSTMENT_SHEET_FAIR_VALUE as string
Dim COL_BMF_ADJUSTMENT_SHEET_ECONOMIC_AGENT as string
Dim COL_BMF_ADJUSTMENT_SHEET_OPAQUE_REPORTING_ENTITY as string
Dim COL_BMF_ADJUSTMENT_SHEET_REMAINING_MATURITY as string
Dim COL_BMF_ADJUSTMENT_SHEET_IFRS13 as string
Dim COL_BMF_ADJUSTMENT_SHEET_COUNTRY as string
Dim COL_BMF_ADJUSTMENT_SHEET_LOCAL_OPERATIONAL_ACCOUNT as string


Sub init()
  On Error GoTo eh

  ' HEADER_ROW = 6
  first_row = 6
  current_row = first_row
  last_check_row = 9999

  ' ADJUSTMENT_SHEET
  ADJUSTMENT_WORKSHEET = "Adjustment"
  
  ' BALANCE_SHEET
  COL_BALANCE_SHEET_NA_1 = "Q"
  COL_BALANCE_SHEET_PCCO_1 = "R"
  COL_BALANCE_SHEET_AMOUNT_TO_ADJ_1 = "S"

  COL_BALANCE_SHEET_NA_2 = "T"
  COL_BALANCE_SHEET_PCCO_2 = "U"
  COL_BALANCE_SHEET_AMOUNT_TO_ADJ_2 = "V"

  ' OFF_BALANCE_SHEET
  COL_OFF_BALANCE_SHEET_NA_1 = "X"
  COL_OFF_BALANCE_SHEET_PCCO_1 = "Y"
  COL_OFF_BALANCE_SHEET_AMOUNT_TO_ADJ_1 = "Z"

  COL_OFF_BALANCE_SHEET_NA_2 = "AA"
  COL_OFF_BALANCE_SHEET_PCCO_2 = "AB"
  COL_OFF_BALANCE_SHEET_AMOUNT_TO_ADJ_2 = "AC"

  COL_FINAL_BMF_ID = "AI"

  ' BMF_ADJUSTMENT_SHEET
  BMF_ADJUSTMENT_WORKSHEET = "BMF Adjustment result"
  COL_BMF_ADJUSTMENT_SHEET_BMF_REFERENCE_ID = "A"
  COL_BMF_ADJUSTMENT_SHEET_LINE_TYPE = "B"
  COL_BMF_ADJUSTMENT_SHEET_ADJUSTMENT_ID = "C"
  COL_BMF_ADJUSTMENT_SHEET_ADJUSTMENT_TYPE = "D"
  COL_BMF_ADJUSTMENT_SHEET_COMMENT = "E"
  COL_BMF_ADJUSTMENT_SHEET_REPORTING_ENTITY = "F"
  COL_BMF_ADJUSTMENT_SHEET_PCCO = "G"
  COL_BMF_ADJUSTMENT_SHEET_PCEC = "H"
  COL_BMF_ADJUSTMENT_SHEET_ACCOUNTING_BALANCE_CURRENCY = "I"
  COL_BMF_ADJUSTMENT_SHEET_AMOUNT_IN_ACCT_BALANCE = "J"
  COL_BMF_ADJUSTMENT_SHEET_AMOUNT_IN_FUNT = "K"
  COL_BMF_ADJUSTMENT_SHEET_FAIR_VALUE = "BH"
  COL_BMF_ADJUSTMENT_SHEET_ECONOMIC_AGENT = "BI"
  COL_BMF_ADJUSTMENT_SHEET_OPAQUE_REPORTING_ENTITY = "BJ"
  COL_BMF_ADJUSTMENT_SHEET_REMAINING_MATURITY = "BK"
  COL_BMF_ADJUSTMENT_SHEET_IFRS13 = "BL"
  COL_BMF_ADJUSTMENT_SHEET_COUNTRY = "BM"
  COL_BMF_ADJUSTMENT_SHEET_LOCAL_OPERATIONAL_ACCOUNT = "BN"

Done:
  Exit Sub

eh:
  Debug.Print "init:Error: " & Err.Description


End Sub


Function CheckIfBalanceSheetAmountToAdjust2IsZero(current_row As Integer)
  On Error GoTo eh
  Dim output as boolean
  output = false
  Dim temp as string 
  temp = Worksheets(ADJUSTMENT_WORKSHEET).Range(COL_BALANCE_SHEET_AMOUNT_TO_ADJ_2 & CStr(current_row)).Value 

  if (temp = "0") then
    output = true
  end if

  CheckIfBalanceSheetAmountToAdjust2IsZero = output

Done:
  Exit Function

eh:
  Debug.Print "CheckIfBalanceSheetAmountToAdjust2IsZero:Error: " & Err.Description

End Function



Function CheckIfBalanceSheetNa2Exist(current_row As Integer)
  On Error GoTo eh
  Dim output as boolean
  output = false
  Dim temp as string 
  temp = Worksheets(ADJUSTMENT_WORKSHEET).Range(COL_BALANCE_SHEET_NA_2 & CStr(current_row)).Value 

  if (temp <> "") then
    output = true
  end if

  CheckIfBalanceSheetNa2Exist = output

Done:
  Exit Function

eh:
  Debug.Print "CheckIfBalanceSheetNa2Exist:Error: " & Err.Description

End Function

Function CheckIfBalanceSheetPcco2Exist(current_row As Integer)
  On Error GoTo eh

  Dim output as boolean
  output = false
  Dim temp as string 
  temp = Worksheets(ADJUSTMENT_WORKSHEET).Range(COL_BALANCE_SHEET_PCCO_2 & CStr(current_row)).Value 

  if (temp <> "") then
    output = true
  end if

  CheckIfBalanceSheetPcco2Exist = output

Done:
  Exit Function

eh:
  Debug.Print "CheckIfBalanceSheetPcco2Exist:Error: " & Err.Description

End Function

Function CheckIfBalanceSheetAmountToAdjust2Exist(current_row As Integer)
  On Error GoTo eh

  Dim output as boolean
  output = false
  Dim temp as string 
  temp = Worksheets(ADJUSTMENT_WORKSHEET).Range(COL_BALANCE_SHEET_AMOUNT_TO_ADJ_2 & CStr(current_row)).Value 

  if (temp <> "") then
    output = true
  end if

  CheckIfBalanceSheetAmountToAdjust2Exist = output

Done:
  Exit Function

eh:
  Debug.Print "CheckIfBalanceSheetAmountToAdjust2Exist: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

Function getLastCheckRow(first_row As Integer)
  On Error GoTo eh
  getLastCheckRow = first_row + 10

Done:
  Exit Function

eh:
  Debug.Print "getLastCheckRow:Error: " & Err.Description

End Function

function checkAdjustmentSheetLastRow(current_row as integer)
  On Error GoTo eh
  Dim output as boolean
  output = True

  dim j as integer
  Dim temp as string

  For j = current_row to getLastCheckRow(current_row)
    temp = Worksheets(ADJUSTMENT_WORKSHEET).Range(COL_FINAL_BMF_ID & CStr(current_row)).Value
    if (temp <> "") then
      output = False
    end if
  next j
  
  checkAdjustmentSheetLastRow = output

Done:
  Exit Function

eh:
  Debug.Print "checkAdjustmentSheetLastRow:Error: " & Err.Description

end function 

function checkBmfAdjustmentSheetLastRow(current_row as integer)
  On Error GoTo eh
  Dim output as boolean
  output = True

  dim j as integer
  Dim temp as string

  For j = current_row to getLastCheckRow(current_row)
    temp = Worksheets(BMF_ADJUSTMENT_WORKSHEET).Range(COL_BMF_ADJUSTMENT_SHEET_BMF_REFERENCE_ID & CStr(current_row)).Value
    if (temp <> "") then
      output = False
    end if
  next j
  
  checkBmfAdjustmentSheetLastRow = output

Done:
  Exit Function

eh:
  Debug.Print "checkBmfAdjustmentSheetLastRow:Error: " & Err.Description

end function 

Function checkIfAlreadyExistInBmfAdjustmentResult(adj_bmf_id as string, adj_pcco as string)
  On Error GoTo eh

  Dim i as integer
  Dim bmf_adjustment_sheet_last_row as integer
  Dim found as boolean
  found = false
  Dim temp_value as string
  Dim bmf_adj_bmf_id as string
  Dim bmf_adj_pcco as string

  bmf_adjustment_sheet_last_row = getBMFAdjustmentResultListEnd(1)

  For i = 1 to bmf_adjustment_sheet_last_row
    bmf_adj_bmf_id = Worksheets(BMF_ADJUSTMENT_WORKSHEET).Range(COL_BMF_ADJUSTMENT_SHEET_BMF_REFERENCE_ID & CStr(i)).Value
    bmf_adj_pcco = Worksheets(BMF_ADJUSTMENT_WORKSHEET).Range(COL_BMF_ADJUSTMENT_SHEET_PCCO & CStr(i)).Value
    debug.print adj_bmf_id
    debug.print adj_pcco
    if (bmf_adj_bmf_id = adj_bmf_id and bmf_adj_pcco = adj_pcco ) then
      found = true
      Worksheets(BMF_ADJUSTMENT_WORKSHEET).Range(COL_BMF_ADJUSTMENT_SHEET_BMF_REFERENCE_ID & CStr(i)).Interior.Color = rgb(214, 48, 49)
      Worksheets(BMF_ADJUSTMENT_WORKSHEET).Range(COL_BMF_ADJUSTMENT_SHEET_PCCO & CStr(i)).Interior.Color = rgb(214, 48, 49)
      exit for
    end if
  next i

  checkIfAlreadyExistInBmfAdjustmentResult = found

Done:
  Exit Function

eh:
  Debug.Print "checkIfAlreadyExistInBmfAdjustmentResult:Error: " & Err.Description

End Function

Function getBMFAdjustmentResultListEnd(start_row as integer)
  On Error GoTo eh

  Dim i as integer

  Dim last_row as boolean
  Dim last_row_found as boolean
  last_row_found = false
  last_row = True
  current_row = start_row

  For i = start_row To 9999
    current_row = i
    last_row_found = checkBmfAdjustmentSheetLastRow(current_row)

    if (last_row_found = true) then
      exit for
    end if
  Next i

  getBMFAdjustmentResultListEnd = current_row -1

Done:
  Exit Function

eh:
  Debug.Print "getBMFAdjustmentResultListEnd:Error: " & Err.Description

End Function

Function getAdjustmentSheetListEnd(start_row as integer)
  On Error GoTo eh

  Dim i as integer

  Dim last_row as boolean
  Dim last_row_found as boolean
  last_row_found = false
  last_row = True
  current_row = start_row

  For i = start_row To 9999
    current_row = i
    last_row_found = checkAdjustmentSheetLastRow(current_row)

    if (last_row_found = true) then
      exit for
    end if
  Next i

  getAdjustmentSheetListEnd = current_row -1

Done:
  Exit Function

eh:
  Debug.Print "getAdjustmentSheetListEnd:Error: " & Err.Description

End Function


Sub helloworld()
  On Error GoTo eh
  Dim need_to_copy as boolean

  Dim adjustment_sheet_last_row as integer
  Dim adjustment_sheet_current_row as integer

  Dim bmf_adjustment_sheet_last_row as integer
  Dim bmf_adjustment_sheet_current_row as integer

  init
  'MsgBox "helloworld"
  adjustment_sheet_last_row= getAdjustmentSheetListEnd(first_row)

  For adjustment_sheet_current_row = 6 To adjustment_sheet_last_row
    need_to_copy = false

    Dim bmf_id as string
    Dim pcco as string
    bmf_id = Worksheets(ADJUSTMENT_WORKSHEET).Range(COL_FINAL_BMF_ID & CStr(adjustment_sheet_current_row)).Value
    pcco=Worksheets(ADJUSTMENT_WORKSHEET).Range(COL_BALANCE_SHEET_PCCO_2 & CStr(adjustment_sheet_current_row)).Value

    if (CheckIfBalanceSheetAmountToAdjust2Exist(adjustment_sheet_current_row) and CheckIfBalanceSheetNa2Exist(adjustment_sheet_current_row) and CheckIfBalanceSheetPcco2Exist(adjustment_sheet_current_row)) then
      if (CheckIfBalanceSheetAmountToAdjust2IsZero(adjustment_sheet_current_row)) then
        debug.print Cstr(adjustment_sheet_current_row) & " no need to copy as zero"

      elseif (checkIfAlreadyExistInBmfAdjustmentResult(bmf_id, pcco)) then
        debug.print "already exist in bmf table, skipping"
      else
        need_to_copy = True
      end if
    end if

    ' helloworld
    if (need_to_copy = true) then
      debug.print adjustment_sheet_current_row & " need to copy"
      bmf_adjustment_sheet_last_row = getBMFAdjustmentResultListEnd(1)
      bmf_adjustment_sheet_current_row = bmf_adjustment_sheet_last_row + 1

      Dim temp_value as string


      Worksheets(BMF_ADJUSTMENT_WORKSHEET).Range(COL_BMF_ADJUSTMENT_SHEET_REPORTING_ENTITY & CStr(bmf_adjustment_sheet_current_row)).Value = "14004"
      Worksheets(BMF_ADJUSTMENT_WORKSHEET).Range(COL_BMF_ADJUSTMENT_SHEET_OPAQUE_REPORTING_ENTITY & CStr(bmf_adjustment_sheet_current_row)).Value = "N"

      temp_value = Worksheets(ADJUSTMENT_WORKSHEET).Range(COL_BALANCE_SHEET_NA_2 & CStr(adjustment_sheet_current_row)).Value
      Worksheets(BMF_ADJUSTMENT_WORKSHEET).Range(COL_BMF_ADJUSTMENT_SHEET_LOCAL_OPERATIONAL_ACCOUNT & CStr(bmf_adjustment_sheet_current_row)).Value = temp_value & " 15130"

      temp_value = Worksheets(ADJUSTMENT_WORKSHEET).Range(COL_BALANCE_SHEET_PCCO_2 & CStr(adjustment_sheet_current_row)).Value
      Worksheets(BMF_ADJUSTMENT_WORKSHEET).Range(COL_BMF_ADJUSTMENT_SHEET_PCCO & CStr(bmf_adjustment_sheet_current_row)).Value = temp_value 

      temp_value = Worksheets(ADJUSTMENT_WORKSHEET).Range(COL_BALANCE_SHEET_AMOUNT_TO_ADJ_2 & CStr(adjustment_sheet_current_row)).Value
      Worksheets(BMF_ADJUSTMENT_WORKSHEET).Range(COL_BMF_ADJUSTMENT_SHEET_AMOUNT_IN_ACCT_BALANCE & CStr(bmf_adjustment_sheet_current_row)).Value = temp_value 
      Worksheets(BMF_ADJUSTMENT_WORKSHEET).Range(COL_BMF_ADJUSTMENT_SHEET_AMOUNT_IN_FUNT & CStr(bmf_adjustment_sheet_current_row)).Value = temp_value 

      temp_value = Worksheets(ADJUSTMENT_WORKSHEET).Range(COL_FINAL_BMF_ID & CStr(adjustment_sheet_current_row)).Value
      Worksheets(BMF_ADJUSTMENT_WORKSHEET).Range(COL_BMF_ADJUSTMENT_SHEET_BMF_REFERENCE_ID & CStr(bmf_adjustment_sheet_current_row)).Value = temp_value 

      ' update formatting
      Worksheets(BMF_ADJUSTMENT_WORKSHEET).Range(COL_BMF_ADJUSTMENT_SHEET_AMOUNT_IN_ACCT_BALANCE & CStr(bmf_adjustment_sheet_current_row)).NumberFormat = "#,##_);[Red](#,##)"
      Worksheets(BMF_ADJUSTMENT_WORKSHEET).Range(COL_BMF_ADJUSTMENT_SHEET_AMOUNT_IN_FUNT & CStr(bmf_adjustment_sheet_current_row)).NumberFormat = "#,##_);[Red](#,##)"


    else
      debug.print adjustment_sheet_current_row & " no need to copy"
    end if

    
  Next adjustment_sheet_current_row

Done:
  Exit Sub

eh:
  Debug.Print "helloworld:Error: " & Err.Description

End Sub
