完成后调用sub

时间:2017-07-27 09:14:25

标签: vba excel-vba excel

我有一个用户表单循环一个有2个设置的范围; manualautomatic

如果我的表单上有一个选项按钮设置为manual,然后单击next命令按钮,我会检查范围中的下一个单元格,相应地更改表单的内容,然后等待按下按钮。

但是,如果我发现选项按钮设置为automatic,那么我不必完成代码并等待按下下一个按钮,我必须以编程方式按下一个按钮。这意味着每个调用下一​​个代码的前一个subs在调用堆栈中慢慢建立起来,我担心如果我在大范围内循环,这会产生一些内存影响。 在代码中:

Private Sub nextItem()
Dim willShow As Boolean
returnResults 'return details from the form to the sheet
clearMemory 'clear out previous items on form
itemNo = itemNo + 1 'iterate over the range
SetupParts 'place new items on form
'do what next
Select Case displaySetting 'this variable holds the result from my option button in a custom enum "dispMode"
    Case dispMode.Manual 'always show form
        willShow = True
    Case dispMode.SemiAutomatic 'show form based on condition
        willShow = (Data.Count = 0) 'if SetupParts returns no data, display form, otherwise keep hidden
    Case dispMode.Automatic 'don't show form
        willShow = False
End Select 'there are actually a few more options here, but I've simplified
If willShow = False Then
    If Me.Visible = True Then 'if needs to hide, and currently visible, then hide the form
        Me.Hide
        nextItem 'this is the problem, I call this code again, so the call stack grows
    Else
     'form is already hidden, do nothing
    End If
ElseIf Me.Visible = False Then 'willShow must be True
    Me.Show 'then wait for button click
End If
End Sub

Private Sub commandBtnNext_Click()
    nextItem
End Sub

要避免此问题,是否有任何方法可以在nextItem的上一次调用运行后立即运行nextItem;即。告诉一个子在这个完成后立即运行(不引入时间延迟)。或者这可能不是问题;如果是的话,请解释原因。

更新

还有一个SemiAutomatic检查,根据userform的内容查看要使用的模式。这在递归调用时很好,但我看不出如何将它合并到循环方法中。

0 个答案:

没有答案