Search code examples
vbaexcelfinance

How to: Retrieve last cell in a column on one sheet, and paste on another sheet


I'm using VBA for an accounting project and I'm pretty new to it.

Short Version: User enters a Name (name) and $Transaction Amount (tran) into excel Cells A12,B12

I have a Table on A28:D32 that contains names and Account Values.The names are in Column A and the account values are in D.

I want add the transaction amount to the correct row in Column D based on name of the depositor.

E.G 
Transaction: 
name: Charles 
Transaction Amount: $500

Before
Charles  $2154
John     $3150
Chris    $8450

After
Charles  $2654
John     $3150
Chris    $8450

Below is the mess, I've come up with that does not work.

Please help Thanks!!

Sub Step3UpdateEquity()

    Dim name As String
    Dim tran As Integer
    Dim pretranIvalue As Integer
    Dim PosttranIvalue As Integer

    name = Range("A12").Value
    tran = Range("B12").Value

    PosttranIvalue = pretranIvalue + tran

    Set Depositcell = Range("A28:D32").Find(pretranIvalue)
    Range("Depositcell.adress").Value = PosttranIvalue

End Sub

Solution

  • Try this:

    Sub Step3UpdateEquity()
    
        Dim name As String
        Dim tran As Integer
        Dim DepositCell As Range
    
        name = Range("A12").Value
        tran = Range("B12").Value
    
        Set DepositCell = Range("A28:D32").Find(name).Offset(0, 1)'Asuming that the Transaction
                                                                  'amount is located in the cell
                                                                  'on the right of the name cell
        DepositCell.Value = DepositCell.Value + tran
    
    End Sub
    

    I assumed, that the value of each name is stored next to the name (on the right), if this is not the case, you would need to change the Offset Value acordingly