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

时间:2016-10-21 18:31:13

标签: excel vba excel-vba

在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

EXcel说错误9超出范围。

1 个答案:

答案 0 :(得分:3)

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

Sub GenerateDB()   
  Dim Appro() As Variant

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