动态标注利用VBA阵? [英] Dynamically Dimensioning A VBA Array?

查看:184
本文介绍了动态标注利用VBA阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我无法设置一个数组的基于变量的大小?什么是解决这个问题的最好方法?

 暗淡NumberOfZombies为整数
NumberOfZombies = 20000
昏暗僵尸(NumberOfZombies)作为新的僵尸


解决方案

您可以使用动态数组当你不知道值的数量,它将包含直到运行时:

 暗淡僵尸()作为整数
REDIM僵尸(NumberOfZombies)

或者你可以做一切与一个声明,如果你创建一个数组这是当地一个过程:

 使用ReDim僵尸(NumberOfZombies)作为整数

固定大小的数组需要包含在编译时被称为元素的数量。这就是为什么不能使用一个变量来设置的大小的阵列的定义,一个变量的值是可变的,只有在运行时已知的。

如果你明明知道这个变量的值是不会改变你可以使用一个常量:

 常量NumberOfZombies = 2000

但没有办法,常量和变量投。它们有截然不同的含义。

Why am I unable to set the size of an array based on a variable? What's the best way around this?

Dim NumberOfZombies as integer
NumberOfZombies = 20000
Dim Zombies(NumberOfZombies) as New Zombie

解决方案

You can use a dynamic array when you don't know the number of values it will contain until run-time:

Dim Zombies() As Integer
ReDim Zombies(NumberOfZombies)

Or you could do everything with one statement if you're creating an array that's local to a procedure:

ReDim Zombies(NumberOfZombies) As Integer

Fixed-size arrays require the number of elements contained to be known at compile-time. This is why you can't use a variable to set the size of the array—by definition, the values of a variable are variable and only known at run-time.

You could use a constant if you knew the value of the variable was not going to change:

Const NumberOfZombies = 2000

but there's no way to cast between constants and variables. They have distinctly different meanings.

这篇关于动态标注利用VBA阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆