[英]Excel - reference another worksheet by a string in a random cell, and reference a value in its given row
I'm trying to reference a string in another worksheet in a random cell, and reference a value in its given row. 我试图在随机单元格中的另一个工作表中引用一个字符串,并在其给定的行中引用一个值。
The reason is that I'm working with 30+ documents, and the given string (and the value that I need next to it) Appears at different positions in each document. 原因是我正在处理30多个文档,并且给定的字符串(及其旁边需要的值)出现在每个文档的不同位置。
I need to collect these values from each document, and combine it into one. 我需要从每个文档中收集这些值,并将其组合为一个。
Thanks! 谢谢!
Say on Sheet6 there is some cell that contains the phrase: 说在Sheet6上有一些包含短语的单元格:
happiness is
and we want to retrieve the contents of the cell just to the right of it: 我们想检索单元格右边的内容:
First enter the following UDF in a standard module: 首先在标准模块中输入以下UDF :
Public Function Find_Range(Find_Item As Variant, _
Search_Range As Range, _
Optional LookIn As Variant, _
Optional LookAt As Variant, _
Optional MatchCase As Boolean) As String
Dim c As Range
If IsMissing(LookIn) Then LookIn = xlValues 'xlFormulas
If IsMissing(LookAt) Then LookAt = xlPart 'xlWhole
If IsMissing(MatchCase) Then MatchCase = False
With Search_Range
Set c = .Find( _
What:=Find_Item, _
LookIn:=LookIn, _
LookAt:=LookAt, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=MatchCase, _
SearchFormat:=False)
If Not c Is Nothing Then
Find_Range = Search_Range.Parent.Name & "!" & c.Address
End If
End With
End Function
The UDF will search for Find_Item
and return the address as a string. UDF将搜索
Find_Item
并将地址作为字符串返回。 So: 所以:
=find_range("happiness is",Sheet6!1:1048576)
will return: 将返回:
Sheet6!$E$13
Note that the search range is the entire Sheet6 . 请注意,搜索范围是整个 Sheet6 。 But we want an OFFSET() , so pick some cell in Sheet5 and enter:
但是我们需要一个OFFSET() ,因此在Sheet5中选择一些单元格并输入:
=OFFSET(INDIRECT(find_range("happiness is",Sheet6!1:1048576)),0,1)
User Defined Functions (UDFs) are very easy to install and use: 用户定义函数(UDF)易于安装和使用:
If you save the workbook, the UDF will be saved with it. 如果您保存工作簿,则UDF将随之保存。 If you are using a version of Excel later then 2003, you must save the file as .xlsm rather than .xlsx
如果您在2003年以后使用Excel版本,则必须将文件另存为.xlsm而不是.xlsx
To remove the UDF: 删除UDF:
To use the UDF from Excel: 要从Excel使用UDF:
=Find_Range(A1,C1:X100) =查找范围(A1,C1:X100)
To learn more about macros in general, see: 要总体上了解有关宏的更多信息,请参见:
http://www.mvps.org/dmcritchie/excel/getstarted.htm http://www.mvps.org/dmcritchie/excel/getstarted.htm
and 和
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx http://msdn.microsoft.com/zh-CN/library/ee814735(v=office.14).aspx
and for specifics on UDFs, see: 有关UDF的详细信息,请参见:
http://www.cpearson.com/excel/WritingFunctionsInVBA.aspx http://www.cpearson.com/excel/WritingFunctionsInVBA.aspx
Macros must be enabled for this to work! 必须启用宏才能使其正常工作!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.