+ Reply to Thread
Results 1 to 6 of 6

Thread: Creating a macro to SUM a varying range of cells

  1. #1
    Registered User
    Join Date
    11-05-2010
    Location
    California, US
    MS-Off Ver
    Excel 2003
    Posts
    5

    Creating a macro to SUM a varying range of cells

    First: Hello all, greetings!

    Second: I am completely worthless when it comes to programming, so it was much simpler to just ask this here. I tried several variations on some automatically generated macros, but none of them worked.

    Now: I need a macro to create a SUM formula based on selected cells, and it needs to appear in a single cell relative to the cells I selected. Here's some screens demonstrating what I need:
    1: Select some cells
    http://img251.imageshack.us/img251/9981/38594808.jpg

    2: SUM them up in that cell there
    http://img249.imageshack.us/img249/6193/85762684.jpg

    3: Done
    http://img20.imageshack.us/img20/7238/19859476.jpg

    Thank you!
    Last edited by Earmite; 11-05-2010 at 03:58 PM. Reason: specified which "things"

  2. #2
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    768

    Re: Creating a macro to SUM a varying range of cells

    It would help to know how you are using the spreadsheet. ie what is the spreadsheet doing.


    Hope this was useful or entertaining.

  3. #3
    Forum Guru mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    2,998

    Re: Creating a macro to SUM a varying range of cells

    Perhaps something like

    Sub aMacro()
        On Error GoTo CancelExit:
        With Application.InputBox("Select the Cells to sum", Type:=8)
             .Offset(.Rows.Count - 1, .Columns.Count).Cells(1, 1).FormulaR1C1 = "=SUM(" & .Address(, , xlR1C1) & ")"
        End With
    CancelExit:
        On Error GoTo 0
    End Sub
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  4. #4
    Extremely Helpful member
    Join Date
    12-23-2006
    Location
    Belgium
    MS-Off Ver
    XL2003
    Posts
    6,127

    Re: Creating a macro to SUM a varying range of cells

    Why post an image when we need a sheet?
    Quoting entire posts clutters the forum and makes threads hard to read !

    If you are pleased with a member's answer then use the Star icon to rate it

    Click here to see forum rules

  5. #5
    Registered User
    Join Date
    11-05-2010
    Location
    California, US
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Creating a macro to SUM a varying range of cells

    Ah sorry. I forgot that that would be the most logical. Here's a sample sheet wtihout all my sensitive information on it:

    EDIT: Mike, I tried your thing, and it worked perfectly. A little editing of it, and I figured out exactly what I needed! Thanks again!
    Attached Files Attached Files
    Last edited by Earmite; 11-05-2010 at 04:39 PM. Reason: testing between edits...

  6. #6
    Registered User
    Join Date
    11-05-2010
    Location
    California, US
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Creating a macro to SUM a varying range of cells

    If anybody cares, here's the final macro I'll be using:
    Sub aMacro2()
        On Error GoTo CancelExit:
        With Selection
             .Offset(.Rows.Count - 1, .Columns.Count).Cells(1, 1).FormulaR1C1 = "=SUM(" & .Address(, , xlR1C1) & ")"
        End With
    CancelExit:
        On Error GoTo 0
    End Sub
    Thanks mikerickson!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0