开发者

Forcing the unloading forms from memory

开发者 https://www.devze.com 2023-01-21 19:11 出处:网络
I am writing a solution in Excel that uses a number of linked data entry forms. To move between he sequence of forms, the user can click a \"Previous\" or \"Next button. The current form is unloaded a

I am writing a solution in Excel that uses a number of linked data entry forms. To move between he sequence of forms, the user can click a "Previous" or "Next button. The current form is unloaded and the new one loaded and opened.

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

The code as is allows new forms to be added into the sequence at any time through the editing of the range "SYS.formlist".

One problem I have noticed is that even after the current form is unloaded, it still re开发者_如何学Cmains in the VBA.Userforms collection. I would presume this is because this code has been called from that userform.

Is there a way to force the removal of that form from the VBA.Userforms collection? What is occuring is that if the user moves forward and then back, two copies of the form appear in memory and and excel throws exceptions about two modal forms being open.

Cheers, Nick


The answer was (sadly) quite simple and inspired by bugtussle's answer.

The subroutine was passing the curForm variable as an MSForms.Userform object, but the form is held in memory as its own object type. (As an example, you can access a form through Set form = new formName)

So by changing the curForm paramater type to Variant, it will pass the actual object through rather than a copy of the object. Unload was only unloading the copy, not the actual object.

Thanks bugtussle!

So, corrected code is:

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


I'm thinking that unloading from the collection object instead of the variable will really get rid of it. Try something like this:

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号