简体   繁体   English

VBA查找-查找范围错误

[英]VBA Lookup- lookup range error

I've exhausted every option so I'm left with throwing myself at your mercy. 我已经尽了所有的选择,所以我只能任由你摆布。 I'm trying to automate a report in excel but the lookup just isn't working. 我正在尝试在excel中自动执行报告,但查找不起作用。 the idea is it does a lookup on a pivot table that is refreshed every day and moves along to the next empty day and gives the results. 其思想是在数据透视表上进行查找,该数据透视表每天刷新并移至下一个空天并给出结果。 I've tried recording and the lookup works but each day it moves the look up range one column on and I can't get it to fix. 我已经尝试了录制并且查找正常,但是每天它都会将查找范围移动到一列上,而我无法修复它。 My code is below, any help will be greatly appreciated. 我的代码如下,任何帮助将不胜感激。

   Range("B36").Select
    Selection.End(xlToRight).Select
    ActiveCell.Offset(0, 1).Select


  Dim row As Integer
    For i = 36 To 40


Set inRange = Range("B" & i & ":B" & i)
Set LookupRange = Sheets("MV Pivot").Columns("N:R")


MsgBox (inRange)

    ActiveCell.FormulaR1C1 = _
        "=IFERROR(VLOOKUP(" & inRange & "," & LookupRange & ",5,FALSE),0)"
                      ActiveCell.Offset(1, 0).Select
        Next I

Thanks 谢谢

Logie143 洛奇143

Just a few things.. 只是几件事。

  1. As FAURILLOU Amandine mentioned, I think you can reference the inRange as Range("B" & i) - agreed. 正如FAURILLOU Amandine所述,我认为您可以将inRange引用为Range(“ B”&i)-同意。
  2. As Darren mentioned use formula - agreed. 正如达伦提到的使用公式-同意。
  3. I would personally look at the pivot table, and try to reference the data range for the pivot, as opposed to reference it to. 我将亲自查看数据透视表,并尝试引用数据透视表的数据范围,而不是对其进行引用。 Just the way I would try to do it. 就是我尝试做的方式。 That way if the location of the pivot changed you'll be fine. 这样,如果枢轴的位置发生变化,您就可以了。 Had a very quick look at the object model and it would something like: 快速查看了对象模型,结果如下:

    Worksheets("MV Pivot").PivotTables("The Name").DataBodyRange 工作表(“ MV Pivot”)。PivotTables(“名称”).DataBodyRange

  4. Although you use a formula to lookup the pivot data column, you could actually call the Evaluate function. 尽管您使用公式来查找数据透视表数据列,但实际上可以调用Evaluate函数。 If you use your formula in the cell, if the pivot table data changes, it will also change the historical data since its a formula. 如果在单元格中使用公式,则数据透视表数据发生更改时,由于其公式也将更改历史数据。 You can get around this by simply hardcoding the result. 您可以通过简单地对结果进行硬编码来解决此问题。

All of that being said, please give this a go and see if it works/fixes your issue. 话虽如此,请尝试一下,看看它是否有效/解决了您的问题。

Sub Test()

Range("B36").Select
Selection.End(xlToRight).Select
ActiveCell.Offset(0, 1).Select

Dim inRange As Range, LookupRange As Range
Dim row As Integer, i As Integer

For i = 36 To 40
    Set inRange = Range("B" & i)
    Set LookupRange = Range("$K$36:$P$41")
    MsgBox (inRange)
    ActiveCell.Formula = Evaluate("=IFERROR(VLOOKUP(" & inRange.Address & "," & LookupRange.Address & ",5,FALSE),0)")
    ActiveCell.Offset(1, 0).Select
Next i

End Sub

Hope that helps. 希望能有所帮助。

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

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