将工作表单元格的值分配给常量 [英] Assigning the value of a worksheet cell to a constant
问题描述
我正在尝试将工作表单元格的值分配给VBA宏中的常量变量。该操作背后的逻辑是,最终用户应该在运行宏之前在指定的单元格中输入当前周。由于这个价值将在整个宏观中被重用,我想要保护它,所以我试图将它声明为公共常量:
I am trying to assign the value of a worksheet cell to a constant variable in a VBA macro. The logic behind that action is that the end user is supposed to enter the current week in a specified cell before running the Macro. Since this value is going to be reused throughout the macro and I wanted to play it safe, I tried to declare it as a public constant:
private const thisWeek as Integer = Range("B1")
但是我收到一条错误消息关于需要的恒定值。那么,甚至可以在VBA中声明一个这样的常数呢?
However I get an error message about a constant value being needed. So, is it even possible to declare a constant like this in VBA?
推荐答案
不可以。作为这个词,应该是 。
No it is not possible. As the word suggest it should be Constant.
解决方法 b
Public Const weekRange As String = "$B$1"
然后在代码中:
Sub Something()
Dim thisWeek As Integer: thisWeek = Range(weekRange).Value
'~~> some codes here
End Sub
这篇关于将工作表单元格的值分配给常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!