excel - COUNTIFS 函数跨多个工作表工作
问题描述
如何在我当前的代码集中包含一个 vlookup,以对所有类似工作表中的所有 vlookup 结果进行计数。我拥有的代码将尝试对一个指定单元格或列或行中的整个数据范围跨工作表执行 countif。相反,我希望下面的函数能够计算跨相似名称工作表的列中 vlookup 结果的数量。
Function myCountIfSheet1(rng As Range, criteria) As Long
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name Like "Sheet1*" Then
myCountIfSheet1 = myCountIfSheet1 + WorksheetFunction.CountIf(ws.Range(rng.Address), criteria)
End If
Next ws
End Function
Public Function shifted_lookup(lookup_value As Variant, table_array As Range, column_index As Integer, range_lookup As Integer) As Variant
Dim curr_wsname As String, oth_wsname As String
Dim curr_ws As Worksheet, oth_ws As Worksheet
Set curr_ws = ActiveSheet
curr_wsname = curr_ws.Name
oth_wsname = Right(curr_wsname, 3)
Set oth_ws = Worksheets(oth_wsname)
Dim src_rng_base As String, src_rng As Range
src_rng_base = table_array.Address
Set src_rng = oth_ws.Range(src_rng_base)
Dim aux As Variant
shifted_lookup = Application.WorksheetFunction.VLookup(lookup_value, src_rng, column_index, range_lookup)
End Function
解决方案
这应该可以完成这项工作。请尝试一下。
Function myCountIfSheet1(Rng As Range, _
Clm1 As Long, _
Crit1 As Variant, _
Clm2 As Long, _
Crit2 As Variant) As Long
' 011
Dim Fun As Long ' function return value
Dim Ws As Worksheet
For Each Ws In ThisWorkbook.Worksheets
With Ws
If .Name Like "Sheet1*" Then
Fun = Fun + WorksheetFunction.CountIfs( _
.Range(Rng.Columns(Clm1).Address), Crit1, _
.Range(Rng.Columns(Clm2).Address), Crit2)
End If
End With
Next Ws
myCountIfSheet1 = Fun
End Function
为了便于调用,我将函数调用结构化为仅提供一个范围地址。在我的测试中,我使用了 A1:D30。列 (A) 包含一个标准,列 (D) 包含另一个。当然,列(A)是范围的第一列 - 列(1) - 列 D 是范围的列(4)。因此,以下函数调用将在 A 列中查找“3”,在 D 列中查找“red”。
Debug.Print myCountIfSheet1(Range("A1:D30"), 4, "red", 1, 3)
标准的顺序无关紧要。您还可以使用相同的结构添加更多条件。
推荐阅读
- bit-manipulation - 形式 (start + 2 * i) 的所有元素的异或,其中 i 的范围从 0 到 n - 1
- class - kotlin 关于课堂语法的基本问题
- typo3-11.x - 在typo3扩展版本11.2中动态生成javascript
- node.js - 关于Spring Boot的加密方式的一个问题
- sql - 如何从 IEnumerable 获取数据
? - python - ASCII 编解码器不能编码字符,只能 concat str
- python - 如何在python中将列表列表与引用变量连接起来
- react-native - ref.current null 不是对象
- javascript - 如何禁用pdf查看器的屏幕截图
- c++ - 插入空向量后大小为零的向量