How call function in For each loop?

时间:2016-10-20 20:00:45

标签: vba excel-vba excel

I have a simple function to get data:

Function GetAppro(Current_Sheet As String)

   Dim myArray As Variant

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

   GetAppro = myArray
End Function

And other funtion to get other data:

Function GetTabs()

Dim Get_Tabs_generated(2) As String

Get_Tabs_generated(0) = "AA"
Get_Tabs_generated(1) = "BB"
Get_Tabs_generated(2) = "CC"

 GetTabs = Get_Tabs_generated
End Function

In my final procedure i do:

Sub GenerateDB()

  Dim Appro() As String
  Dim Tabs() As String

  'Init
  Tabs = GetTabs() 

  For Each Tabs_item In Tabs
      Appro = GetAppro(Tabs_item.Value)
      MsgBox Appro(0, 0)
  Next Tabs_item

End Sub

Excel say me compile error Object required (Error 424). I am novice with functions

1 个答案:

答案 0 :(得分:0)

<强>解决方案: 不要忘记CStr()

Sub GenerateDB()

  Dim Appro() As String
  Dim Tabs() As String

  'Init
  Tabs = GetTabs() 

  For Each Tabs_item In Tabs
      Appro = GetAppro(CStr(Tabs_item))
      MsgBox Appro(0, 0)
  Next Tabs_item

End Sub

特别感谢@ScottCraner