如何让VLOOKUP选择到VBA中的最下一行? [英] How to get VLOOKUP to select down to the lowest row in VBA?

查看:359
本文介绍了如何让VLOOKUP选择到VBA中的最下一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

寻求在单元格中自动插入VLOOKUP公式。
当录制宏时,我指示它用相同的公式填充下面的列。但是,当VLOOKUP通过更改(或多或少行)搜索的表格时,会出现一个问题。



在记录时,VLOOKUP将下拉到表中最后一行(273)。但是,我想设置它,以便它到最后一行。这意味着我可以在不同行数的表上运行脚本。



所选列将保持不变。选择
ActiveCell.FormulaR1C1 = _
= VLOOKUP(RC) [-20],Previous!R2C2:R273C22,17,FALSE)


解决方案

尝试这样:

 使用工作表(上一个)
范围(AJ2)。公式R1C1 = _
= VLOOKUP(RC [-20],上一个!R2C2:R& .Cells(.Rows.Count,2).End(xlUp).Row&C22,17,FALSE )
结束

其中:




  • 范围(AJ2)



    引用ActiveSheet


  • .Cells(.Rows.Count,2).End(xlUp).Row



    将引用上一个工作表,位于之内,带有工作表(上一个) - 以块结尾



Looking to automate the insertion of a VLOOKUP formula in a cell. When recording the macro I instruct it to populate the columns below with the same formula. Works great, however, there is an issue when the table that the VLOOKUP searches through changes (more or less rows).

As it's recorded, the VLOOKUP drops down to the final row in the table (273). However, I want to set it up so that it will go down to the very last row. Meaning that I can run the script on tables of varying numbers of rows.

Selected columns will remain the same.

Range("AJ2").Select
ActiveCell.FormulaR1C1 = _
    "=VLOOKUP(RC[-20], Previous!R2C2:R273C22,17,FALSE)"

解决方案

try this:

With Worksheets("Previous")
    Range("AJ2").FormulaR1C1 = _
        "=VLOOKUP(RC[-20], Previous!R2C2:R" & .Cells(.Rows.Count, 2).End(xlUp).Row & "C22,17,FALSE)"
End With

where:

  • Range("AJ2")

    will implicitly reference the ActiveSheet

  • .Cells(.Rows.Count, 2).End(xlUp).Row

    will reference "Previous" worksheet, being inside a With Worksheets("Previous")- End With block

这篇关于如何让VLOOKUP选择到VBA中的最下一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆