excel 创建一个类似于subtotal的函数,它忽略隐藏的列

dxxyhpgq  于 2023-04-22  发布在  其他
关注(0)|答案(3)|浏览(151)

我试图找到一个替代的小计或聚合,将所有单元格在一个范围内,除了那些列是隐藏的。没有内置的功能,我试图创建自己的,但有很多困难。我已经尝试了以下VBA代码,但无济于事:

Function VISSUM(myRange As range) As Double
On Error Resume Next

Dim i As Variant
Dim mySum As Integer

For Each i In myRange
    If myRange.SpecialCells(xlCellTypeVisible) = True Then
        mySum = mySum + myRange
    End If
Next i

VISSUM = mySum

End Function

这个也没用

Function mySum(myRange As range) As Double
On Error Resume Next

mySum = Application.WorksheetFunction.Sum(myRange.SpecialCells(xlCellTypeVisible) = True)

End Function

我也曾使用myRange.EntireColumn.Hidden = False,但没有成功。任何帮助都将不胜感激。

qcuzuvrc

qcuzuvrc1#

求和列

快速修复

Function VISSUM(ByVal myRange As Range) As Double

    Dim crg As Range, mySum As Double

    For Each crg In myRange.Columns
        If crg.Hidden = False Then
            mySum = mySum + Application.Sum(crg)
        End If
    Next crg

    VISSUM = mySum

End Function
new9mtju

new9mtju2#

你总是可以尝试:

i.entirecolumn.hidden = False

myrange将检查所有隐藏和未隐藏的内容,而我应该是你正在处理的隐藏或未隐藏的单个范围。

snz8szmq

snz8szmq3#

谢谢乔纳森,这起作用了,我不知道我怎么会错过那个明显的错误。这就是我最终写的。

Function VISSUM(myRange As range) As Double
On Error Resume Next

Dim i As Variant
Dim mySum As Double

For Each i In myRange
    If i.EntireColumn.Hidden = False Then
        mySum = mySum + i
    End If
Next i

VISSUM = mySum

End Function

相关问题