【发布时间】:2020-04-26 10:46:49
【问题描述】:
如何在我当前的代码集中包含一个 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
【问题讨论】:
-
不清楚。 VLookup 返回一个单元格值。 CountIf 返回一个计数。函数返回一个值。您希望该值是多少?
-
嘿@Variatus,我实际上希望当前的代码集能够计算类似工作表中的vlookup结果的数量;希望它不会让您感到困惑。因此,例如(根据屏幕截图),如果我想找出 C 列中
3重复了多少Orange,该函数应该能够告诉我有 3;并汇总所有相似的工作表名称。