Results 1 to 5 of 5

How to set the SubTotal TotalList using a variant instead of an array

Threaded View

  1. #1
    Registered User
    Join Date
    06-03-2013
    Location
    Calgary, Canada
    MS-Off Ver
    Excel 2007/2010/2013
    Posts
    2

    How to set the SubTotal TotalList using a variant instead of an array

    Greetings! I am currently redesigning a workbook that is tracking the daily output of a number of wells. The original tracked 100 wells, but the new version could track anywhere up to 1000 wells. I have been trying to find a method of getting around typing up to 1000 column numbers into the TotalList:=Array (3,4,5,etc.) statement. After a fair bit of research I created the following code (based on examples that seem to have worked for others:

    Sub WklySubtotal()
        Dim varCols()   As Variant  'array to hold column numbers
        Dim intCount    As Integer  'for..next counter
        Dim intMaxCol   As Integer  'number of columns to subtotaled
        
        Sheets("Sheet1").Select
        Cells(1, 3).Select
        Selection.End(xlToRight).Select
        intMaxCol = ActiveCell.Column
        
        ReDim varCols(intMaxCol - 2)
        
        For intCount = 3 To intMaxCol
            varCols(intCount - 3) = intCount
           ' Debug.Print intCount - 3, varCols(intCount - 3)
        Next intCount
    
        Selection.Subtotal Groupby:=1, Function:=xlAverage, TotalList:=varCols, _
        Replace:=True, PageBreaks:=False, SummaryBelowData:=True
    '    Selection.Subtotal Groupby:=1, Function:=xlAverage, TotalList:=Array(3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14), _
    '    Replace:=True, PageBreaks:=False, SummaryBelowData:=True
        
        ActiveSheet.Outline.ShowLevels RowLevels:=2
        Columns("B:B").Select
        Selection.EntireColumn.Hidden = True
        
    End Sub
    The commented out code (where I entered the column numbers in the array) works just fine. The version using the varCols variant generates Run time error 1004 - "Subtotal method of Range class failed"

    The debug statement showed that the proper column numbers are contained in the variant array.

    I am using Excel 2013 on a Windows 10 platform, but I am getting the same results with 2010 and 2016, also on Windows 10.

    I have included a sample of the data.

    Any help is greatly appreciated!
    Attached Images Attached Images

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Does setting a variant acting as an array equal to "" accomplish same as "Erase" variant?
    By dmasters4919 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-21-2014, 05:59 PM
  2. Array Variant
    By jakc in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-07-2014, 11:16 AM
  3. Variant vs Array
    By Kyle123 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-26-2012, 09:45 AM
  4. Resize a variant array
    By Jean-Pierre Bidon in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-19-2006, 09:55 AM
  5. Array problem: Key words-Variant Array, single-element, type mismatch error
    By davidm in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-09-2005, 01:54 AM
  6. Array problem: Key words-Variant Array, single-element, type mismatch error
    By davidm in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-08-2005, 12:30 AM
  7. [SOLVED] ReDim Object array as parameter of Variant array
    By Peter T in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-10-2005, 10:06 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