我目前在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行都没有完成,因此就在内存中。
无模式表单会阻止此问题,但用户可以加载其他表单并执行其他可能导致重大问题的事项。
有没有人有任何建议可以帮助解决这个问题?以某种方式强制执行代码而不是停止表单的模态能力?
远射,但感谢任何帮助。
答案 0 :(得分: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
您还需要修改您的O.K.只是隐藏表单而不是卸载它并将CloseStatus设置为true。
我们的想法是在一个程序中控制从外部加载/卸载所有表单。
希望它足够清楚。