English 中文(简体)
强制从内存中卸载表单
原标题:Forcing the unloading forms from memory
  • 时间:2010-10-26 06:31:24
  •  标签:
  • vba
  • excel

我正在Excel中编写一个使用许多链接数据输入表单的解决方案。要在一系列表单之间移动,用户可以单击“上一个”或“下一个”按钮。当前表单将被卸载,新表单将被加载并打开。

Sub NextForm(curForm As MSForms.UserForm, strFormName As String)
    Dim intCurPos             As Integer
    Dim strNewForm            As String
    Dim newForm               As Object

    intCurPos = WorksheetFunction.Match(strFormName, Range("SYS.formlist"), 0)
    If intCurPos = WorksheetFunction.CountA(Range("SYS.formlist")) Then
        Debug.Print "No"
    Else
        Unload curForm
        strNewForm = WorksheetFunction.Index(Range("SYS.formlist"), intCurPos + 1)
        Set newForm = VBA.UserForms.Add(strNewForm)
        newForm.Show
End Sub

代码原样允许通过编辑范围“SYS.formlist”随时将新表单添加到序列中。

我注意到的一个问题是,即使卸载了当前表单,它仍然保留在VBA.Userforms集合中。我想这是因为这个代码是从那个用户表单调用的。

是否有办法强制从VBA.Userforms集合中删除该表单?发生的情况是,如果用户向前移动,然后向后移动,则该表单的两个副本会出现在内存中,并且excel会抛出关于两个模态表单打开的异常。

Cheers, Nick

最佳回答

答案(可悲的是)相当简单,并受到了军号战的启发。

该子例程将curForm变量作为MSForms.Userform对象传递,但该窗体作为自己的对象类型保存在内存中。(例如,您可以通过Set form=new formName访问表单)

因此,通过将curForm参数类型更改为Variant,它将传递实际对象,而不是对象的副本。Unload只是卸载副本,而不是实际对象。

谢谢你!

因此,更正后的代码为:

Sub NextForm(curForm As Variant, strFormName As String)
    Dim intCurPos             As Integer
    Dim strNewForm            As String
    Dim newForm               As Object

    intCurPos = WorksheetFunction.Match(strFormName, Range("SYS.formlist"), 0)
    If intCurPos = WorksheetFunction.CountA(Range("SYS.formlist")) Then
        Debug.Print "No"
    Else
        Unload curForm
        strNewForm = WorksheetFunction.Index(Range("SYS.formlist"), intCurPos + 1)
        Set newForm = VBA.UserForms.Add(strNewForm)
        newForm.Show
End Sub
问题回答

我认为从集合对象而不是从变量中卸载会真正消除它。试试这样的方法:

For i = VBA.UserForms.Count - 1 To 0 Step -1
    if VBA.UserForms(i).Name = curForm.name
        Unload VBA.UserForms(i)
    end if
Next i




相关问题
import of excel in SQL imports NULL lines

I have a stored procedure that imports differently formatted workbooks into a database table, does work on them then drops the table. Here is the populating query. SELECT IDENTITY(INT,1,1) AS ID ...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

Excel date to Unix timestamp

Does anyone know how to convert an Excel date to a correct Unix timestamp?

C# GemBox Excel Import Error

I am trying to import an excel file into a data table using GemBox and I keep getting this error: Invalid data value when extracting to DataTable at SourceRowIndex: 1, and SourceColumnIndex: 1. As ...

Importing from excel "applications" using SSIS

I am looking for any tips or resources on importing from excel into a SQL database, but specifically when the information is NOT in column and row format. I am currently doing some pre-development ...

热门标签