+ Reply to Thread
Results 1 to 6 of 6

Using Sum function in VBA

  1. #1
    Registered User
    Join Date
    02-25-2016
    Location
    London
    MS-Off Ver
    2010
    Posts
    31

    Using Sum function in VBA

    This should be simple but.........

    I am trying to sum a range of cells using VBA were the range of the cell is variable. The following works using absolute values:

    [HTML][/HSheets("Treasurers Account").Cells(StartIncome + IncomeRowCount + 2, 5) = Application.WorksheetFunction.Sum(Range("E106:E116"))TML]

    How do I replace the 106 and 116 with variables such as startrow and endrow?

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,461

    Re: Using Sum function in VBA

    One way:

    Please Login or Register  to view this content.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    02-25-2016
    Location
    London
    MS-Off Ver
    2010
    Posts
    31

    Re: Using Sum function in VBA

    Thank you. I am sure that syntax was one of the many i tried with no success!

    Can you also tell me why this code does not work? Nothing is cleared.

    HTML Code: 

  4. #4
    Registered User
    Join Date
    02-25-2016
    Location
    London
    MS-Off Ver
    2010
    Posts
    31

    Re: Using Sum function in VBA

    Sorry!

    Dim LastRow As Integer
    LastRow = 100
    ThisWorkbook.Sheets("Treasurers Account").Range("C" & StartIncome & ":C" & LastRow).Clear
    ThisWorkbook.Sheets("Treasurers Account").Range("E" & StartIncome & ":E" & LastRow).Clear

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,461

    Re: Using Sum function in VBA

    I don't know.


    Fast answers need visual help. Please read the yellow banner at the top of this page on how to attach a file and a mocked up solution.

  6. #6
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2402-17328.20068
    Posts
    1,361

    Re: Using Sum function in VBA

    Check the value of StartIncome, you can use 'Debug.Print StartIncome' OR 'Msgbox StartIncome', make sure the value is what you expect it to be. I would guess that it is clearing an area but not the area you expected to clear.

    Make sure to remove any instances of: 'On Error Resume Next' from the code to ensure you see errors when they crop up.
    If things don't change they stay the same

+ 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. Replies: 3
    Last Post: 09-30-2022, 11:04 AM
  2. Right function, Left function, Mid function to extract values in column A
    By bjnockle in forum Excel Formulas & Functions
    Replies: 18
    Last Post: 06-14-2020, 03:14 PM
  3. COUNTUNIQUE Function in Google Sheets; Excel lacks a direct counterpart to this function?
    By PivotTablePSHomage in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-18-2018, 05:27 AM
  4. Calling function inside function. (aka nested function)
    By jakopak in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-02-2015, 05:58 AM
  5. VBA code for custom function that returns detail results of array function
    By onechipshot in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-10-2015, 06:30 PM
  6. Replies: 13
    Last Post: 04-08-2014, 05:46 AM
  7. Replies: 1
    Last Post: 03-21-2012, 11: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