Excel VBA获取“UserForm1”私有子脚本中的值(整数K),转到“Module1”子脚本

问题描述 投票:0回答:3

我很难从KPrivate SubUserForm1Sub获得我的价值Module1(整数)。我使用UserForm1来声明我想要运行代码的表单(通过从弹出的下拉列表中选择,然后将其声明为2到9之间的值/整数),所以我只需要传输值K I在UserForm1 Private Sub找到我的SubModule1

我希望这是可以理解的,即我希望能够在我的K脚本中读取UserForm1中找到的值Module1

我目前的代码如下,从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

实际结果是MsgBox(K)脚本中的Module1将显示MsgBox(K)UserForm1中显示的相同数字。现在我在K得到MsgBox in the UserForm1的正确值(2到9取决于我在下拉列表中选择的内容),但在Module1 MsgBox我只得到0

提前谢谢。

excel vba module combobox userform
3个回答
1
投票

如果在userform代码中,您将UserForm1的内部引用更改为Me,即

UserForm1.Hide
End Sub

Private Sub CommandButton2_Click()
Unload UserForm1

Me.Hide
End Sub

Private Sub CommandButton2_Click()
Unload Me

并在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

4
投票

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


示例代码。阅读代码注释以获取详细信

用户表格:

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

1
投票

我的方法与Brian M Stafford一致。

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

public K as integer

第二:

Sub HenteMengderFraAutoCAD()

Load UserForm1
UserForm1.Show

MsgBox (UserForm1.K)

Unload UserForm1
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.