简体   繁体   English

清理VLOOKUP / INDEX(MATCH())VBA

[英]Cleaning up VLOOKUP / INDEX(MATCH()) VBA

I recorded the following macro: 我录制了以下宏:

ActiveCell.FormulaR1C1 = _
            "=INDEX(Sheet1!R2C2:R491C2,MATCH(Sheet2!RC[-2],Sheet1!R2C1:R491C1,0))"
        Range("D2").End (xlToLeft)
        Range("C2").End (xlDown)
        Range("D882").Select
        Range(Selection, Selection.End(xlUp)).Select
        Selection.FillDown

which, as some of you may be able to tell, is the result not just of recording but of trying to edit the 'select' statements out of said recording. 正如你们中的一些人所能说的那样,这不仅仅是录音的结果,而是试图从所述录音中编辑“选择”语句。 However, VBA tells me that I am using the .End property incorrectly. 但是,VBA告诉我,我正在错误地使用.End属性。 What is the best way to edit this so that select is not used, and so that the end of the range depends solely on the value in column A being filled for each row? 编辑它的最佳方法是什么,以便不使用select,并且范围的结尾仅取决于每行填充的A列中的值?

The structure for a more general macro would be: 更一般的宏的结构将是:

1) Select cell D2 in Sheet(1) (the cell under Header_1)
2) Write an INDEX(MATCH()) formula here referencing some specific columns in Sheet(1) and Sheet(2)
3) Select D2 in Sheet(2) and copy down to Dn where n is the first row where An = "".

Then I would simply repeat this for the other two INDEX(MATCH()) formulas needed. 然后我会简单地重复这个所需的另外两个INDEX(MATCH())公式。

UPDATE: 更新:

Is there also a way to have INDEX(MATCH()) set up so that the column range continues as long as cells are not blank (eg R2C1:Rnonblank). 是否还有一种方法可以设置INDEX(MATCH()),以便只要单元格不为空(例如R2C1:Rnonblank),列范围就会继续。 More specifically, can I just use the same & Cells() method? 更具体地说,我可以使用相同的&Cells()方法吗? These ranges will be longer or shorter depending on the workbook being targeted as well. 这些范围将更长或更短,具体取决于所针对的工作簿。

On the assumption this formula goes in column D from row 2 down as far as there are entries in column A, and needs to use all cells on Sheet1 column B: 假设此公式在第2行的D列中,只要列A中有条目,并且需要使用Sheet1列B上的所有单元格:

Dim LR as long
lr = sheets("Sheet1").Cells(Rows.Count, "B").End(xlUp).row
Range("D2:D" & Cells(Rows.Count, "A").End(xlUp).Row).FormulaR1C1 = _
                "=INDEX(Sheet1!R2C2:R" & lr & "C2,MATCH(RC[-2],Sheet1!R2C1:R" & lr & "C1,0))"

Short Answer You don't need these lines, they don't do anything. 简答你不需要这些线,他们什么都不做。 They got recorded when you were navigating to the bottom of your data using the keyboard. 当您使用键盘导航到数据底部时,它们被记录下来。

Long Answer The .End property returns a Range object, but your macro isn't doing anything on those lines. 长答案 .End属性返回一个Range对象,但是你的宏在这些行上没有做任何事情。 Instead of doing all of this selecting, decide what range you want and call .FillDown on that range. 而不是完成所有这些选择,决定你想要的范围,并在该范围内调用.FillDown The easiest is to have a fixed range: Range("D2:D822").Filldown but you can make it smarter than that, if you'd like. 最简单的是有一个固定的范围: Range("D2:D822").Filldown但如果你愿意,你可以使它更聪明。

Try describing, in detail, how you would decide what cells to pulldown and we can help you translate it into code. 尝试详细描述如何决定下拉哪些单元格,我们可以帮助您将其转换为代码。

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

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