可见细胞总数 [英] Sum visible cells

查看:49
本文介绍了可见细胞总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应用高级过滤器后,我使用下面的代码仅对可见单元进行求和,并且功能良好.但是,当过滤器后面没有可见的单元格时,它将显示错误.对如何解决错误有任何想法吗?

After applied the advance filter, I sum only the visible cells by using the code below and it functions well. However, it shows error when there is no visible cell after the filter. Any idea on how to cover the error?

Sub sum ()
    Sheets("Sheet1").Select
    x = cells(Rows.Count, 8).End(xlUp).Row

    Sheets("Tax Invoice").Range("M55") = WorksheetFunction.Sum(Range("H7:H" & x).SpecialCells(xlCellTypeVisible))
End sub

推荐答案

在尝试计算总和之前计算可见行数:

Count the number of visible rows before attempting to calculate the sum:

Option Explicit

Public Sub sumTaxInvRng()
    Dim x As Long, vRng As Range

    With Sheets("Sheet1")
        x = .Cells(.Rows.Count, 8).End(xlUp).Row
        Set vRng = .Range("H7:H" & x).SpecialCells(xlCellTypeVisible)
    End With

    If vRng.Count > vRng.Columns.Count Then
        Sheets("Tax Invoice").Range("M55") = WorksheetFunction.Sum(vRng)
    End If
End Sub


此外,您不应为函数使用本机函数名称: Sub sum()


Also, you should not use a native function name for your functions: Sub sum()

这篇关于可见细胞总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆