Vba 如何读取二维数组中的值?

Vba 如何读取二维数组中的值?,vba,excel,Vba,Excel,在VBA中,我有一个函数来获取数据并将其保存在数组中: Function GetAppro(Current_Sheet As String) Dim myArray As Variant myArray = Worksheets(Current_Sheet).Range("A3:C6") GetAppro = myArray End Function Sub GenerateDB() Dim Appro() As Variant Appro = GetA

在VBA中,我有一个函数来获取数据并将其保存在数组中:

Function GetAppro(Current_Sheet As String)

   Dim myArray As Variant

   myArray = Worksheets(Current_Sheet).Range("A3:C6")

   GetAppro = myArray
End Function
Sub GenerateDB()   
  Dim Appro() As Variant

  Appro = GetAppro("Sheet1")
  MsgBox Appro(0, 0) 'Error come from here
End Sub
在其他函数中,我希望读取数组中的值:

Function GetAppro(Current_Sheet As String)

   Dim myArray As Variant

   myArray = Worksheets(Current_Sheet).Range("A3:C6")

   GetAppro = myArray
End Function
Sub GenerateDB()   
  Dim Appro() As Variant

  Appro = GetAppro("Sheet1")
  MsgBox Appro(0, 0) 'Error come from here
End Sub

EXcel告诉我错误9超出范围

在本例中,数组索引从1开始。使用:

Sub GenerateDB()   
  Dim Appro() As Variant

  Appro = GetAppro("Sheet1")
  MsgBox Appro(1, 1) 'Error come from here
End Sub

在此实例中,数组索引从1开始。使用:

Sub GenerateDB()   
  Dim Appro() As Variant

  Appro = GetAppro("Sheet1")
  MsgBox Appro(1, 1) 'Error come from here
End Sub

当将范围加载到范围中时,它是以1为基数而不是以0为基数,因此将其更改为
MsgBox Appro(1,1)
以获得左上角。当将范围加载到范围中时,它是以1为基数而不是以0为基数,因此将其更改为
MsgBox Appro(1,1)
进入左上角。向1分钟前进来的Scott Craner大声喊出同样的答案。是的,但我很懒,而且采取了简单的方法。只是添加一些建议,以便将来能够解决此类错误。我确认索引结构的方法是添加一个断点,例如在MsgBox上,并在变量上放置一个手表(在本例中为
Appro
)。然后我可以看到所有的子项,第一个是
Appro(1,1)
。没有0个索引项。如果你觉得我的答案很有用,不像斯科特,我会迎合虚假的互联网点,所以如果你能把这个标记为正确的答案,我将不胜感激。感谢斯科特·克兰纳,他在1分钟前得到了同样的答案。是的,但我很懒,而且采取了简单的方法。只是想补充一些建议,以便将来能够解决此类错误。我确认索引结构的方法是添加一个断点,例如在MsgBox上,并在变量上放置一个手表(在本例中为
Appro
)。然后我可以看到所有的子项,第一个是
Appro(1,1)
。没有0个索引项。如果你觉得我的答案很有用,不像斯科特,我会迎合虚假的互联网点,所以如果你能把这个标记为正确的答案,我将不胜感激。谢谢