VBA function subtotal that works for hidden columns

CDienst

New Member
Joined
Jul 11, 2018
Messages
31
Hello,

Subtotal apparently only works for filtered data or manually hidden rows. I'd like to make a function in VBA that is basically just subtotal, but work for whatever might be hidden, rather than just rows.

This seems to work as a test
Code:
Sub subtest()
   
    Dim cl As Range
    Set cl = Range(Range("h15"), Range("k15")).SpecialCells(xlCellTypeVisible)
    
    MsgBox WorksheetFunction.Subtotal(4, cl)
  
End Sub
In this case, H15:K15 is a list of dates, and I want it to output the most recent visible date

The problem arises when I try to put this in the form of a function.
Code:
Function SubCol(x As Integer, y As Range)
    
    Dim cl As Range


    Set cl = Range(Range(y), Range(y).End(xlToRight)).SpecialCells(xlCellTypeVisible)
      SubCol = WorksheetFunction.Subtotal(x, cl)


End Function
I'm sure I must have my formatting wrong to reference the parameter y.

I realize that even with the formatting correct, this might not function entirely as subtotal would since it only works for an input of a single range besides the mode of the subtotal, but for my current purposes it should work fine.

Does anyone know what I need to change in the formatting here and/or a better way to mimic subtotal functionality with the addition of ignoring values in hidden columns?

Thanks
 
Last edited:

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Hello,

Subtotal apparently only works for filtered data or manually hidden rows. I'd like to make a function in VBA that is basically just subtotal, but work for whatever might be hidden, rather than just rows.

This seems to work as a test
Code:
Sub subtest()
   
    Dim cl As Range
    Set cl = Range(Range("h15"), Range("k15")).SpecialCells(xlCellTypeVisible)
    
    MsgBox WorksheetFunction.Subtotal(4, cl)
  
End Sub
In this case, H15:K15 is a list of dates, and I want it to output the most recent visible date

The problem arises when I try to put this in the form of a function.
Code:
Function SubCol(x As Integer, y As Range)
    
    Dim cl As Range


    Set cl = Range(Range(y), Range(y).End(xlToRight))[COLOR=#ff0000][B].SpecialCells(xlCellTypeVisible)[/B][/COLOR]
      SubCol = WorksheetFunction.Subtotal(x, cl)


End Function
I'm sure I must have my formatting wrong to reference the parameter y.

I realize that even with the formatting correct, this might not function entirely as subtotal would since it only works for an input of a single range besides the mode of the subtotal, but for my current purposes it should work fine.

Does anyone know what I need to change in the formatting here and/or a better way to mimic subtotal functionality with the addition of ignoring values in hidden columns?

Thanks
I haven't looked closely enough at your function to know if there are any other issues, but at a minimum, SpecialCells cannot be used in a UDF in any version of Excel I'm aware of.
 
Upvote 0
Oh, well I hadn't considered something like that. I did a bit of googling and modified something from a similar situation.

Code:
Function SubCol(x As Integer, y As Range)
    SubCol = WorksheetFunction.Subtotal(x, VisibleCells(y))
End Function


Private Function VisibleCells(rng As Range) As Range
    Dim r As Range
    For Each r In rng
        If r.Hidden = False Then
            If VisibleCells Is Nothing Then
                Set VisibleCells = r
            Else
                Set VisibleCells = Union(VisibleCells, r)
            End If
        End If
    Next r
End Function

There aren't any errors, but this outputs #VALUE !
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,194
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top