【问题标题】:VBA: Placing a forumula down a column using a vlookup formulaVBA:使用 vlookup 公式将公式放在列中
【发布时间】:2014-09-24 07:55:30
【问题描述】:

下面我试图将公式放在最后一列的右侧,从第 2 行开始。我知道 For 语句有效,以及搜索最后一列/行,因为我在将公式放在列下时的前一个宏。我唯一的问题是如何使 VLookup 公式正常工作?

最终目标: 1)论坛在最后一个右侧的列 2) Vlookup 在名为“Lookup”的选项卡上查找 For 语句中给定行的最后一列中的值 3) 在此 Lookup 选项卡上,A 列是找到值的位置,但我需要返回第二列值。

请在以“=iferror(...”开头的论坛中归零。我目前收到错误“应用程序定义或对象定义”错误。

EThree = Cells(Rows.Count, 4).End(xlUp).Row
NumThree = Evaluate("=COUNTA(9:9)")

For r = 2 To EThree
    Cells(r, NumThree + 2).Formula = "=IFERROR(((Vlookup(" & Cells(r, 14).Value & ",Lookup!$A:$B,2,0)""))))"

Next

【问题讨论】:

  • 我认为你的问题是这样的:Cells(r, 14).Address(False, False)
  • 尝试使用Cells(r, 14).Address.Value
  • 这并没有解决问题。我仍然收到错误“应用程序定义或对象定义错误”。我以前以同样的方式使用过这个地址功能,但这是唯一一次,我遇到了困难。我的感觉是问题在于我如何输入 Vlookup 论坛以及我的“&”可能。
  • 您能否修改您的问题以显示代码当前存在(即,按照我建议的修改)?让我看看您正在使用什么,并且可能可以为您指明正确的方向:)
  • 废话,我的评论中有错字,应该是Cells(r, 14).Value不是 ...Address.Value。先试试吧。

标签: vba vlookup


【解决方案1】:

您可以一次性输入公式;无需循环。
试试这个:

With Sheets("NameOfWorksheet") '~~> change to suit
    '~~> first get the last row and column
    Dim lrow As Long, lcol As Long
    lrow = .Range("D" & .Rows.Count).End(xlUp).Row
    lcol = .Cells(9, .Columns.Count).End(xlToLeft).Column
    Dim rngToFillFormula As Range, mylookup As String
    '~~> get the lookup value address
    mylookup = .Cells(2, lcol).Address(False, False, xlA1)
    '~~> set the range you need to fill your formula
    Set rngToFillFormula = .Range(.Cells(2, lcol), Cells(lrow, lcol)).Offset(0, 1)
    rngToFillFormula.Formula = "=IFERROR(VLOOKUP(" & mylookup & _
        ",Lookup!A:B,2,0),"""")"
End With

我们所做的在 cmets 中进行了解释。 HTH。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-04
    • 1970-01-01
    • 1970-01-01
    • 2018-12-31
    相关资源
    最近更新 更多