简体   繁体   English

源为QUERY TABLE(或ListObject)时,EXCEL VBA VLOOKUP

[英]EXCEL VBA VLOOKUP when the source is QUERY TABLE (or ListObject)

I'm stuck, I used to run a code that perform a vlookup from a query (data from web power query) - see example in the code below). 我被困住了,我曾经运行过一个通过查询(来自Web Power查询的数据)执行vlookup的代码-请参见下面代码中的示例。 It all ran smooth, until i tried to add more queries, and then I needed to change the function to perform the vlookup from varies queries, and for that I needed the queryName to be a var. 一切运行顺利,直到我尝试添加更多查询,然后我需要更改函数以通过各种查询执行vlookup,为此,我需要将queryName设为var。 no matter what I try, it doesn't work for me ( replacing the "US" in the vlookup ): 不管我尝试什么,都对我不起作用( 替换vlookup中的“ US” ):

Function doVlookup(selectedSheet As String)

Dim queryName As ListObject
Set queryName = Worksheets("US API").ListObjects(1)

Debug.Print queryName.Name

Sheets(selectedSheet).Select
Range("A1").Select

Selection.End(xlToRight).Select
ActiveCell.Offset(0, 1).Columns("A:A").EntireColumn.Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
ActiveCell.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=IF(VLOOKUP(RC1,US,5,0)<>0, VLOOKUP(RC1,US,5,0),""-"")"
ActiveCell.Offset(-1, 0).Select
ActiveCell.FormulaR1C1 = "=TODAY()"
ActiveCell.Offset(0, -1).Select
Selection.End(xlDown).Select
ActiveCell.Offset(0, 1).Select
Range(Selection, Selection.End(xlUp)).Select
Selection.FillDown
Selection.End(xlUp).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("A1").Select
End Function

the var queryName, does have the desired table, but when I run the code it just return #NAME! var queryName,确实具有所需的表,但是当我运行代码时,它只返回#NAME! for all keywords. 对于所有关键字。 If I hard code it back to "US" (in the example, it is hard-coded) it runs without a problem. 如果我将其硬编码回“ US”(在本例中为硬编码),则可以正常运行。 I tried: 我试过了:

  • queryName queryName
  • Range(queryName) 范围(queryName)
  • queryName.Range queryName.Range
  • queryName!R1:R100 R1:R100
  • queryName.Range.Select queryName.Range.Select
  • queryName.DataBodyRange queryName.DataBodyRange

For clarification "queryName" suppose to replace the "US" in the following: "=IF(VLOOKUP(RC1,US,5,0)<>0, VLOOKUP(RC1,US,5,0),""-"")" Just that I am not sure how it's done. 为了澄清起见,“ queryName”假设在以下替换“ US”: "=IF(VLOOKUP(RC1,US,5,0)<>0, VLOOKUP(RC1,US,5,0),""-"")"只是我不确定它是如何完成的。 "US" is a name of a table that is loaded from a connection of "data from web" to the sheet "US API". “ US”是从“来自Web的数据”到工作表“ US API”的连接加载的表的名称。

any idea what should bet put in the place of the hard-coded US to make it work? 任何想法都应该押在硬编码的美国上才能使其生效?

I realize it's a month after the fact, and you've likely moved on to bigger and better things, but I think I may know how to tackle this. 我意识到事实已经过去了一个月,您可能已经开始着手做更大更好的事情了,但是我想我可能知道如何解决这个问题。 Here is an example of a dumbed-down spreadsheet and the VBA code that will implement a vlookup on various listobjects. 这是一个精简电子表格和VBA代码的示例,该代码将在各种列表对象上实现vlookup。

在此处输入图片说明

In this example, I use the corresponding table values from the first, second and third tables (listobjects) in each of the lookups for G7-9, just to show how each can be declared dynamically. 在此示例中,我在G7-9的每个查找中使用来自第一张,第二张和第三张表(列表对象)的相应表值,只是为了说明如何动态声明每个表。

Dim lo As ListObjects
Set lo = ActiveSheet.ListObjects

Range("G7").FormulaR1C1 = "=VLOOKUP(RC[-1]," & lo(1).Name & "[#All],2,FALSE)"
Range("G8").FormulaR1C1 = "=VLOOKUP(RC[-1]," & lo(2).Name & "[#All],2,FALSE)"
Range("G9").FormulaR1C1 = "=VLOOKUP(RC[-1]," & lo(3).Name & "[#All],2,FALSE)"

I suspect the missing link in your code above is the [] following the listobject name, which indicates the range within the table to use. 我怀疑上面代码中缺少的链接是listobject名称后面的[] ,它表示要使用的表范围。

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

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