如何从函数中读取数组值?

时间:2016-10-20 18:27:50

标签: excel vba excel-vba

我有一个简单的函数来获取和保存数组中的值:

Function GetDataOwner()

 Dim DataOwner(2) As String

 DataOwner(0) = Sheets(1).Range("H21").Value
 DataOwner(1) = Sheets(1).Range("I21").Value
 DataOwner(2) = Sheets(1).Range("J21").Value


End Function

在其他功能中,我想读取值:

Sub GenerateDB()

  Dim DataOwner() As String
  DataOwner = GetDataOwner()

  MsgBox DataOwner(1)
End Sub

但Excel说错误13类型不匹配。我是VBA功能的新手

1 个答案:

答案 0 :(得分:1)

Soluce:

Function GetDataOwner()

 Dim DataOwner(2) As String

 DataOwner(0) = Sheets(1).Range("H21").Value
 DataOwner(1) = Sheets(1).Range("I21").Value
 DataOwner(2) = Sheets(1).Range("J21").Value

 GetDataOwner = DataOwner
End Function


Sub GenerateDB()

  Dim DataOwner() As String
  DataOwner = GetDataOwner()

  MsgBox DataOwner(1)
End Sub

Thanks to @Comintern