简体   繁体   English

Excel VBA ActiveX组合框更改事件无限循环

[英]Excel VBA ActiveX combobox change event infinite loop

Let me preface by saying I am a self taught novice at VBA or coding for that matter. 首先,请允许我说我是VBA的自学新手或对此进行编码。

I have a combobox "cmbStyle" not in a userform but diretly on a worksheet named "Cost". 我有一个组合框“ cmbStyle”,它不在用户窗体中,而是直接放在名为“成本”的工作表上。 I need to run code on the change event of "cmbStyle". 我需要在“ cmbStyle”的更改事件上运行代码。 The problem I have is that when the user changes the combobox the event fires and the code runs but at the end of the code the combobox event fires again and so on and so on. 我遇到的问题是,当用户更改组合框时,将触发事件并运行代码,但是在代码结尾时,组合框事件将再次触发,依此类推。

I know that Application.EnableEvents = False will have not effect on ActiveX controls so this is not a solution. 我知道Application.EnableEvents = False不会对ActiveX控件产生影响,因此这不是解决方案。

I have found descriptions on how to use a boolean variable to stop the looping in the change event for listboxes but I can't seem to get it to work in my instance. 我已经找到了有关如何使用布尔变量停止列表框的change事件中循环的描述,但我似乎无法使其在我的实例中正常工作。

My code will end the subroutine after the Cange Event fires the second time. 我的代码将在Cange事件第二次触发后结束该子例程。 However, the next time the user selects another value from the Combobox the CodeDoneRun variable is still TRUE so the subroutines won't run again when I need it to. 但是,下一次用户从组合框选择另一个值时,CodeDoneRun变量仍为TRUE,因此在需要时不会再次运行子例程。

I feel I am missing something very basic here.... 我觉得这里缺少一些非常基本的东西。

My code is as follows: 我的代码如下:

Public CodeDoneRun as Boolean

Private Sub cmbStyle_Change()
    If CodeDoneRun = True Then Exit Sub

    CodeDoneRun = True

    Call other Subroutines

End Sub

Enclose your function call in the if statement and let the code run through. 将函数调用包含在if语句中,并让代码运行通过。

Public CodeDoneRun as Boolean

Private Sub cmbStyle_Change()
    If Not CodeDoneRun Then
         Call other Subroutines
    End If

    CodeDoneRun = Not CodeDoneRun

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM