Excel – 通过随机单元格中的string引用另一个工作表,并在给定行中引用一个值
我想在一个随机的单元格中的另一个工作表中引用一个string,并在其给定的行中引用一个值。
原因是我正在处理30多个文档,并且给定的string(以及我需要的值)出现在每个文档的不同位置。
我需要从每个文档中收集这些值,并将其合并为一个。
谢谢!
在Sheet6上说有一些单元格包含这个短语:
happiness is
我们想要检索它右边的单元格的内容:
首先在标准模块中input以下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
UDF将searchFind_Item
并以stringforms返回地址。 所以:
=find_range("happiness is",Sheet6!1:1048576)
将返回:
Sheet6!$E$13
请注意,search范围是整个 Sheet6 。 但是我们想要一个OFFSET() ,所以在Sheet5中select一些单元格并input:
=OFFSET(INDIRECT(find_range("happiness is",Sheet6!1:1048576)),0,1)
用户定义的函数(UDF)非常易于安装和使用:
- ALT-F11调出VBE窗口
- ALT-I ALT-M打开一个新的模块
- 粘贴东西,closuresVBE窗口
如果保存工作簿,则UDF将随之保存。 如果您在2003年以后使用的是Excel版本,则必须将该文件另存为.xlsm而不是.xlsx
要删除UDF:
- 如上所示调出VBE窗口
- 清除代码
- closuresVBE窗口
从Excel中使用UDF:
= Find_Range(A1,C1:X100)
要了解有关macros的更多信息,请参阅:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
和
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
有关UDF的具体信息,请参阅:
http://www.cpearson.com/excel/WritingFunctionsInVBA.aspx
macros必须启用这个工作!