Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 2863|回复: 2

[分享] 分享一个自己写的函数,实现 sumifs 函数功能,同时实现 subtotal 不统计隐藏行功能

[复制链接]
发表于 2016-2-4 08:28 | 显示全部楼层 |阅读模式

  1. Option Explicit

  2. Function Sumifs_visible(qiuhehangshu_first As Long, qiuhe_liebiao As String, tiaojianquyu1 As Range, tj1 As String, _
  3.                         Optional tiaojianquyu2 As Range, Optional tj2 As String, Optional tiaojianquyu3 As Range, Optional tj3 As String)
  4. '可以用本代码实现 sumifs 函数功能,同时实现 subtotal 不统计隐藏行功能,最多限制条件为三个
  5. 'qiuhehangshu_first 求和开始行数
  6. 'qiuhe_liebiao 要求和的列,例如 A、B、C......列

  7.       Dim quyushuzu1
  8.       Dim quyushuzu2
  9.       Dim quyushuzu3
  10.       Dim xunhuan_hangshu
  11.       Dim lieshu
  12.       Dim qiuhe
  13.    
  14.       '求取列数
  15.       If Len(UCase(qiuhe_liebiao)) = 1 Then lieshu = Asc(UCase(UCase(qiuhe_liebiao))) - 64 '确认 列表为 字母 A 到 Z
  16.       
  17.       If Len(UCase(qiuhe_liebiao)) = 2 Then   '确认 列表为 字母 AA 到 ZZ
  18.              lieshu = (Asc(Mid(UCase(UCase(qiuhe_liebiao)), 1, 1)) - 64) * 26 + (Asc(Mid(UCase(UCase(qiuhe_liebiao)), 2, 1)) - 64) Mod 26 '确认 列表为 字母 AA 到 ZZ
  19.              If Len(UCase(qiuhe_liebiao)) = 2 And ((Asc(Mid(UCase(UCase(qiuhe_liebiao)), 2, 1)) - 64) Mod 26) = 0 Then lieshu = (Asc(Mid(UCase(UCase(qiuhe_liebiao)), 1, 1)) - 64) * 26 + 26
  20.       End If
  21.       
  22.       If Len(UCase(qiuhe_liebiao)) = 3 Then   '确认 列表为 字母 AAA 到 XFD
  23.              lieshu = ((Asc(Mid(UCase(UCase(qiuhe_liebiao)), 1, 1)) - 65) Mod 26) * 676 + (((Asc(Mid(UCase(UCase(qiuhe_liebiao)), 2, 1)) - 64) Mod 26) + 26) * 26 + (Asc(Mid(UCase(UCase(qiuhe_liebiao)), 3, 1)) - 64) '确认 列表为 字母 AAA 到 XFD
  24.              If Len(UCase(qiuhe_liebiao)) = 3 And ((Asc(Mid(UCase(UCase(qiuhe_liebiao)), 2, 1)) - 64) Mod 26) = 0 Then lieshu = ((Asc(Mid(UCase(UCase(qiuhe_liebiao)), 1, 1)) - 65) Mod 26) * 676 + (26 + 26) * 26 + (Asc(Mid(UCase(UCase(qiuhe_liebiao)), 3, 1)) - 64) '确认 列表为 字母 AAA 到 XFD
  25.       End If

  26.       '执行加总
  27.       If tiaojianquyu2 Is Nothing Then
  28.       
  29.              quyushuzu1 = tiaojianquyu1
  30.             
  31.              For xunhuan_hangshu = 1 To UBound(quyushuzu1)
  32.               
  33.                    If tj1 = "<>" And quyushuzu1(xunhuan_hangshu, 1) <> "" Then quyushuzu1(xunhuan_hangshu, 1) = "<>" '防止统计不为 空值的时候出错,将 非空值单元格全部替换为  "<>"
  34.             
  35.                    If quyushuzu1(xunhuan_hangshu, 1) = tj1 And Cells(qiuhehangshu_first + xunhuan_hangshu - 1, lieshu).EntireRow.Hidden = False Then
  36.                              qiuhe = qiuhe + Cells(qiuhehangshu_first + xunhuan_hangshu - 1, lieshu)
  37.                    Else
  38.                              qiuhe = qiuhe + 0       '如果单元格所在的行已经隐藏,则不加总数字,将其作为 数字 0 加总执行一次循环
  39.                    End If
  40.                   
  41.              Next xunhuan_hangshu
  42.             
  43.       ElseIf tiaojianquyu3 Is Nothing Then
  44.       
  45.             quyushuzu1 = tiaojianquyu1
  46.             quyushuzu2 = tiaojianquyu2
  47.      
  48.              For xunhuan_hangshu = 1 To UBound(quyushuzu1)
  49.             
  50.                    If tj1 = "<>" And quyushuzu1(xunhuan_hangshu, 1) <> "" Then quyushuzu1(xunhuan_hangshu, 1) = "<>"
  51.                    If tj2 = "<>" And quyushuzu2(xunhuan_hangshu, 1) <> "" Then quyushuzu2(xunhuan_hangshu, 1) = "<>"
  52.             
  53.                    If quyushuzu1(xunhuan_hangshu, 1) = tj1 And quyushuzu2(xunhuan_hangshu, 1) = tj2 And _
  54.                    Cells(qiuhehangshu_first + xunhuan_hangshu - 1, lieshu).EntireRow.Hidden = False Then
  55.                              qiuhe = qiuhe + Cells(qiuhehangshu_first + xunhuan_hangshu - 1, lieshu)
  56.                    Else
  57.                              qiuhe = qiuhe + 0
  58.                    End If
  59.                   
  60.              Next xunhuan_hangshu
  61.             
  62.       Else    '如果是多条件三个
  63.       
  64.             quyushuzu1 = tiaojianquyu1
  65.             quyushuzu2 = tiaojianquyu2
  66.             quyushuzu3 = tiaojianquyu3
  67.       
  68.              For xunhuan_hangshu = 1 To UBound(quyushuzu1)
  69.             
  70.                    If tj1 = "<>" And quyushuzu1(xunhuan_hangshu, 1) <> "" Then quyushuzu1(xunhuan_hangshu, 1) = "<>"
  71.                    If tj2 = "<>" And quyushuzu2(xunhuan_hangshu, 1) <> "" Then quyushuzu2(xunhuan_hangshu, 1) = "<>"
  72.                    If tj3 = "<>" And quyushuzu3(xunhuan_hangshu, 1) <> "" Then quyushuzu3(xunhuan_hangshu, 1) = "<>"
  73.             
  74.                    If quyushuzu1(xunhuan_hangshu, 1) = tj1 And quyushuzu2(xunhuan_hangshu, 1) = tj2 And quyushuzu3(xunhuan_hangshu, 1) = tj3 And _
  75.                    Cells(qiuhehangshu_first + xunhuan_hangshu - 1, lieshu).EntireRow.Hidden = False Then
  76.                              qiuhe = qiuhe + Cells(qiuhehangshu_first + xunhuan_hangshu - 1, lieshu)
  77.                    Else
  78.                              qiuhe = qiuhe + 0
  79.                    End If
  80.                   
  81.              Next xunhuan_hangshu

  82.       End If

  83.       Sumifs_visible = qiuhe
  84.               
  85. End Function
复制代码

评分

参与人数 1 +1 收起 理由
QCW911 + 1 来学习

查看全部评分

发表于 2016-2-4 09:44 | 显示全部楼层
回复

使用道具 举报

 楼主| 发表于 2016-2-5 09:25 | 显示全部楼层
QCW911 发表于 2016-2-4 09:44
高人啊,自己写函数

工作需要迫于无奈,边学边卖啊
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-5-31 22:02 , Processed in 0.174114 second(s), 8 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表