简体   繁体   English

Excel-通过随机单元格中的字符串引用另一个工作表,并引用其给定行中的值

[英]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)易于安装和使用:

  1. ALT-F11 brings up the VBE window ALT-F11弹出VBE窗口
  2. ALT-I ALT-M opens a fresh module ALT-I ALT-M打开一个新模块
  3. paste the stuff in and close the VBE window 将内容粘贴并关闭VBE窗口

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:

  1. bring up the VBE window as above 如上调出VBE窗口
  2. clear the code out 清除代码
  3. close the VBE window 关闭VBE窗口

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.

相关问题 如何使用参考单元格的值对另一个工作表中的excel数据求和 - How to sum excel data in another worksheet, using the value of a reference cell 在Excel中将单元格值引用为字符串 - Reference cell value as string in Excel 通过将另一个单元格中的字符串引用为列来在另一个工作表中引用动态单元格 - Dynamic cell reference in another worksheet by referencing string in another cell as column Excel Worksheet公式计算出要在单元格引用计算中使用的行 - Excel Worksheet formulas that figure out the row it is on to use in cell reference calculations 引用另一个工作表中的单元格 - Reference a cell from another worksheet 在 excel 中,我试图从另一个工作表中引用合并的单元格,但它显示 #value 错误 - In excel, I'm trying to reference a merged cell from another worksheet but it says #value error Excel 中另一个工作表中单元格的参考值(无背景填充等) - reference value only(no background fill, etc.) of a cell in another worksheet in Excel 如何使用公式中的ID引用另一个Excel工作表中的单元格 - how to reference a cell in another excel worksheet using ID from a formula Excel VBA:引用另一个工作表中单元格的公式(变量) - Excel VBA: Formula with reference to cell in another worksheet (variable) Excel:从另一个添加行的工作表的单元格中引用一个公式 - Excel: Reference a formula in a cell from another worksheet where rows are added
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM