首页 文章

VBA:使用vlookup公式将一个论坛放在一列中

提问于
浏览
0

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

最终目标:1)在最后一个右侧的列上的Forumla 2)Vlookup在名为“查找”的选项卡上的For语句中查找给定行的最后一列中的值3)在此查找选项卡上,列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

1 回答

  • 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
    

    我们所做的工作在评论中有所解释 . HTH .

相关问题