2013-06-05 41 views
4

我的模塊代碼調用用戶窗體:VBA:用戶窗體初始化方法沒有被擊中時,用戶窗體初始化

PreInfo.Show 

我的用戶窗體代碼:

Public Sub PreInfo_Initialize() 
Dim Invoice, Name, Model, Crank, MyValue1, StrokeL As Variant 
'Dim ListBox1 As ListBox 
Dim c As Range 
Dim oneControl As Object 

'Empty Text Boxes and Set Focus 
For Each oneControl In PreInfo.Controls 
Select Case TypeName(oneControl) 
Case "TextBox" 
    oneControl.Text = vbNullString 
'Case "ListBox" 
    'oneControl.AddItem "Test" 
End Select 
Next oneControl 

With lbTest 
    .AddItem Item:="2 Cylinders" '3 different syntax used as test to isolate issue 
    .AddItem "3 Cylinders" 
    .AddItem ("5 Cylinders") 
End With 

Invoice.TextBox.SetFocus 'Activate? 

End Sub 

我的模塊代碼不會觸發我的用戶窗體初始化子,因此該子文件中沒有任何內容。我無法弄清楚爲什麼會發生這種情況。我將不勝感激任何幫助!

運行此代碼時,用戶窗體彈出,但沒有一個列表框項目添加

+0

您以前使用過'UserForm.hide'嗎? – enderland

+0

當用戶單擊用戶窗體上的「繼續」按鈕時,我使用userform.hide,這會關閉用戶窗體並將用戶窗體輸入打印到工作表中。 –

回答

0

我已經想通了。長話短說,我需要的模塊下面的代碼:

Userform.Userform_Activate 'THIS IS THE NEW CODE 
Userform.Show 'existing code, unchanged 

這標誌着用戶窗體激活之前,它是開放的(叫「初始化」,然後顯示用戶窗體爲用戶改變)。

Userform.Show應該提示這個激活子運行,但是我的並不是出於任何原因。這解決了這個問題,直到我確定爲什麼Userform.Userform_Activate沒有像應該被調用一樣。

0

我用userform.hide當用戶點擊「繼續」按鈕,用戶窗體,其中關閉用戶窗體和將用戶表單輸入打印到工作表中

出現的情況是您的用戶表單永遠不會從內存中卸載。 Hide只能從視圖中刪除它。

這意味着它僅在您第一次在該Excel實例中運行用戶窗體時進行初始化。

您可以通過使用

unload me 

End 

,而不是UserForm.Hide取決於你的其他代碼防止這種情況。您也可以使用UserForm_Activate方法而不是UserForm_Initialize方法。


填充ListBox,使用:

lbTest.AddItem "3 Cylinders" 

等的With語句外。

+0

更改語法爲: 卸載我 用戶表單出現,但ListBox仍未填充。 –

+0

@AlexBarrie我添加了解決這個問題的代碼。如果上面的代碼不起作用,那就在那裏放一個斷點,看看你的代碼是否進入了這個方法。 – enderland

+0

更改了代碼(相同的結果)。在代碼中添加了斷點(程序繼續超過該點而不觸發斷點)。就好像userform.show工作,但不觸發userform_activate子。 –

2

我有同樣的問題,並找到了一個非常簡單的解決方案。

而是採用

Public Sub PreInfo_Initialize()  

使用

Public Sub UserForm_Initialize()  
0

你必須保持語法UserForm_Initialize在你的情況,()做到這一點

乾杯

+0

我覺得這對我來說是個問題,我使用TestForm_Initialize()來創建一個名爲TestForm的表單,並將其更改爲UserForm_Initialize(),它的工作方式與預期的一樣。 –

3

UserForm_Initialize事件中,是在模塊中調用這樣的線來觸發:

Load Userform1 

爲了再次觸發它,您需要卸載userform(而不是簡單地隱藏它)。這是可以做到無論是在模塊內的負載調用後:

Unload Userform1 

或在用戶窗體的代碼的任何地方:

Unload Me 

注意,事件初始化將對QueryClose將被觸發卸載調用以及(QueryClose也會在右上角的關閉按鈕被按下時觸發),所以我真的建議你不要使用初始化。相反,在調用之後,在同一模塊中添加初始化代碼(或者如果將從多個位置調用它,則創建一個單獨的子代碼)。

Sub LoadThatUserform 
    Load Preinfo 
    'All textboxes are loaded with their value set to vbnullstring, _ 
     unless you specified otherwise in the Properties box. 
    With ThatUserform.lbTest 
    'Answering this test 
     .AddItem Item:="2 Cylinders" 'Here you used the parameter name. _ 
       It's entirely optional, which is why the one below _ 
       also works. It's necessary, however, if you wanna skip _ 
       an optional parameter on a procedure call. 
     .AddItem "3 Cylinders" 
     .AddItem ("5 Cylinders") 'This will theoretically create a _ 
       run-time error: a procedure call either outside of a Call _ 
       statement or not setting a value to a variable or property _ 
       doesn't require parentheses. 
    End With 
    'After loading, show the form 
    Preinfo.Show 
    'Showing, if not as modeless, stops code execution for the user _ 
      to make changes to the form. Once he presses a button _ 
      or whatever, and the form is hidden, code will resume. _ 
      After you grab every form data you need, just call Unload. 
    Unload Preinfo 
End Sub 

最後但並非最不重要的,如果你(在後臺,讓我們的代碼運行時顯示)運行無模式形式,你需要使用激活事件的代碼運行。事件序列是:

  • Userform_Initialize,後加載用戶窗體
  • Userform_Activate後Userform.Show
  • Userform_QueryClose,後卸載用戶窗體,按壓閉合「 X「或通過關閉Excel /任務管理器終止
  • Userform_Terminate,當它真的要結束時(雖然我不知道如何使用它)。
相關問題