从 vba 中的 vlookup 返回一个行号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13805942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 14:34:50  来源:igfitidea点击:

return a row number from a vlookup in vba

vbavlookup

提问by Mary Ellen

I need to know what code to use in this situation.

我需要知道在这种情况下使用什么代码。

I am using vlookup in vba to locate a certain record number. I then need to know what row number the record is located in. I have tried the following, but received errors:

我在 vba 中使用 vlookup 来定位某个记录号。然后我需要知道记录所在的行号。我尝试了以下操作,但收到错误:

nRowSavedRecord = [Vlookup(Cells(nRows, nColRecNmbr),Range("RecordInfo"),2,False).Row]

This gives me a "Type Mismatch" error.

这给了我一个“ Type Mismatch”错误。

or

或者

nRowSavedRecord = Application.vlookup(cells(nRows, nColRecNmbr), Range("RecordInfo"),2,False).Rows

This gives me an "Object Required" error.

这给了我一个“ Object Required”错误。

I'm sure whatever I'm missing is simple.
(nRowSavedRecordis a Long) Can anyone help?

我确定我缺少的东西很简单。
nRowSavedRecordLong)任何人都可以帮忙吗?

Thanks!

谢谢!

回答by martin

There are several problems in your question.

你的问题有几个问题。

  • VLookup is not property of Application, but of Application.WorksheetFunction
  • VLookup returns value, not reference to a cell, so you cannot get row from it
  • VLookup 不是 Application 的属性,而是 Application.WorksheetFunction 的属性
  • VLookup 返回值,而不是对单元格的引用,因此您无法从中获取行

You probably want to use the Match function, for example like this:

您可能想要使用 Match 函数,例如像这样:

nRowSavedRecord = Application.WorksheetFunction.Match(Cells(nRows, nColRecNmbr), Range("RecordInfo"), 0)

The last 0 means that this is an exact match. You also have to make sure that RecordInfois a one-dimensional range

最后一个 0 表示这是完全匹配。您还必须确保这RecordInfo是一个一维范围