+ Reply to Thread
Results 1 to 6 of 6

Table column sum generates error

Hybrid View

  1. #1
    Registered User
    Join Date
    04-04-2018
    Location
    Glasgow, Scotland
    MS-Off Ver
    MS 365
    Posts
    34

    Table column sum generates error

    I have a worksheet containing a table. The following function returns the total of any column.

    Function ColSum(col As Integer)
    
        Dim lo As ListObject
        Dim TotalSP As Integer
        
        
        Application.Volatile
        
        Sheets("ContractAuth").Activate
        Set lo = ActiveSheet.ListObjects(1)
        ColSum = Application.WorksheetFunction.Sum(lo.DataBodyRange.Columns(col))
        
    End Function
    I find that often the cell holding the result of this function has the error '#VALUE' and I have to press F9 to force calculation, although automatic calculation is already on. This appears to happen when a change is made to another worksheet in the same workbook - I have to return to the worksheet containing the table and press F9.
    Last edited by DougieJLockhart; 05-02-2018 at 05:29 AM. Reason: More information

  2. #2
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,264

    Re: Table column sum generates error

    Remove maybe 'Application.Volatile'.

  3. #3
    Registered User
    Join Date
    04-04-2018
    Location
    Glasgow, Scotland
    MS-Off Ver
    MS 365
    Posts
    34

    Re: Table column sum generates error

    Well, that stops the error being thrown but it also means that the sum is never updated when the spreadsheet changes.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Table column sum generates error

    Perhaps you could handle the error with something like this.
    Copy to clipboard
    Function ColSum(col As Integer) As Variant
    Dim lo As ListObject
    Dim TotalSP As Variant
        
       Application.Volatile
        
        Set lo = Sheets("ContractAuth").ListObjects(1)
        TotalSP = Application.Sum(lo.DataBodyRange.Columns(col))
        
        If Not IsError(TotalSP) Then
            ColSum = TotalSP
        Else
            ColSum = "#N/A"
        End If
    
    End Function
    If posting code please use code tags, see here.

  5. #5
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Table column sum generates error

    Hi,
    It would be much better design to pass the table to the function as well. Then you would not need to make the function volatile or to hardcode sheet names.
    Function ColSum(tbl As Range, col As Integer)
    
        ColSum = Application.WorksheetFunction.Sum(tbl.Columns(col))
        
    End Function
    and call it using =ColSum(tablename, 1) for example.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  6. #6
    Registered User
    Join Date
    04-04-2018
    Location
    Glasgow, Scotland
    MS-Off Ver
    MS 365
    Posts
    34

    Re: Table column sum generates error

    Many thanks - that does the trick.

    Also greatly reduces recalc time.
    Last edited by DougieJLockhart; 05-03-2018 at 05:41 AM.

+ 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. [SOLVED] Refresh pivot table generates run time error
    By Mskrn in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-28-2016, 11:59 PM
  2. set variable to 0 if a function generates an error
    By luv2glyd in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 02-03-2016, 08:43 PM
  3. how to exit a sub if it generates an error
    By luv2glyd in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-04-2015, 03:34 PM
  4. [SOLVED] Code generates 400 error
    By BRISBANEBOB in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-06-2014, 02:33 AM
  5. VBA generates error 13
    By jmafabi in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-31-2012, 11:04 AM
  6. Macro generates error '5'
    By Rick_Stanich in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-18-2009, 04:17 PM
  7. Working application - Now generates error
    By Madiya in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-24-2005, 07:38 AM

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