简体   繁体   English

excel在多个列中查找

[英]excel lookup within multiple columns

I hope someone can help with what I thought was a simple task. 我希望有人可以帮助我完成一个简单的任务。

I have a worksheet(Sheet1) that has numerous columns. 我有一个工作表(Sheet1),其中有许多列。 The first two are just a number and a name respectively. 前两个分别是数字和名称。 After these two columns there are a number of columns which is different for each row(eg row one has 10 colums row 2 has 4 columns etc). 在这两列之后,每一行都有许多列(例如,第一行有10个列,第二行有4列,等等)。

I have another worksheet(Sheet2) with 2 columns. 我有另一个工作表(Sheet2)2列。 The first column has a value that I want to lookup within Sheet1. 第一列具有我要在Sheet1中查找的值。 The problem is that the value can be anywhere within the columns on Sheet2 after the first two columns.(eg lookup value 123, value is in column 13 row 6 or lookup value 456 is in column 5 row 14) I then want the value in the column 1 row 6 on Sheet1 to be put into column 2 on Sheet2. 问题是该值可以在前两列之后的Sheet2的列中的任何位置。(例如,查找值123,值在第13列第6行或查找值456在第5列第14行),然后我想要在Sheet1上的第1列第6行要放入Sheet2的第2列。

I have tried using Vlookup,Hlookup and Lookup but cant seem to figure out how to get it to work. 我曾尝试使用Vlookup,Hlookup和Lookup,但似乎无法弄清楚如何使其工作。

Any help would be much appreciated 任何帮助将非常感激

Thanks Gareth 谢谢加雷斯

... I know you didn't ask for a VBA solution, but I just felt like writig one up to see how I would do it... ...我知道您并没有要求VBA解决方案,但我只是想写一个,看看我将如何做...

I made it "quick and dirty" without ANY error checking etc, but it will give you what you're looking for: 我使它“快速又脏”,没有任何错误检查等,但是它将为您提供所需的内容:

  Public Function FindInRange(Val As Variant, LookIn As Range, ColIndexNumber As Integer) As Range

  ' INPUTS:
     ' Val             = The Value you're looking for in your Range of cells
     ' Range           = The range of cells you're searching through
     ' ColIndexNumber  = The index of the column you want a value returned from within the row from which the value is found

  ' NOTE:
  ' This will only pull the first value matching your "Val" argument

  Dim FoundCell As Range

  Set FoundCell = LookIn.Find(what:=Val, LookAt:=xlWhole)

  If FoundCell Is Nothing Then
     Set FindInRange = Nothing
  Else
     Set FindInRange = Intersect(LookIn.Columns(ColIndexNumber), FoundCell.EntireRow)
  End If

  End Function

If you paste this into a VBA module, you can call it from your spreadsheet like any other function at that point. 如果将其粘贴到VBA模块中,则可以像此时的其他任何函数一样从电子表格中调用它。

Hope this helps :) 希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM