VBA Code to calculate changing cell Value

Denny57

Active Member
Joined
Nov 23, 2015
Messages
275
Office Version
  1. 365
Platform
  1. Windows
I am looking for a line of code that will calculate a new balance each time a new transaction entry is added.

Column H contains the account balance detail and can be either positive or negative

I have a couple of code options prepared in the example but I am struggling to get my head around how the updated balance should be calculated.

There are 4 scenarios
1) Debit Balance + new debit value
2) Debit Balance - new credit value
3) Credit Balance - new debit value
4) Credit Balance + new credit value

Negative balances will be preceded with a minus sign (-)

Any suggestions will be gratefully received.
VBA Code:
Private Sub txtDebitAmount_Exit(ByVal Cancel As MSForms.ReturnBoolean)
txtDebitAmount.Value = Format(txtDebitAmount.Value, "Currency")
End Sub
Private Sub txtCreditAmount_Exit(ByVal Cancel As MSForms.ReturnBoolean)
txtCreditAmount.Value = Format(txtCreditAmount.Value, "Currency")
End Sub

Private Sub cmdAddUpdate_Click()

    Dim answer      As VbMsgBoxResult
    Dim AddRecord   As Boolean
    Dim sCredit As String, sDebit As String
    
    sDebit = Me.txtDebitAmount
    sCredit = Me.txtCreditAmount
    
    AddRecord = Val(Me.cmdAddUpdate.Tag) = xlAdd
    
    answer = MsgBox(IIf(AddRecord, "Add New", "Update Current") & "Record?", 36, "Information")
    
    If answer = vbYes Then
    
    If AddRecord Then CurrentRow = wsSpendingAccount.Range("A" & wsSpendingAccount.Rows.Count).End(xlUp).Row + 1
    
    On Error GoTo Myerror
    
    With wsSpendingAccount
        .Cells(CurrentRow, 1).Value = Cells(CurrentRow - 1, 1).Value + 1
        .Cells(CurrentRow, 2).Value = DTPicker1.Value
        .Cells(CurrentRow, 3).Value = txtVendor.Value
        .Cells(CurrentRow, 4).Value = cboPaymentMethod.Value
        '.Cells(CurrentRow, 5).Value = txtDebitAmount.Value
        '.Cells(CurrentRow, 6).Value = txtCreditAmount.Value
        .Cells(CurrentRow, 7).Value = cboTransactionStatus
        
    ' credit or debit
        If Len(sDebit) > 0 Then
            If Len(sCredit) > 0 Then
                MsgBox "Warning - Both Credit and Debit", vbExclamation
            Else
                .Cells(CurrentRow, 5).Value = CDbl(sDebit)
            End If
        ElseIf Len(sCredit) > 0 Then
           .Cells(CurrentRow, 6).Value = CDbl(sCredit)
        End If
        
        'If CurrentRow > 21 Then
            'Application.Goto Reference:=.Cells(r - 20, "A"), Scroll:=True
        'End If
    End With
        
End Sub

 
I am not sure I completely understand your concerns, but a simple rule would be to always use addition. So:
Code:
old_debit_balance + entry_debit = new_debit_balance 
old_credit_balance + entry_credit = new_credit_balance
The only thing that you have to choose/decide is whether the credit values are negative or positive.
I personally would represent credit values with negative numbers, and then:
Code:
total_balance =  debit_balance + credit_balance

If credit values are also positive, then:
Code:
total_balance =  debit_balance - credit_balance

I hope this helps.
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top