2016-02-04 78 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中的所有代碼放入新的類模塊中。在你的表單中實例化一個實例,它應該可以正常工作。