VBA -公共数组错误-超出范围的下标

时间:2022-03-15 16:32:51

I want to declare a public array, create it and then use it in another sub. This is exapmle of what I wrote:

我想声明一个公共数组,创建它,然后在另一个子数组中使用它。

Public array1() As String

Sub Create_Array()

Dim array1(1 To 4) As String

array1(1) = "1"
array1(2) = "2"
array1(3) = "A"
array1(4) = "B"

End Sub

Sub Show_Some_Index()

Dim a As String
a = array1(1)
MsgBox (a)

End Sub

I get Error 9: "Subscript out of range". Couldn't find an answer, what am I doing wrong?

我得到错误9:“下标超出范围”。找不到答案,我做错了什么?

2 个解决方案

#1


2  

Variable array1() in Sub Create_Array is scoped to that procedure - basically it's a local variable that's only ever accessible within that procedure, and it happens to have the same name as another public field declared elsewhere, so what's happening is that Show_Some_Index is working off an array that hasn't been initialized yet.

变量array1()子Create_Array范围仅仅是那过程,基本上它是一个局部变量,只能在访问过程中,它发生在具有相同的名称作为另一个公共字段声明在其他地方,所以发生了什么是Show_Some_Index工作尚未初始化的数组。

Dim is used for declaring variables. If you mean to re-dimension an array that's in-scope, use the ReDim keyword.

Dim用于声明变量。如果您想要重新度量一个范围内的数组,请使用ReDim关键字。


A better approach would be to use a function that returns the array, instead of relying on global variables.

更好的方法是使用返回数组的函数,而不是依赖全局变量。

#2


2  

I want to declare a public array, create it and then use it in another sub.

我想声明一个公共数组,创建它,然后在另一个子中使用它。

In that case remove the Dim Statement from your code. Further to what Mat explained, here is another way to make your code work

在这种情况下,从代码中删除Dim语句。对于Mat解释的内容,下面是另一种使代码工作的方法。

WAY 1

方法1

Public array1(1 To 4) As String

Sub Create_Array()
    array1(1) = "1"
    array1(2) = "2"
    array1(3) = "A"
    array1(4) = "B"

    Show_Some_Index
End Sub

Sub Show_Some_Index()
    Dim a As String
    a = array1(1)
    MsgBox (a)
End Sub

WAY 2

方法2

Public array1(1 To 4) As String

Sub Create_Array()
    array1(1) = "1"
    array1(2) = "2"
    array1(3) = "A"
    array1(4) = "B"
End Sub

Sub Show_Some_Index()
    Create_Array

    Dim a As String
    a = array1(1)
    MsgBox (a)
End Sub

Once you initialize it, you should be able to use it in other procedures.

一旦初始化它,您应该能够在其他过程中使用它。

#1


2  

Variable array1() in Sub Create_Array is scoped to that procedure - basically it's a local variable that's only ever accessible within that procedure, and it happens to have the same name as another public field declared elsewhere, so what's happening is that Show_Some_Index is working off an array that hasn't been initialized yet.

变量array1()子Create_Array范围仅仅是那过程,基本上它是一个局部变量,只能在访问过程中,它发生在具有相同的名称作为另一个公共字段声明在其他地方,所以发生了什么是Show_Some_Index工作尚未初始化的数组。

Dim is used for declaring variables. If you mean to re-dimension an array that's in-scope, use the ReDim keyword.

Dim用于声明变量。如果您想要重新度量一个范围内的数组,请使用ReDim关键字。


A better approach would be to use a function that returns the array, instead of relying on global variables.

更好的方法是使用返回数组的函数,而不是依赖全局变量。

#2


2  

I want to declare a public array, create it and then use it in another sub.

我想声明一个公共数组,创建它,然后在另一个子中使用它。

In that case remove the Dim Statement from your code. Further to what Mat explained, here is another way to make your code work

在这种情况下,从代码中删除Dim语句。对于Mat解释的内容,下面是另一种使代码工作的方法。

WAY 1

方法1

Public array1(1 To 4) As String

Sub Create_Array()
    array1(1) = "1"
    array1(2) = "2"
    array1(3) = "A"
    array1(4) = "B"

    Show_Some_Index
End Sub

Sub Show_Some_Index()
    Dim a As String
    a = array1(1)
    MsgBox (a)
End Sub

WAY 2

方法2

Public array1(1 To 4) As String

Sub Create_Array()
    array1(1) = "1"
    array1(2) = "2"
    array1(3) = "A"
    array1(4) = "B"
End Sub

Sub Show_Some_Index()
    Create_Array

    Dim a As String
    a = array1(1)
    MsgBox (a)
End Sub

Once you initialize it, you should be able to use it in other procedures.

一旦初始化它,您应该能够在其他过程中使用它。