在VBA中将可变表单模块传递给用户表单

首先,这是我第一次在堆栈溢出的post,所以我希望我遵循正确的程序。 我已经浏览了这个网站和其他网站上的几十个post,但我似乎无法推断类似案件的解决scheme。 我也尝试过使用debugging行,但我不能指出问题,可能是由于我是VBA新手。 以下是我简而言之,希望你能帮助:

Sheet1上的一个命令button引发了一个Yes / No / Cancel msgbox,我想要一个机制来记住用户窗体和模块中的这个select,所以我声明boolNieuweOpdrachtgever是一个公共variables,但是在后面的窗体中,debugging行指示它根本不记得它的价值。 代码如下:

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语句。 但是到那时它已经失去了价值。 你能告诉我我做错了什么吗?

我相信问题是与工作表关联的代码不是一个模块相同的types。 工作表代码不能设置公共或全局variables。

我已经testing了不同的场景,解决scheme是将所有代码放入Worksheet代码中,然后将其放入单独的Module中,然后从触发代码的Worksheet事件中调用Module。

在这一点上,模块声明公共variables,它可以作为一个公共variables访问。

表单代码:

 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 

其他代码:把它放在一个不同的模块中。 请注意,根本没有variables的声明。 只在包含“NewModule”的模块中。

 Sub TestValues() MsgBox("The value from the public variable is :" & tempValue & ".") End Sub 

通过从模块而不是工作表编码中声明variables,该variables被全局捕获和访问。 从工作表代码做同样的事情失败。

我试过你的代码,我想我知道你的问题。 当你在窗体中获取variables时,比如说窗体nieuweOpdrachtgeverForm,那么你需要调用模块和variables,而不仅仅是variables。 例如:Me.Label1.Caption = Module1.boolNieuweOpdrachtgever

这里是我用于button调用的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 

这里是表单中的代码,我用来testing传递variables。 请注意,我添加了一个名为Label1的标签,并将结果值放在标签中:

 Private Sub UserForm_Initialize() Me.Label1.Caption = Module1.boolNieuweOpdrachtgever End Sub 

select“是”和“否”后,在表格上显示结果:

表单返回false

表单返回true

Sjors – 我想知道我的答案是否有帮助(我没有看到检查选中),但我很高兴。 要回答你最后的评论,我添加了代码。 第一位是Module1中的代码,第二位在Sheet1中,另一位来自Forms(根据我上面的回答)。 我build议你将代码剪切并粘贴到你的项目中,并逐步完成(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 

一种forms的例子:

 Private Sub UserForm_Initialize() Me.Label1.Caption = Module1.boolNieuweOpdrachtgever End Sub