[英]Can I run a Sub based on whether another Sub has run?
I have a userform with two text boxes to search a worksheet based on the value in either text box.我有一个带有两个文本框的用户表单,用于根据任一文本框中的值搜索工作表。 The form populates other boxes based on the active cell.
该表单根据活动单元格填充其他框。
How can each sub check if the other has executed?每个子如何检查另一个是否已执行?
Pseudocode:伪代码:
Sub StatusTextBox_AfterUpdate()
'Check if TransitTextBox_AfterUpdate() has run
If yes Then End Sub
If No Then Carry on with the rest of StatusTextBox_AfterUpdate()
This would change the value of the TransitTextBox
to pull from the worksheet, and I want to prevent the TransitTextBox_AfterUpdate()
from running.这将更改
TransitTextBox
的值以从工作表中提取,并且我想阻止TransitTextBox_AfterUpdate()
运行。
As soon as the TransitTextBox
is populated the AfterUpdate
event runs and the two conflict.一旦
TransitTextBox
被填充, AfterUpdate
事件就会运行并且两者发生冲突。 Is there a way of preventing this?有没有办法防止这种情况?
SOLVED!解决了!
My Workbook_Open code wouldn't work if I had a cocktail of other excel files open.如果我打开了其他 excel 文件的鸡尾酒,我的 Workbook_Open 代码将无法工作。 It wasn't connected to the conditional formatting you read about around this issue.
它与您阅读的有关此问题的条件格式无关。 I'm really not sure how this got around it because I tried the .activate and also the EVENT time delay, but neither of those would work when the glitch appeared.
我真的不确定这是如何解决的,因为我尝试了 .activate 和 EVENT 时间延迟,但是当出现故障时,这两个都不起作用。 In this example the MSG box never opens for me to confirm the re-run was successful, however the issue has been resolved with the addition of the following code.
在此示例中,MSG 框从未打开以确认重新运行是否成功,但是通过添加以下代码解决了该问题。 Just substitute your code where I put the 'Comments sections
只需将您的代码替换为我放置“评论”部分的位置
/CODE Public ImDone As Boolean /CODE Public ImDone As Boolean
Public Sub Workbook_Open()公共子工作簿_Open()
ImDone = True 'Run Code End Sub ImDone = True '运行代码结束子
Public Sub BoolCheck() If ImDone = True Then MsgBox "code has run" Else 're-run code End Sub /Code Public Sub BoolCheck() If ImDone = True Then MsgBox "code has run" Else '重新运行代码 End Sub /Code
Use a Global variable to allow the two subs to communicate:使用全局变量来允许两个 sub 进行通信:
Public ImDone As Boolean
Sub ShouldRunFirst()
ImDone = True
End Sub
Sub ShouldRunSecond()
If ImDone Then
MsgBox "first macro has been run"
Else
MsgBox "first macro has not run yet"
End If
End Sub
EDIT#1:编辑#1:
We need to make the communication variable visible across the board:我们需要使通信变量全面可见:
It should then be "visible" to subs, and events.然后它应该对潜艇和事件“可见”。 See some of the answers Here
在这里查看一些答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.