数组可以声明为常量吗?

时间:2016-12-08 18:15:22

标签: arrays vba excel-vba constants excel

是否可以:

  1. 将数组声明为常量

    或者

  2. 使用变通方法声明一个受保护的数组,以防止添加,删除或更改元素,因此在宏的生命周期内功能上保持不变?

  3. 当然,我可以这样做:

    Const myConstant1 As Integer = 2
    Const myConstant2 As Integer = 13
    Const myConstant3 As Integer = 17
    Const myConstant4 ...and so on
    

    ...但它失去了使用数组的优雅。我还可以将常量加载到数组中,并在每次使用时重新加载它们,但是在使用之前无法使用这些常量值重新加载数组可能会将代码暴露给已经更改的“常量”值。

    欢迎任何可行的答案,但理想的答案是可以设置一次而不需要在修改其他代码时进行任何更改/维护。

10 个答案:

答案 0 :(得分:25)

您可以使用函数返回数组并将该函数用作数组。

Function ContantArray()
    ContantArray = Array(2, 13, 17)
End Function

enter image description here

enter image description here

答案 1 :(得分:6)

如何让它成为一个功能?如:

Public Function myConstant(ByVal idx As Integer) As Integer
    myConstant = Array(2, 13, 17, 23)(idx - 1)
End Function

Sub Test()
    Debug.Print myConstant(1)
    Debug.Print myConstant(2)
    Debug.Print myConstant(3)
    Debug.Print myConstant(4)
End Sub

没有人可以更改它,调整其大小或编辑其内容......此外,您可以只在一行上定义常量!

答案 2 :(得分:4)

我宣布String常量为"1,2,3,4,5",然后使用Split创建一个新数组,如下所示:

Public Const myArray = "1,2,3,4,5"

Public Sub createArray()

        Dim i As Integer
        A = Split(myArray, ",")

        For i = LBound(A) To UBound(A)
                Debug.Print A(i)
        Next i

End Sub

当我尝试在ReDim上使用ReDim PreserveA时,它没有让我这么做。这种方法的缺点是你仍然可以编辑数组的值,即使你不能改变大小。

答案 3 :(得分:2)

我知道这是一个老问题,但这些档案在发布后经常被扫描多年,所以我认为在原始日期之后很长时间添加内容没有问题。

如何创建一个具有返回“数组”值的只读属性的类?您可以使用与数组索引相同的语法来指定参数,并且仅有效地定义 GET 属性使其成为只读的。在类中定义常量值,它会像常量数组一样工作,即使实际构造不同。

答案 4 :(得分:1)

如果特定的VBA环境是Excel-VBA,则可以从Excel Application的Evaluate方法获得一个很好的语法,该方法可以缩短为方括号。

看看这个

Sub XlSerialization1()
    Dim v
    v = [{1,2;"foo",4.5}]

    Debug.Assert v(1, 1) = 1
    Debug.Assert v(1, 2) = 2
    Debug.Assert v(2, 1) = "foo"
    Debug.Assert v(2, 2) = 4.5

    '* write all cells in one line
    Sheet1.Cells(1, 1).Resize(2, 2).Value2 = v
End Sub

答案 5 :(得分:0)

数组可以声明为常量吗?否。

变通方法 - 我能想到的最简单的方法是使用delim定义一个常量,然后使用Split函数创建一个数组。

Const myConstant = "2,13,17"

Sub Test()
    i = Split(myConstant, ",")

    For j = LBound(i) To UBound(i)
        Debug.Print i(j)
    Next
End Sub

答案 6 :(得分:0)

否 - 数组不能声明为常量,但您可以使用解决方法。

您可以创建一个返回所需数组的函数

http://www.vbaexpress.com/forum/showthread.php?1233-Solved-Declare-a-Constant-Array

答案 7 :(得分:0)

如果每次都不需要新实例,则可以使用require __DIR__ . '/vendor/autoload.php'; use BotMan\BotMan\BotMan; use BotMan\BotMan\BotManFactory; use BotMan\BotMan\Drivers\DriverManager; $config = [ 'facebook' => [ 'token' => 'my_token', 'app_secret' => 'my_secret', 'verification'=>'my_verification', ] ]; DriverManager::loadDriver(\BotMan\Drivers\Facebook\FacebookDriver::class); $botman = BotManFactory::create($config); $botman->hears('hello', function (BotMan $bot) { $bot->reply('Hello yourself.'); }); $botman->listen(); 局部变量来避免创建和初始化多个对象:

Static

答案 8 :(得分:0)

这太简单了吗?

PUBLIC CONST MyArray = "1,2,3,4"

然后在模块中

Dim Arr as Variant

SET Arr = split(MyArray,",")

答案 9 :(得分:0)

不知道何时更改,但是在Excel 365中,此方法有效(或者至少不会生成编译器错误): Const table1Defs As Variant = Array(“ value 1”,42,Range(“ A1:D20”))