简体   繁体   English

VBA:引用相同代码的多个用户窗体

[英]VBA: Multiple Userforms Referencing Same Code

I have a VBA code that takes user inputs (criteria) via a userform, creates a list of items that meet said criteria, and randomly outputs one of the list items via message box. 我有一个VBA代码,该代码通过用户表单获取用户输入(条件),创建满足上述条件的项目列表,并通过消息框随机输出列表项之一。

I want to create a second userform that will open after the above code completes, and have the userform display the random output. 我想创建第二个用户窗体,在上述代码完成后将打开它,并让该用户窗体显示随机输出。

I have a few questions regarding how this can be done. 我对如何做到这一点有一些疑问。

  1. I want to the second userform to contain a label, whose caption will reflect the random output. 我希望第二个用户窗体包含一个标签,其标题将反映随机输出。
  2. I want the second userform to allow the user to re-execute the original code (ie. to change the random result) 我希望第二个用户窗体允许用户重新执行原始代码(即更改随机结果)

Anyone have experience with this? 有人对此有经验吗?

I'd use a Property procedure in the second form to get the random value. 我将使用第二种形式的Property过程来获取随机值。

As an example in a new blank workbook: Insert a new module and paste in the code below. 在新的空白工作簿中作为示例:插入一个新模块并粘贴下面的代码。 This code represents your code which outputs a random result. 此代码代表您的代码,它输出随机结果。

Public Function RandomNumber() As Double

    RandomNumber = Rnd(10)

End Function

Create a new userform (UserForm2), add a command button (CommandButton1) and a label (Label1). 创建一个新的用户窗体(UserForm2),添加一个命令按钮(CommandButton1)和一个标签(Label1)。 Add the below code to this form. 将以下代码添加到此表单。 I've put four procedures in there. 我在其中放置了四个过程。 MyProp sets the value of ThePassedNumber , UpdateLabel1 updates the value shown in the label, UserForm_Initialize executes when the form loads, CommandButton1_Click executes when you click the button. MyProp设置ThePassedNumber的价值,UpdateLabel1更新标签显示的值,UserForm_Initialize加载窗体时,CommandButton1_Click当您单击按钮,执行执行。

Private ThePassedNumber As Double

Property Let MyProp(TheNumber As Double)
    ThePassedNumber = TheNumber
    UpdateLabel1
End Property


Private Sub CommandButton1_Click()

    ThePassedNumber = RandomNumber
    UpdateLabel1

End Sub

Private Sub UserForm_Initialize()

    UpdateLabel1

End Sub

Private Sub UpdateLabel1()

    Me.Label1.Caption = ThePassedNumber

End Sub

Add a second form (UserForm1) and add a command button (CommandButton1) and place this code within the form. 添加第二个窗体(UserForm1)和添加命令按钮(CommandButton1)并将此代码放在窗体中。
It will create a new instance of the userform, pass a random value to it and then display the form. 它将创建一个新的用户窗体实例,向其传递一个随机值,然后显示该窗体。

Private Sub CommandButton1_Click()

    Dim frm2 As UserForm2

    Set frm2 = New UserForm2
    frm2.MyProp = RandomNumber
    frm2.Show

End Sub

Now you just need to rewrite the RandomNumber function to return your list item rather than a number. 现在,您只需要重写RandomNumber函数即可返回列表项而不是数字。

I imagine you just want to use an endless loop that the user breaks when canceled. 我想您只想使用一个无限循环,该循环在用户被取消时会中断。 Something like this: 像这样:

Sub MySub()
    Dim myRandOutput As Single
    Do
        myRandOutput = myRand()
    Loop While MsgBox(myRandOutput, vbRetryCancel) = vbRetry
End Sub

Function myRand() As Single
    myRand = Rnd()
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM