[英]Simplifying complex excel formula with VBA
I have a macro that is generally slow due to overuse of LOOKUP formulas.由于过度使用 LOOKUP 公式,我有一个通常很慢的宏。 I want to insert some VBA variables to speed these up.
我想插入一些 VBA 变量来加速这些。 I am currently working on speeding up the formula below:in Excel:
我目前正在加速以下公式:在 Excel 中:
=IF(ISNA(MATCH(A2,Summary!B:B,0)),"n",I2-((I2/LOOKUP(2,1/(I:I<>""),I:I))*VLOOKUP(A2,Summary!$G$10:$H$902,2,FALSE)))
in VBA:在 VBA 中:
"=IF(ISNA(MATCH(RC[-9],Summary!C[-8],0)),""n"",RC[-1]-((RC[-1]/LOOKUP(2,1/(C[-1]<>""""),C[-1]))*VLOOKUP(RC[-9],Summary!R10C7:R902C8,2,FALSE)))"
The portion I need to replace is LOOKUP(2,1/(C[-1]<>""""),C[-1])
.我需要替换的部分是
LOOKUP(2,1/(C[-1]<>""""),C[-1])
。 All this does is reference the last non empty cell in column I. Right now I have the following code to return the address of the last cell in VBA所有这些都是引用 I 列中的最后一个非空单元格。现在我有以下代码来返回 VBA 中最后一个单元格的地址
Sub FormulaTest()
Set lRow = Range("I1").SpecialCells(xlCellTypeLastCell).Address
End Sub
I am trying to figure out how to implement this "lRow" into the VBA code for the formula.我想弄清楚如何将这个“lRow”实现到公式的 VBA 代码中。 Can anyone steer me in the right direction?
任何人都可以引导我朝着正确的方向前进吗?
**EDIT 1 Please see Fernando's comment below. **编辑 1 请参阅下面费尔南多的评论。 He has the right idea however the solution is still off a bit.
他的想法是正确的,但解决方案仍然有点偏离。 Ill try to explain it better in a few comments: First off, The first row is always a title row, the last row is always a sum row, the current tab is the "Sales" tab, and the amount of rows in any given Sales tab will vary (could be I1:I59, could be I:1:I323).
我会尝试在一些评论中更好地解释它:首先,第一行始终是标题行,最后一行始终是总和行,当前选项卡是“销售”选项卡,以及任何给定的行数销售标签会有所不同(可能是 I1:I59,也可能是 I:1:I323)。
In this example I1 is a row title and I59 is the sum of I2:I58.在本例中,I1 是行标题,I59 是 I2:I58 的总和。 Rows I2:I58 are dollar amounts.
行 I2:I58 是美元金额。 My macro places this formula in J2:J58.
我的宏将这个公式放在 J2:J58 中。 This formula takes each row's dollar amount (I2:I58) as a percentage of the total (I59) and multiplies it by an input amount on the Summary tab (the VLOOKUP).
此公式将每一行的美元金额 (I2:I58) 作为总金额 (I59) 的百分比,并将其乘以“摘要”选项卡 (VLOOKUP) 上的输入金额。 This amount is then subtracted proportionately from the dollar value in column I with the J cell showing the result.
然后从 I 列中的美元价值中按比例减去该金额,J 单元格显示结果。
I am looking to eliminate the need for the LOOKUP function (selects last non empty cell) within my formula above: LOOKUP(2,1/(C[-1]<>""""),C[-1]).我希望在上面的公式中消除对 LOOKUP 函数(选择最后一个非空单元格)的需要:LOOKUP(2,1/(C[-1]<>""""),C[-1])。
**EDIT 2 Fernando's solution worked. **EDIT 2 Fernando 的解决方案有效。 Thank you all for your input
谢谢大家的意见
This would return the last non-empty row in column I这将返回 I 列中的最后一个非空行
with Worksheets("Summary")
lRow = .Cells(.Rows.Count, "I").End(xlUp).Row
end with
So your code would be所以你的代码将是
sub testy
dim lRow as long
with Worksheets("Summary")
lRow = .Cells(.Rows.Count, "I").End(xlUp).Row
end with
"=IF(ISNA(MATCH(RC[-9],Summary!C[-8],0)),""n"",RC[-1]-_
((RC[-1]/R"&lRow&"C[-1])*VLOOKUP(RC[-9],Summary!R10C7:R902C8,2,FALSE)))"
In your solution you're using xlCellTypeLastCell
.在您的解决方案中,您使用的是
xlCellTypeLastCell
。 This is very useful, but it calculates based on UsedRange
, which may not be what you want.这非常有用,但它是根据
UsedRange
计算的,这可能不是您想要的。 with this, if you have data up to row n
and then you update the data and now you have less records, the last row with xlCellTypeLastCell
will still be n
, so be careful with that.有了这个,如果你有第
n
行的数据,然后你更新数据,现在你的记录减少了,最后一行xlCellTypeLastCell
仍然是n
,所以要小心。
Assuming that you are doing all your work on the active sheet, looking up to a "Summary" sheet:假设您正在活动工作表上完成所有工作,查找“摘要”工作表:
Sub fillCol()
Dim aRow As Long, bRow As Long
aRow = Cells(Rows.Count, "I").End(xlUp).Row
bRow = Sheets("Summary").Cells(Rows.Count, "I").End(xlUp).Row
Range("J2:J" & aRow).FormulaR1C1 = "=IF(ISNA(MATCH(RC[-9],Summary!C[-8],0)),""n"",RC[-1]-" _
& "((RC[-1]/" & aRow & ")*VLOOKUP(RC[-9],Summary!R10C7:R" & bRow & "C8,2,FALSE)))"
End Sub
You made need to change the columns which contain the contiguous range (in order to determine the last row)您需要更改包含连续范围的列(以确定最后一行)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.