2016-02-04 54 views
1

我有一个名为MainForm的在VBA Excel中传球变的ByRef 2007

Public increaseArray As Variant 
Public countryArray As Variant 

然后用户窗体声明的公共Variant变量在按钮点击的的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到当得到ByRef参数类型不匹配错误功能。我试过使用Function insertSection(ByVal country As String, ByVal arr as Variant) As Integer但我得到同样的错误。

如果我尝试从它的getter函数Set testArray = MainForm.getterForIncreaseArray定义createPage子Dim testArray As Variant Variant变量,并得到increaseArray我得到类型不匹配的错误... 如果我直接传递吸气功能insertSection功能的调用者,我得到ByRef参数类型不匹配...

请帮忙:)

+1

我不认为一个窗体跌倒的范围即使它是公开的。 – findwindow

+0

@findwindow,错了,你明确地可以,只需要声明为public,然后用userform的名称来调用它。至于这个问题,通过参数作为byref(实际上写byref,不要懒惰) –

+0

@PatrickLepelletier你有文档或代码来证明这一点?我不得不破解我自己的代码,但如果范围超出范围会很好。 – findwindow

回答

1

这个简单的代码工作正常。

不允许在用户表单中声明公用数组(因此使用variant作为伪装是个好主意)。

但是,函数不希望接受将它作为参数传递为合法数组,所以我使用了一个临时的'合法'数组。

UserForm1上:

Option Explicit 

Public a As Variant 'i would usually declare it like this : Public a() as variant, but public arrays not allowed in userforms (throws error) 
'Private a() as variant , would not throw error (inside userform) 

Private Sub UserForm_Initialize() 
Dim i& 
ReDim a(1 To 2) 'absolutely needed, it shows a is actually an array type 
a(1) = 1 
a(2) = 2 
End Sub 

Private Sub UserForm_Terminate() 
Erase a 
End Sub 

一个模块中: 选项调用TEST明确

Sub test() 
Load UserForm1 
Dim b& 
Call get_value(1, UserForm1.a, b) 
Unload UserForm1 
MsgBox b 
End Sub 

Sub get_value(ByVal i&, ByRef arr As Variant, ByRef answer As Long) ' function won't let it through, i used a sub with aditionnal variable as Byref. 
answer = arr(i) 
End Sub 

启动它。

注意:我没有成功地在一个函数中传递参数,所以它在SUB中添加了一个名为Answer的参数,这个参数是Byref。注意2:我回头看看我的旧代码,似乎你可以在函数中传递一个byref数组(伪装成变体),但也许是因为这个声明不是用()或其他任何东西声明的,它不会不想通过函数来​​工作。

注3:thurther挖进去后,我发现了一个解决方案使用功能,因为我认为,数组,声明是麻烦制造者:

'in a module (use the same userform as before) 
Sub test() 
Load UserForm1 
Dim b& 
Dim i& 'counter 
Dim Temp_Array() As Long 'as variant works too, but i filled it with numbers so as long is ok too 
ReDim Temp_Array(LBound(UserForm1.a) To UBound(UserForm1.a)) 
'Temp_Array = UserForm1.a 'damn, i first thought this would work, in the same way you can fill a listbox in one simple line (wich would be a 3rd solution passing an array from the userform to a module) 
For i = LBound(UserForm1.a) To UBound(UserForm1.a) 
    Temp_Array(i) = UserForm1.a(i) 
Next i 
b = get_value(1, Temp_Array) 
Erase Temp_Array 
Unload UserForm1 
MsgBox b 
End Sub 

Function get_value(ByVal i&, ByRef arr As Variant) As Long 
get_value = arr(i) 
End Function 
+0

'我没有成功通过函数传递参数'OP使用函数...所以你也必须破解它。 – findwindow

+1

当你不能推,拉 –

+0

离开工作,但不得不记得回来看看。 – findwindow

0

根据findwindow的评论。你不能在表单中使用模块1中的东西,因为它超出了范围。相反,请尝试将模块1中的所有代码放入新的类模块中。在你的表单中实例化一个实例,它应该可以正常工作。