+ Reply to Thread
Results 1 to 5 of 5

How to Sum in a Macro

  1. #1
    Forum Contributor
    Join Date
    11-12-2014
    Location
    Boston, MA
    MS-Off Ver
    Office 2010
    Posts
    172

    How to Sum in a Macro

    Hi,

    The last thing I want my macro to do is scroll to the last row of my table and SUM Column H. The rows in my table vary every time I run the report, so normally I would use this formula:

    =SUM(H2:OFFSET(H2,COUNTA(A:A)-2,0))

    Column A will always have data, so this will give me the accurate number of rows for the sum range.

    When I write that formula into the end of my Macro, it doesn't work. I look at the formula bar for the cell that should show the sum, and the formula has extra parentheses that I did not write. How can I write the SUM formula into the Macro??? *Note- I can also use the name of the column, which is "Days". I'm just not as comfortable using named ranges as I am with cell references.

    Also, the main part of this macro is to take about 20 tables from other sheets and put them all into this one sheet. So I have tried just using the last column as a sum, but when my Macro pastes the last table it does not make it part of the table, so it sits below my SUM formula.

  2. #2
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: How to Sum in a Macro

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    12-12-2014
    Location
    PL
    MS-Off Ver
    2010
    Posts
    55

    Re: How to Sum in a Macro

    Once you determine in which cell you'd like to have the sum:

    [cell address].Formula = "=SUM(H2:OFFSET(H2,COUNTA(A:A)-2,0))"

    Edit: Bob already gave a complete solution, just letting you know that you can keep your worksheet formula if you prefer.
    Last edited by JazzKult; 01-23-2015 at 10:20 AM.

  4. #4
    Forum Contributor
    Join Date
    11-12-2014
    Location
    Boston, MA
    MS-Off Ver
    Office 2010
    Posts
    172

    Re: How to Sum in a Macro

    Hi Bob,

    Could you explain that formula a bit? I don't really understand it. I'm pretty new to writing Macros. I've just started to record them and look at that code to learn it as I go

  5. #5
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: How to Sum in a Macro

    It would just end up as

    =SUM(H2:Hn)

    where n is the last row of data in column A.

+ 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. Using a macro on workbook1 to create a button in wb2 and assigning macro "wb2!macro"
    By penfold1992 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-13-2014, 11:39 AM
  2. [SOLVED] Macro to show Which macro didnt work in a nested macro
    By akhileshgs in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 06-10-2013, 03:21 AM
  3. Perform macro "on open" specific file- store macro in Personal Macro Workbook?
    By thompssc in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-17-2012, 12:38 PM
  4. lookup macro, solver macro, realtime macro
    By xelhelp in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-02-2011, 06:14 PM
  5. Cannot find macro error when running a macro from a macro in a diffrent workbook.
    By Acrobatic82 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-05-2010, 09:22 AM

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