【问题标题】:VBA How to avoid recusrion when using many integersVBA如何在使用许多整数时避免递归
【发布时间】:2018-05-30 07:04:10
【问题描述】:

我试图弄清楚如何避免在我运行代码时出现多个 msgbox:

Public Function IsItGood(aWord As Variant) As Boolean
Dim s As String
s = "|"
tmp = s & aWord & s
patern = ""

For i = 1 To 100
    patern = patern & s & i
Next i
For i = 1 To 10
    patern = patern & s & "C" & i
Next i
patern = patern & s & "merge|complete framed|width|border left|border right" & s

If InStr(1, patern, tmp) > 0 Then
    IsItGood = True
Else
    IsItGood = False
End If

End Function

上面是下面worksheet_change中使用的函数:

Sub Worksheet_Change(ByVal Target As Range)
Dim BigS As String
Dim arr As Variant
Dim a As Variant
If Intersect(Range("G3:G19"), Target) Is Nothing Then Exit Sub
arr = Split(Target, " ")

    If IsItGood(a) Then
    MsgBox (" In row" + Target.Address(0, 0)) & vbCrLf & a & vbCrLf + "are ok"
    Else
        MsgBox Target.Address(0, 0) & vbCrLf & a & vbCrLf & "has bad stuff"
        Application.Undo
    End If

End Sub

第一个“for”循环 100 个整数,第二个循环从 C1 到 C10,并且每个拆分的字符串都重复 msgbox。有没有办法防止多个 msgbox 一次只出现一个 msgbox。由于递归,还会出现“堆栈空间不足”错误。

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    在开头设置:Application.EnableEvents = False,最后设置为True。发生递归,因为你在工作簿的更改事件上调用宏,它也产生了这个事件,因此方法是在调用自己,因此是递归。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-13
      • 2021-09-20
      • 1970-01-01
      • 1970-01-01
      • 2013-11-10
      • 1970-01-01
      相关资源
      最近更新 更多