2014-02-06 27 views
-1

我正在創建一個項目,其中有3個窗體form1-> form2-> form3-> form1-> form2-> form3 ...的循環,並且循環保持到應用程序退出。循環是通過一個按鈕。在form3中,我使用了interop.excel,並將數據從工作表複製到數組中,這是通過從列表框中選擇excel文件的名稱完成的。如何保留循環中的表單的值?

現在的問題是,每次我從form3到form1 form3都失去了焦點和form2總是創建一個form3的新實例。所以即使我之前的form3打開,它也會打開新的form3。

我看過並嘗試過解決方案How can I loop through all the open instances of a particular form?Application.OpenForms.Count = 0 always 但它沒有幫助。

有沒有一種方式,form2可以檢查是否有任何form3的實例,然後做出決定,使form3的新實例或焦點到以前的form3。

請幫幫我。先謝謝你。

回答

0

This Thread

Function IsUserFormLoaded(ByVal UFName As String) As Boolean 
Dim UForm As Object 

IsUserFormLoaded = False 
For Each UForm In VBA.UserForms 
    If UForm.Name = UFName Then 
     IsUserFormLoaded = True 
     Exit For 
    End If 
Next 
End Function 'IsUserFormLoaded 

使您的需求,你可以做

If IsUserFormLoaded(UserForm3) Then 
    'Code if it is open here 
Else 
    'Code if it is NOT open here 
End If