+ Reply to Thread
Results 1 to 4 of 4

Code for cell to left + cell above

Hybrid View

  1. #1
    Registered User
    Join Date
    02-23-2016
    Location
    United States
    MS-Off Ver
    2010
    Posts
    1

    Code for cell to left + cell above

    Hello!

    I'm new to the "advanced" coding in excel, so I'm hoping you all may be able to help:


    I am wondering if there is a code to enter so as to get the input of the cell to the left, plus the cell above, but without having to specifically enter each cell's title (so I can easily copy and past the code into various cells a lot faster than manually adding or entering the specific titles. )

    Example for Cell A5: Code input to get Cell A5=B5+A4.

  2. #2
    Valued Forum Contributor
    Join Date
    12-02-2012
    Location
    Melbourne, VIC
    MS-Off Ver
    Excel 2016
    Posts
    750

    Re: Code for cell to left + cell above

    your request:
    a code to enter so as to get the input of the cell to the left, plus the cell above
    and your example:
    Cell A5=B5+A4
    are not the same. Your example is adding the cell to the right and the cell above.

    as per your request, try this:
    Sub Add()
    If ActiveCell.Column = 1 Then Exit Sub                 'there is no cell to the left
    If ActiveCell.Row = 1 Then Exit Sub                      'there is no cell above
    ActiveCell.Value = ActiveCell.Offset(-1, 0) + ActiveCell.Offset(0, -1)
    End Sub
    HTH!

  3. #3
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Code for cell to left + cell above

    Hi there,

    This situation is just crying out for a Defined Name Formula

    I'll assume that you want to implement the formula:

    A5: Code input to get Cell A5=B5+A4

    Proceed as follows:

    Select Cell A5

    Select Formulas >> Define Name from the Ribbon

    In the New Name DialogBox:

    enter frmRunningTotal in the Name box
    select the name of the current worksheet from the dropdown list in the Scope combobox
    enter =B5 + A4 in the Refers to box (note that these references are RELATIVE, and should NOT be entered as e.g. $B$5)
    click on OK
    click on Close

    Now enter "=frmRunningTotal" (without quotes) in Cell A5, and the sum of Cells B5 and A4 will be displayed.

    What's so special about the above approach? It's that now you can enter =frmRunningTotal in ANY cell on the worksheet, and the result displayed will be the sum of the cell above and the cell on the right of the cell in which the formula has been entered.


    Hope this helps - please let me know how you get on.

    Regards,

    Greg M

  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,438

    Re: Code for cell to left + cell above

    Before getting into more complicated solutions like these, I might ask what about A5 =B5+A4 does not work? These are relative references, so when the formula is copied, the references should always be cell above + cell to the right. For example, if this formula is copied to C8, it should become =D8+C7 automatically.

    This explains how to use relative and absolute referencing: https://support.office.com/en-us/art...es_in_formulas
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Find cell with text, offset to the left one cell and write code
    By timas in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 07-23-2015, 01:22 AM
  2. Insert left most cell in function code
    By sachinahj in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 07-26-2012, 08:36 PM
  3. Code to insert row at top left cell
    By qussai in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-14-2012, 03:15 AM
  4. Color code cell with case statement and datestamp cell(s) to the left
    By garricko in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-26-2010, 08:37 PM
  5. macro code for adding a cell in left of original cell?
    By gkeith in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-29-2010, 03:30 PM
  6. Code to pull the value of a Cell of the next Worksheet to the left
    By CRayF in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-27-2005, 05:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1