1

我目前在 Excel 2003 中构建了一个显示一系列数据输入表单的工具。客户要求表格上有“上一个表格”和“下一个表格”按钮。

用于在表单之间移动的代码如下

Sub NextForm(strFormName As String)
    Dim intCurPos As Integer
    Dim strNewForm As String

    'Find out which form we are currently on from a list in a range
    intCurPos = WorksheetFunction.Match(strFormName, Range("SYS.formlist"), 0)
    If intCurPos = WorksheetFunction.CountA(Range("SYS.formlist")) Then
        'We want to use the first one
        intCurPos = 0
    End If

    'Get the name of the form to open
    strNewForm = WorksheetFunction.Index(Range("SYS.formlist"), intCurPos + 1)
    'Load the form into the Userforms Collection
    Set newForm = VBA.UserForms.Add(strNewForm)
    'Show the form
    newForm.Show
End Sub

我遇到的问题是,在你这样做 25 次之后(是的,我知道)系统崩溃了。我意识到这是因为每次您到达代码上方的 newForm.Show 行时,代码都没有完成,因此位于内存中。

无模式表单将停止此问题,但随后用户可以加载其他表单并执行其他会导致重大问题的事情。

有没有人有任何建议可以帮助解决这个问题?以某种方式强制执行代码但不停止表单的模态能力?

远射,但感谢任何帮助。

4

1 回答 1

1

也许你应该改变你的方法。

我建议如下:

在主模块中,每次用户按下“下一个表单”按钮时,使用循环循环打开表单并循环。

'This sub in your main Module
sub ShowAndCyleForms
   Dim FormName As String
   Dim MyForm as Object
   Dim CloseForm as Boolean

   FormName = "frmMyForm"         

   do while CloseForm=False
      set MyForm = VBA.UserForms.Add(FormName)           
      MyForm.Show
      CloseForm=MyForm.CloseStatus
      FormName=MyForm.strNewForm
      Unload MyForm
      Set MyForm = Nothing
   loop 

end sub

在每种形式中,声明:

Public CloseStatus as Boolean
Public strNewForm as String

在每个“下一个表单”按钮中,输入如下内容:

Private Sub btnNextForm_Click()
   CloseStatus=False
   strNewForm= NextForm(Me.Name)
   Me.Hide
End Sub

将您的 sub 修改为提供下一个表单名称的函数

Sub NextForm(strFormName As String)
   Dim intCurPos As Integer

   'Find out which form we are currently on from a list in a range
   intCurPos = WorksheetFunction.Match(strFormName, Range("SYS.formlist"), 0)
   If intCurPos = WorksheetFunction.CountA(Range("SYS.formlist")) Then
       'We want to use the first one
       intCurPos = 0
   End If

   'Get the name of the form to open
   NewForm = WorksheetFunction.Index(Range("SYS.formlist"), intCurPos + 1)
   '
End Sub

您还需要修改 OK 以隐藏表单而不是卸载它并将 CloseStatus 设置为 true。

这个想法是在一个过程中控制从外部加载/卸载所有表单。

希望它足够清楚。

于 2010-12-13T14:40:32.153 回答