Excel VBA 获取在 "UserForm1" 私有子脚本中找到的值(整数 K),转到 "Module1" 子脚本
Excel VBA Get value (Integer K) found in "UserForm1" Private Sub script, over to the "Module1" Sub Script
我无法将 K
(整数)的值从 UserForm1
中的 Private Sub
转移到 Module1
中的 Sub
。
我使用 UserForm1
来声明我想要 运行 编码的 sheet(通过从弹出的下拉列表中选择,然后将其声明为 2 之间的 value/Integer和 9),所以我只需要将我在 UserForm1
Private Sub
中找到的值 K
转移到 Module1
.[=40 中的 Sub
=]
我希望这是可以理解的,即我希望能够在我的 Module1
脚本中读取 UserForm1
中找到的值 K
。
我目前的代码如下,从Module
部分开始:
Sub HenteMengderFraAutoCAD()
Dim K As Integer
Load UserForm1
UserForm1.Show
MsgBox (K)
Unload UserForm1
End Sub
接下来是我在 UserForm
中的代码,我在其中找到要在代码中使用的值:
Private Sub UserForm_Activate()
ComboBox1.Clear
With ComboBox1
.AddItem "M350 og XT"
.AddItem "STB 300+450"
.AddItem "Alufix"
.AddItem "MevaDec og MevaFlex"
.AddItem "Alshor Plus"
.AddItem "Rapidshor"
.AddItem "KLK og Sjaktdragere"
End With
End Sub
Private Sub CommandButton1_Click()
If ComboBox1 = "M350 og XT" Then
K = 2
ElseIf ComboBox1 = "STB 300+450" Then
K = 3
ElseIf ComboBox1 = "Alufix" Then
K = 4
ElseIf ComboBox1 = "MevaDec og MevaFlex" Then
K = 5
ElseIf ComboBox1 = "Alshor Plus" Then
K = 6
ElseIf ComboBox1 = "Rapidshor" Then
K = 7
ElseIf ComboBox1 = "KLK og Sjaktdragere" Then
K = 9
End If
MsgBox (K)
UserForm1.Hide
End Sub
Private Sub CommandButton2_Click()
Unload UserForm1
End Sub
实际结果是 Module1
脚本中的 MsgBox(K)
显示的数字与 MsgBox(K)
在 UserForm1
中显示的数字相同。
现在我在 MsgBox in the UserForm1
中得到 K
的正确值(2 到 9 取决于我在下拉列表中选择的内容),但在 Module1 MsgBox
中我只得到 0
.
提前,谢谢。
UserForms
是对象。从对象中获取 read/write 值的推荐且可靠的方法是使用 Properties。您可以创建一个 属性 然后在您的模块中访问它
示例代码。阅读代码注释了解详情。
用户表单:
Option Explicit
'/ backing field for the custom property
Private m_MyProperty As Long
'/ A public variable. Not recommended.
Public lAccessibleVariable As Long
'/ Define property setters and getters
Public Property Let MyProperty(val As Long)
m_MyProperty = val
End Property
Public Property Get MyProperty() As Long
MyProperty = m_MyProperty
End Property
Private Sub CommandButton1_Click()
'/ Do something to the property
MyProperty = 10
lAccessibleVariable = 100
'/ Make sure you just hide the form and not close(destroy it)
Me.Hide
End Sub
模块
Sub test()
'/ Create an instance of the user form
Dim frm As New UserForm1
Dim lValFromUserForm As Long
'/ launch the user form
frm.Show
'/ Read back the property value
lValFromUserForm = frm.MyProperty
'/ do something with the returned value
MsgBox lValFromUserForm
'/Just for example, access the public variable.
MsgBox frm.lAccessibleVariable
'/ Now that you are done, destroy the user form
Unload frm
End Sub
如果在用户窗体代码中,将 UserForm1
的内部引用更改为 Me
,即
UserForm1.Hide
End Sub
Private Sub CommandButton2_Click()
Unload UserForm1
到
Me.Hide
End Sub
Private Sub CommandButton2_Click()
Unload Me
并在用户表单中声明一个 public 变量,例如:
Public K As Integer
那么你可以使用:
Sub HenteMengderFraAutoCAD()
Dim K As Integer
With New UserForm1
.Show
K = .K
End With
MsgBox (K)
End Sub
完成用户表单代码
Option Explicit
Public K As Integer
Private Sub UserForm_Activate()
ComboBox1.Clear
With ComboBox1
.AddItem "M350 og XT"
.AddItem "STB 300+450"
.AddItem "Alufix"
.AddItem "MevaDec og MevaFlex"
.AddItem "Alshor Plus"
.AddItem "Rapidshor"
.AddItem "KLK og Sjaktdragere"
End With
End Sub
Private Sub CommandButton1_Click()
If ComboBox1 = "M350 og XT" Then
K = 2
ElseIf ComboBox1 = "STB 300+450" Then
K = 3
ElseIf ComboBox1 = "Alufix" Then
K = 4
ElseIf ComboBox1 = "MevaDec og MevaFlex" Then
K = 5
ElseIf ComboBox1 = "Alshor Plus" Then
K = 6
ElseIf ComboBox1 = "Rapidshor" Then
K = 7
ElseIf ComboBox1 = "KLK og Sjaktdragere" Then
K = 9
End If
MsgBox (K)
Me.Hide
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub
我的方法与 Brian M Stafford 一致。
1st:在任何子例程之前在您的 UserForm1 下将 K 声明为 public 变量,
public K as integer
第二名:
Sub HenteMengderFraAutoCAD()
Load UserForm1
UserForm1.Show
MsgBox (UserForm1.K)
Unload UserForm1
End Sub
我无法将 K
(整数)的值从 UserForm1
中的 Private Sub
转移到 Module1
中的 Sub
。
我使用 UserForm1
来声明我想要 运行 编码的 sheet(通过从弹出的下拉列表中选择,然后将其声明为 2 之间的 value/Integer和 9),所以我只需要将我在 UserForm1
Private Sub
中找到的值 K
转移到 Module1
.[=40 中的 Sub
=]
我希望这是可以理解的,即我希望能够在我的 Module1
脚本中读取 UserForm1
中找到的值 K
。
我目前的代码如下,从Module
部分开始:
Sub HenteMengderFraAutoCAD()
Dim K As Integer
Load UserForm1
UserForm1.Show
MsgBox (K)
Unload UserForm1
End Sub
接下来是我在 UserForm
中的代码,我在其中找到要在代码中使用的值:
Private Sub UserForm_Activate()
ComboBox1.Clear
With ComboBox1
.AddItem "M350 og XT"
.AddItem "STB 300+450"
.AddItem "Alufix"
.AddItem "MevaDec og MevaFlex"
.AddItem "Alshor Plus"
.AddItem "Rapidshor"
.AddItem "KLK og Sjaktdragere"
End With
End Sub
Private Sub CommandButton1_Click()
If ComboBox1 = "M350 og XT" Then
K = 2
ElseIf ComboBox1 = "STB 300+450" Then
K = 3
ElseIf ComboBox1 = "Alufix" Then
K = 4
ElseIf ComboBox1 = "MevaDec og MevaFlex" Then
K = 5
ElseIf ComboBox1 = "Alshor Plus" Then
K = 6
ElseIf ComboBox1 = "Rapidshor" Then
K = 7
ElseIf ComboBox1 = "KLK og Sjaktdragere" Then
K = 9
End If
MsgBox (K)
UserForm1.Hide
End Sub
Private Sub CommandButton2_Click()
Unload UserForm1
End Sub
实际结果是 Module1
脚本中的 MsgBox(K)
显示的数字与 MsgBox(K)
在 UserForm1
中显示的数字相同。
现在我在 MsgBox in the UserForm1
中得到 K
的正确值(2 到 9 取决于我在下拉列表中选择的内容),但在 Module1 MsgBox
中我只得到 0
.
提前,谢谢。
UserForms
是对象。从对象中获取 read/write 值的推荐且可靠的方法是使用 Properties。您可以创建一个 属性 然后在您的模块中访问它
示例代码。阅读代码注释了解详情。
用户表单:
Option Explicit
'/ backing field for the custom property
Private m_MyProperty As Long
'/ A public variable. Not recommended.
Public lAccessibleVariable As Long
'/ Define property setters and getters
Public Property Let MyProperty(val As Long)
m_MyProperty = val
End Property
Public Property Get MyProperty() As Long
MyProperty = m_MyProperty
End Property
Private Sub CommandButton1_Click()
'/ Do something to the property
MyProperty = 10
lAccessibleVariable = 100
'/ Make sure you just hide the form and not close(destroy it)
Me.Hide
End Sub
模块
Sub test()
'/ Create an instance of the user form
Dim frm As New UserForm1
Dim lValFromUserForm As Long
'/ launch the user form
frm.Show
'/ Read back the property value
lValFromUserForm = frm.MyProperty
'/ do something with the returned value
MsgBox lValFromUserForm
'/Just for example, access the public variable.
MsgBox frm.lAccessibleVariable
'/ Now that you are done, destroy the user form
Unload frm
End Sub
如果在用户窗体代码中,将 UserForm1
的内部引用更改为 Me
,即
UserForm1.Hide
End Sub
Private Sub CommandButton2_Click()
Unload UserForm1
到
Me.Hide
End Sub
Private Sub CommandButton2_Click()
Unload Me
并在用户表单中声明一个 public 变量,例如:
Public K As Integer
那么你可以使用:
Sub HenteMengderFraAutoCAD()
Dim K As Integer
With New UserForm1
.Show
K = .K
End With
MsgBox (K)
End Sub
完成用户表单代码
Option Explicit
Public K As Integer
Private Sub UserForm_Activate()
ComboBox1.Clear
With ComboBox1
.AddItem "M350 og XT"
.AddItem "STB 300+450"
.AddItem "Alufix"
.AddItem "MevaDec og MevaFlex"
.AddItem "Alshor Plus"
.AddItem "Rapidshor"
.AddItem "KLK og Sjaktdragere"
End With
End Sub
Private Sub CommandButton1_Click()
If ComboBox1 = "M350 og XT" Then
K = 2
ElseIf ComboBox1 = "STB 300+450" Then
K = 3
ElseIf ComboBox1 = "Alufix" Then
K = 4
ElseIf ComboBox1 = "MevaDec og MevaFlex" Then
K = 5
ElseIf ComboBox1 = "Alshor Plus" Then
K = 6
ElseIf ComboBox1 = "Rapidshor" Then
K = 7
ElseIf ComboBox1 = "KLK og Sjaktdragere" Then
K = 9
End If
MsgBox (K)
Me.Hide
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub
我的方法与 Brian M Stafford 一致。
1st:在任何子例程之前在您的 UserForm1 下将 K 声明为 public 变量,
public K as integer
第二名:
Sub HenteMengderFraAutoCAD()
Load UserForm1
UserForm1.Show
MsgBox (UserForm1.K)
Unload UserForm1
End Sub