首先,这是我关于堆栈溢出的第一篇文章,所以我希望我遵循正确的程序。我浏览过这个网站和其他网站上的几十个帖子,但我似乎无法推断类似案例的解决方案。我也试过使用调试线,但我无法查明问题,可能是因为我是VBA的新手。简而言之,我希望你能提供帮助:
Sheet1上的一个命令按钮引发了Yes / No / Cancel msgbox,我想要一个机制来记住后面的UserForms和Modules中的这个选择,所以我将boolNieuweOpdrachtgever声明为Public变量,但是,在后续的表单中,调试行表明它根本不记得它的值。这是代码:
Public boolNieuweOpdrachtgever As Boolean
Public Sub nieuw_project_Click()
Dim nieuweOpdrachtgever As Variant
nieuweOpdrachtgever = MsgBox("Text", vbYesNoCancel)
Select Case nieuweOpdrachtgever
Case vbYes
boolNieuweOpdrachtgever = True
Debug.Print "In nieuw_project_Click() boolNieuweOpdrachtgever = " & boolNieuweOpdrachtgever
nieuweOpdrachtgeverForm.Show
Case vbNo
boolNieuweOpdrachtgever = False
Debug.Print "In nieuw_project_Click() boolNieuweOpdrachtgever = " & boolNieuweOpdrachtgever
nieuweOpdrachtForm.Show
Case Else
Exit Sub
End Select
End Sub
例如在vbYes的情况下,它会通过一个工作表单,然后进入第二个具有基于 boolNieuweOpdrachtgever 的IF语句的表单。然而,到那时它已经失去了它的价值。你能告诉我我做错了什么吗?
答案 0 :(得分:1)
我认为问题是与工作表关联的代码与模块的类型不同。工作表代码无法设置公共变量或全局变量。
我测试了不同的场景,解决方案是将所有代码放在Worksheet代码中,并将其放入单独的Module中,然后从触发代码的Worksheet事件中调用Module。
此时,模块声明公共变量,并且可以作为公共变量访问。
工作表代码:
Private Sub SomeValue_Change()
'What would have been your code, now moved to another module
Call NewModule
End Sub
模块代码:
Option Explicit
Public tempValue As String
Sub NewModule()
'Code that was previously in the Worksheet Code
tempValue = InputBox("Please input the public variable value.","Public Variable")
'You can test it by calling it from here to simplify the process.
Call TestValues
End Sub
其他代码:将其放在其他模块中。请注意,根本没有变量声明。仅在包含" NewModule"的模块中。
Sub TestValues()
MsgBox("The value from the public variable is :" & tempValue & ".")
End Sub
通过从Module而不是Worksheet-Code声明变量,可以捕获变量并在全局访问该变量。从工作表代码执行相同的操作失败。
答案 1 :(得分:0)
我尝试了你的代码,我想我知道你的问题。当你获得表单中的变量时,比如表格nieuweOpdrachtgeverForm,那么你需要调用模块和变量,而不仅仅是变量。例如:Me.Label1.Caption = Module1.boolNieuweOpdrachtgever
以下是我用于按钮调用的Sub(在Module1中)的代码:
Public boolNieuweOpdrachtgever As Boolean
Sub nieuw_project_Click()
Dim nieuweOpdrachtgever As Variant
nieuweOpdrachtgever = MsgBox("Text", vbYesNoCancel)
Select Case nieuweOpdrachtgever
Case vbYes
boolNieuweOpdrachtgever = True
Debug.Print "In nieuw_project_Click() boolNieuweOpdrachtgever = " & boolNieuweOpdrachtgever
nieuweOpdrachtgeverForm.Show
Case vbNo
boolNieuweOpdrachtgever = False
Debug.Print "In nieuw_project_Click() boolNieuweOpdrachtgever = " & boolNieuweOpdrachtgever
nieuweOpdrachtForm.Show
Case Else
Exit Sub
End Select
End Sub
这是表单中的代码,我用来测试传递变量。请注意,我添加了一个名为Label1的标签,并将结果值放在标签中:
Private Sub UserForm_Initialize()
Me.Label1.Caption = Module1.boolNieuweOpdrachtgever
End Sub
选择是和否后,表格中的结果如下:
答案 2 :(得分:0)
Sjors - 我想知道我的答案是否有用(我没有看到支票检查),但我很高兴。为了回答你的上一条评论,我添加了代码。第一位是Module1中的代码,第二位是Sheet1中的代码,另一位来自Forms(根据我上面的答案)。我建议你将代码剪切并粘贴到项目中并逐步完成(F8)以查看它是如何工作的。这应该可以让您基本了解如何在对象之间进行调用。
MODULE1:
Public boolNieuweOpdrachtgever As Boolean
Sub nieuw_project_Click()
Dim nieuweOpdrachtgever As Variant
Dim strFromSheet1 As String
nieuweOpdrachtgever = MsgBox("Text", vbYesNoCancel)
Select Case nieuweOpdrachtgever
Case vbYes
boolNieuweOpdrachtgever = True
Debug.Print "In nieuw_project_Click() boolNieuweOpdrachtgever = " & boolNieuweOpdrachtgever
nieuweOpdrachtgeverForm.Show
Case vbNo
boolNieuweOpdrachtgever = False
Debug.Print "In nieuw_project_Click() boolNieuweOpdrachtgever = " & boolNieuweOpdrachtgever
nieuweOpdrachtForm.Show
Case Else
Exit Sub
End Select
Call Sheet1.setString
strFromSheet1 = Sheet1.strPublic
Call Sheet1.Test(strFromSheet1)
End Sub
SHEET1:
Public strPublic As String
Public Sub Test(ByVal strTest As String)
MsgBox (strTest)
End Sub
Public Sub setString()
strPublic = "Hello"
End Sub
其中一个表格的例子:
Private Sub UserForm_Initialize()
Me.Label1.Caption = Module1.boolNieuweOpdrachtgever
End Sub