我目前有3个文本框供用户输入,用户可以输入其中一个文本框,其余2个将填充。但是,当第一个子程序运行时,第二个或第三个子程序不应运行,第二个运行时第一个和第三个子程序不应运行,等等。我已经缩短了代码以输入固定值,因为实际代码需要打开多个其他文件,很长。
使用当前代码,只有当您感觉到文本框的最后一个(又名第3个子控件)时,它才会起作用,并且不会弹出错误消息。
Private Sub tbA_AfterUpdate()
'If user enters text, run this code
If tbA Is Nothing Then
tbA = ""
Elseif tbA.TextLength > 0 Then
tbB = "NA"
tbC = "NA"
End If
End Sub
'the problem starts here if user inputs into tbA, this code still runs
'prompting an error message
Private Sub tbB_AfterUpdate()
'If user enters text, run this code
If tbB Is Nothing Then
tbB = ""
Elseif tbB.TextLength > 0 Then
tbA = "NA"
tbC = "NA"
End If
End Sub
Private Sub tbC_AfterUpdate()
'If user enters text, run this code
If tbC Is Nothing Then
tbC = ""
Elseif tbC.TextLength > 0 Then
tbA = "NA"
tbB = "NA"
End If
End Sub
任何帮助将不胜感激!
答案 0 :(得分:2)
您需要使用Public
Boolean
来帮助管理例程的执行,或者另一种方法是将值存储在单元格中并将其用作控制器的引用。以下代码未经测试,但应为您提供原理:
将布尔值声明为全局变量。为此,您可以在项目中普通模块的顶部插入以下行。
Public bFlag As Boolean
然后,以下内容进入您的相关模块。请注意,使用布尔值可以简单地打开和关闭。
Private Sub tbA_AfterUpdate()
If bFlag = False Then
'If user enters text, run this code
If tbA Is Nothing Then
tbA = ""
ElseIf tbA.TextLength > 0 Then
tbB = "NA"
tbC = "NA"
End If
'set bool to true
bFlag = True
End If
End Sub
'the problem starts here if user inputs into tbA, this code still runs
'prompting an error message
Private Sub tbB_AfterUpdate()
If bFlag = False Then
'If user enters text, run this code
If tbB Is Nothing Then
tbB = ""
ElseIf tbB.TextLength > 0 Then
tbA = "NA"
tbC = "NA"
End If
'set bool to true
bFlag = True
End If
End Sub
Private Sub tbC_AfterUpdate()
If bFlag = False Then
'If user enters text, run this code
If tbC Is Nothing Then
tbC = ""
ElseIf tbC.TextLength > 0 Then
tbA = "NA"
tbB = "NA"
End If
'set bool to true
bFlag = True
End If
End Sub
答案 1 :(得分:0)
使用类似这样的东西,只需使用标志-输入boolean来检查您的值是手动更新还是通过代码更新
Private Sub tbA_AfterUpdate()
'If user enters text, run this code
If tbA Is Nothing Then
tbA = ""
Elseif tbA.TextLength > 0 Then
tbB = "NA"
tbC = "NA"
tbBupdatedByCode = True
tbCupdatedByCode = True
End If
End Sub
'the problem starts here if user inputs into tbA, this code still runs
'prompting an error message
Private Sub tbB_AfterUpdate()
If tbBupdatedByCode = False
'If user enters text, run this code
If tbB Is Nothing Then
tbB = ""
Elseif tbB.TextLength > 0 Then
tbA = "NA"
tbC = "NA"
End If
End Sub