繁体   English   中英

Excel VBA在公式中使用一些命名范围

[英]Excel VBA using a few Named Ranges in a formula

我正在尝试在VBA的公式中一起使用一些命名范围,但是excel计算似乎需要很长时间。 命名范围每个都有近70k的数​​据(全部具有相同的行数)。 下面是代码示例:

Dim i, erow, ecol As Long

erow = Sheets("Sheet1").Range("A11").End(xlDown).Row
ecol = Sheets("Sheet1").Range("A11").End(xlToRight).Column

Sheets("Sheet1").Range(Cells(12, ecol + 2), Cells(erow, ecol + 2)).Formula = "=(0.03 - Ith_LT) * (a_LI_LT * 0.03 ^ 2 + b_LI_LT * 0.03 + c_LI_LT)

Ith_LT,a_LI_LT,b_LI_LT,c_LI_LT是命名范围。

我试图通过先将命名范围存储在内存中然后调用它来加快速度,但无济于事。 下面是我的代码。

Dim i, erow, ecol As Long
Dim Ith, a, b, c As Variant

erow = Sheets("Sheet1").Range("A11").End(xlDown).Row
ecol = Sheets("Sheet1").Range("A11").End(xlToRight).Column
Ith = Range("Ith_LT")
a = Range("a_LI_LT")
b = Range("b_LI_LT")
c = Range("c_LI_LT")
Sheets("Sheet1").Range(Cells(12, ecol + 2), Cells(erow, ecol + 2)).Value = (0.03 - Ith) * (a * 0.03 ^ 2 + b * 0.03 + c)

当我运行此命令时,会弹出类型不匹配错误。 有什么办法可以使这项工作吗? 还是有办法使此代码运行更快? 谢谢。

在变量声明中,您仅定义具有给定类型的最后一个变量:

Dim i, erow, ecol As Long
Dim Ith, a, b, c As Variant

您应该在第二行中使用类型范围而不是variant,而不是variant

Dim Ith as range, a as range, b as range, c  as range

之后,使用此行来使用命名范围

Set Ith = Range("Ith_LT")

@Zabjaku根据您的评论,我假设所有范围均为单列,并且将根据公式填充最终输出,然后您可以使用它。

这将填充您的输出列,如果命名范围发生更改,则无需更改公式。

Sheets("Sheet1").Range(Cells(12, ecol + 2), Cells(erow, ecol + 2)).Value = "=(0.03-" & Replace(Range("Ith_LT").Cells(1, 1).Address, "$", "") & ")*(" & Replace(Range("a_LI_LT").Cells(1, 1).Address, "$", "") & "*0.03^2+" & Replace(Range("b_LI_LT").Cells(1, 1).Address, "$", "") & "*0.03+" & Replace(Range("c_LI_LT").Cells(1, 1).Address, "$", "") & ")"

暂无
暂无

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

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