Forum Discussion

Mix Cyber's avatar
Mix Cyber
Copper Contributor
Feb 15, 2018

Excel formulas

Hi, Is  there a way to enter a number in a cell - "income" then hit "enter" button and it disappears summing all entered numbers in next cell "quantity" ?

  • If you want to enter a number in any "Income" cell, increment the adjacent "Quantity" cell and then clear the "Income" cell then you will need to use a macro (aka VBA code).  Damien_Rosario was on the right track but you don't need the Developer tab to get to the VBA IDE (Integrated Developer Environment) - if you right click the sheet tab it gives you an option to:

     

     

    Sheet Tab context drop-down

     Which clicking View Code will take you to a window that looks like this:

     

    Initial VBA IDE Worksheet pane Yours may not have 

    Option Explicit

    If not, add it!  It catches any typos in Variable names which could make your code useless.

     

    Copy this code into the IDE Worksheet pane and read below for some explanation if interested.

    Private Sub Worksheet_Change(ByVal Target As Range)
        'Test if the changed (Target) cell is within your QUANTITY column
        'If it isn't then the Intersection will be Nothing and you
        'don't want to do anything to the sheet.
        'If it is, then the Intersection will Not be Nothing so do things to sheet
        If Not Intersect(Target, [Table1[QUANTITY]]) Is Nothing Then
            'Add Target value to the value of the cell to its right
            Target.Offset(0, 1) = Target.Offset(0, 1) + Target
            'and clear the Target cell
            Target.ClearContents
        End If
    End Sub

    The lines starting with apostrophes are just comments which should show up in green in the code pane.  They don't do anything and can be deleted if you want.

      

    PS: Delete the merged row 2 if possible.  Tables aren't designed to have merged cells - think of them more as list with fields (columns)  that have names (headers) in the top row. 

    • Damien_Rosario's avatar
      Damien_Rosario
      Silver Contributor
      Awesome answer, Mark. Thanks for adding to the conversation! Hopefully it's what Cyber Mix is after!
  • I don't understand! Where do you want the entered number to "Disappear" to? You could make it "disappear" in many ways - conditional format to set font color the same as background color;; use VBA to Undo your entry; use VBA to move it somewhere else; etc.
    • Mix Cyber's avatar
      Mix Cyber
      Copper Contributor

      Disappear to quantity. It's like I have 1 cookie in my hand (income) then I put it in a jar (hit enter) with 5 cookies, my hand became empty and the jar have +1 cookie. Overall quantity in a jar bacame 6.

      • Damien_Rosario's avatar
        Damien_Rosario
        Silver Contributor
        You may need a macro, something like:

        Private Sub Worksheet_Change (ByVal Target As Range)
        Range("B2").Value = Range("B2").value + Range("A2").value
        End Sub

        This will create a running total whether you press Enter or click away from cell A2.

        Cheers
        Damien

Resources