我如何申报VBA数组变量

我如何申报VBA数组变量

本文介绍了我如何申报VBA数组变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要添加VAR在阵列

I need to add the var in array

Public Sub Testprog()

Dim test As Variant
Dim iCounter As Integer

If test = Empty Then
    iCounter = 0
    test(iCounter) = "test"
Else
    iCounter = UBound(test)
End If
End Sub

测试得到错误(iCounter)=测试

请提出了一些解决方案。

Please suggest some solution

推荐答案

通常情况下,你应该申报的特定类型的变量,而不是。在这个例子中,测试变量的类型应该为字符串

Generally, you should declare variables of a specific type, rather than Variant. In this example, the test variable should be of type String.

和,因为它是一个数组,你需要指出特别是当你声明变量。有声明数组变量的方法有两种:

And, because it's an array, you need to indicate that specifically when you declare the variable. There are two ways of declaring array variables:


  1. 如果你知道数组(它应该包含的元素个数),当你写的程序,您可以指定在声明括号内的数字的大小:

  1. If you know the size of the array (the number of elements that it should contain) when you write the program, you can specify that number in parentheses in the declaration:

Dim test(1) As String   'declares an array with 2 elements that holds strings

这类型的阵列被称为一个的静态的阵列,它的大小是固定的,还是静态的。

This type of array is referred to as a static array, as its size is fixed, or static.

如果你不知道数组的大小,当你写的应用程序,你可以使用的动态的数组。动态数组是指其尺寸没有在声明(点心语句)指定,而是使用的程序的执行过程中后确定的使用ReDim 语句。例如:

If you do not know the size of the array when you write the application, you can use a dynamic array. A dynamic array is one whose size is not specified in the declaration (Dim statement), but rather is determined later during the execution of the program using the ReDim statement. For example:

Dim test() As String
Dim arraySize As Integer

' Code to do other things, like calculate the size required for the array
' ...
arraySize = 5

ReDim test(arraySize)  'size the array to the value of the arraySize variable


这篇关于我如何申报VBA数组变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 17:38