Excel VBA获取值(整数K)位于;UserForm1";私人子脚本,转到;模块1“;子脚本

Excel VBA获取值(整数K)位于;UserForm1";私人子脚本,转到;模块1“;子脚本,excel,vba,module,combobox,userform,Excel,Vba,Module,Combobox,Userform,从UserForm1中的Private Sub获取值K(整数)到Module1中的Sub时遇到问题。 我使用UserForm1来声明我要在哪个工作表上运行代码(从弹出的下拉列表中选择,然后将其声明为介于2和9之间的值/整数),因此,我只需要将在UserForm1私有子部分中找到的K值转移到我的模块1中的子部分 我希望这是可以理解的,也就是说,我希望能够在我的模块1脚本中读取UserForm1中的K值 我当前的代码如下,从模块部分开始: Sub HenteMengderFraAutoCAD() D

UserForm1
中的
Private Sub
获取值
K
(整数)到
Module1
中的
Sub
时遇到问题。 我使用
UserForm1
来声明我要在哪个工作表上运行代码(从弹出的下拉列表中选择,然后将其声明为介于2和9之间的值/整数),因此,我只需要将在
UserForm1
私有子部分中找到的
K
值转移到我的
模块1
中的
子部分

我希望这是可以理解的,也就是说,我希望能够在我的
模块1
脚本中读取
UserForm1
中的
K

我当前的代码如下,从
模块
部分开始:

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)
将显示与
UserForm1中的
MsgBox(K)
显示相同的数字。 现在我在UserForm1
MsgBox中获得了
K
的正确值(2到9取决于我在下拉列表中选择的内容),但在
模块1 MsgBox
中,我只得到了
0


提前谢谢。

用户表单是对象。从对象中读取/写入值的推荐且健壮的方法是使用属性。您可以创建属性,然后在模块中访问它


示例代码。有关详细信息,请阅读代码注释

用户表格:

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

并在userform中声明一个公共变量,如:

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一致

第一:在任何子例程之前,将K声明为UserForm1下的公共变量

public K as integer
第二:


如果
K
被声明为
Public
,则以下操作将起作用:
MsgBox UserForm1.K
。供参考,有关无模式用户表单的更多信息可在问题中找到;c、 f.也使用类:-)@cyboashu感谢您提供了完整的示例!对以后的开发和理解非常有用。我有一个问题:为什么不建议使用公共变量?在我的整个代码中,我还有其他
,它们也使用vaiable
K
。这可能是个问题吗?变量
K
在每个og my
Sub
代码中定义@T.M.谢谢你的补充信息。我将通过这些来了解更多。谢谢@Mistella,这正是我想要的。
public K as integer
Sub HenteMengderFraAutoCAD()

Load UserForm1
UserForm1.Show

MsgBox (UserForm1.K)

Unload UserForm1
End Sub