[英]Lookup value in 2-D List in Excel and find last instance
I am in need of some major assistance.我需要一些重要的帮助。 I am trying to create a VBA function that will allow me to find the last five instances of a string in a 2-D set of data and return the name of the column that each instance is in.
我正在尝试创建一个 VBA function ,这将允许我在二维数据集中找到字符串的最后五个实例并返回每个实例所在的列的名称。
I have a data workbook which looks like this: picture of data worksheet我有一个看起来像这样的数据工作簿:数据工作表的图片
Essentially, every day every employee has a different placement and we input their names into this sheet.基本上,每天每个员工都有不同的位置,我们将他们的名字输入到这张表中。 On a separate sheet, pictured below, the name of the employee is entered and their percentages and last 5 placements are shown.
在一张单独的表格上,如下图所示,输入了员工的姓名,并显示了他们的百分比和最近 5 个职位。 I can do simple things in VBA like making functions and the like, however I have no idea how to go about this beast.
我可以在 VBA 中做一些简单的事情,比如制作函数等,但是我不知道如何 go 关于这个野兽。 Any help would be much appreciated.
任何帮助将非常感激。 Picture of Percentages page
百分比页面的图片
I think this should do it: I was interested to investigate it as some of the microsoft docs/functionality around FindNext
I think might be bugged.我认为应该这样做:我有兴趣调查它,因为我认为
FindNext
周围的一些微软文档/功能可能被窃听。
The function takes as a parameter the name of the person and a range of all the logged palcements. function 将人名和所有记录的范围作为参数。 It returns an array of 5 columns so you'll need to familiarise yourself with
ctrl-shift-enter
for array functions它返回一个包含 5 列的数组,因此您需要熟悉
ctrl-shift-enter
以获取数组函数
eg Formula for b13:f13
is =find_last_5(A13, $A$3:$G$10)
例如
b13:f13
的公式是=find_last_5(A13, $A$3:$G$10)
Option Explicit
Function find_last_5(ByVal employee_name As String, ByRef placement_data As Range)
Const number_placements_required = 5
' return array
Dim placements() As Variant
'redim to required size (1 row by 5 columns)
ReDim placements(0, number_placements_required - 1)
Dim i As Long
' initialise return array to #n/a
For i = 0 To number_placements_required - 1
placements(0, i) = CVErr(xlErrNA)
Next i
i = 0
Dim first_found As String
Dim found As Range
With placement_data
'search passed array, backwards looking along rows from last/bottom row cell towards the top left
Set found = .Find(what:=employee_name, _
after:=placement_data.Range("a1"), _
LookIn:=xlValues, _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByRows)
If Not found Is Nothing Then
first_found = found.Address
Do
placements(0, i) = found.Column
Set found = .Find(what:=employee_name, _
after:=found, _
LookIn:=xlValues, _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByRows)
i = i + 1
' do until 5 found or nothing found or we wrap back to first address found
Loop While i < number_placements_required And _
Not found Is Nothing _
And found.Address <> first_found
End If
End With
find_last_5 = placements
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.