【发布时间】:2016-02-04 21:07:42
【问题描述】:
我在名为 MainForm 的用户窗体中声明了一个公共 Variant 变量
Public increaseArray As Variant
Public countryArray As Variant
然后在 sub on 按钮中单击 MainForm:
Sub testButton_Click()
Dim country As Variant
Set countryArray = Module1.callSomeFunctionThatReturnsVariant(1)
Set increaseArray = Module1.callSomeFunctionThatReturnsVariant(2)
For Each country In countryArray
Call Module1.createPage(country)
Next country
End Sub
在模块 1 中我有:
Function callSomeFunctionThatReturnsVariant(ByVal testInt As Integer) As Variant
.... do something when testInt = 1
.... do something when testInt = 2
callSomeFunctionThatReturnsVariant = someVariant
End Function
Public Sub createPage(ByVal country As String)
Dim testInt As Integer
... do something
testInt=insertSection(country, MainForm.increaseArray)
End Sub
Function insertSection(ByVal country As String, arr as Variant) As Integer
Dim arrCountry As Variant
For Each arrCountry In arr
If country = "France" Then
...do something
insertSection = 1
Exit Function
End If
Next arrCountry
insertSection = 2
End Function
在将 MainForm.increaseArray 传递给 insertSection() 函数时,出现 ByRef 参数类型不匹配错误。我试过使用Function insertSection(ByVal country As String, ByVal arr as Variant) As Integer,但我得到了同样的错误。
如果我尝试在 createPage sub Dim testArray As Variant 中定义 Variant 变量并从它的 getter 函数 Set testArray = MainForm.getterForIncreaseArray 中获取 increaseArray,我会收到类型不匹配错误...
如果我将 getter 函数直接传递给 insertSection 函数的调用者,我会得到 ByRef 参数类型不匹配...
请帮忙:)
【问题讨论】:
-
我不认为用户表单的范围不在它之外,即使它是公开的。
-
@findwindow,错了,你肯定可以,只是你需要声明为公共,然后用用户窗体的名称调用它。至于问题,将参数传递为 byref (实际上是写 byref ,不要偷懒)
-
@PatrickLepelletier 您有文档或代码来证明这一点吗?我不得不破解自己的代码,但如果范围超出范围就好了。
标签: excel vba parameters arguments variant