Consulting

Results 1 to 4 of 4

Thread: How to count, sum and subtotal values in a filtered listbox?

  1. #1
    VBAX Contributor
    Joined
    Jul 2018
    Posts
    174
    Location

    How to count, sum and subtotal values in a filtered listbox?

    I have a userform where I filter things in a listbox that is populated using an array.

    I am trying to COUNT values in the array, SUM the values in the array or use SUBTOTAL function.

    Myarray = Sheet61.ListObjects("Data").DataBodyRange.Value
    The array has 21 columns. And I want to use SUBTOTAL(109, range)on a specific column?

    How can I use subtotal formula on the array/listbox/listbox.list?

    With ListBox1
                .List = Myarray
    End With
    I think that I should use

    Label49.Caption = Application.WorksheetFunction.Subtotal(109, Listbox1.List(i,5))
    Where I am not sure on how to get the range in the filterad listbox?
    Last edited by waimea; 10-08-2019 at 08:25 AM.

  2. #2
    VBAX Contributor
    Joined
    Jul 2018
    Posts
    174
    Location
    Dim j As Long
          
        For j = ListBox1.ListCount - 1 To 0 Step -1
        Me.TextBox5.Text = Application.WorksheetFunction.Subtotal(109, ListBox1.List(j, 8))
        Next
    I am not sure why this doesn't work?

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Not tested

        Dim j As Long, N as long
          
        For j = ListBox1.ListCount - 1 To 0 Step -1
            n = n + ListBox1.List(j,8)
        Next j
    
        Me.TextBox5.Text = N
    Or work with the ListObject maybe


    N = Application.WorksheetFunction.Sum(Sheet61.ListObjects("Data").DataBodyRange.Columns(8))
    (also not tested)
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    VBAX Contributor
    Joined
    Jul 2018
    Posts
    174
    Location
    Hi again Paul,

    the first one works great! Thank you.


    To get the count use:

    Me.TextBox6 = Listbox1.ListCount

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •