vba 有没有办法将用户表单的输入值(文本框)用作另一个用户表单的变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17701020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to use an input value (textbox) of a userform as a variable for another userform?
提问by user2506445
Ok so what I am trying to accomplish: I have one userform that asks how many new orders the user needs to process. I set the user input to a variable in this code (not sure if it even does anything).
好的,所以我想要完成的是:我有一个用户表单,询问用户需要处理多少个新订单。我在这段代码中将用户输入设置为一个变量(不确定它是否做了任何事情)。
Private Sub CommandButton1_Click()
UserForm2.Show
OrderNum.Text = NewOrders
'I changed the textbox name to OrderNum
End Sub
Then when UserForm2 pops up, I want to be able to input more data with more specific information about the orders. So if on Userform1 I entered in 3, I want to have to submit new data into UserForm2 3 different times. I tried using a For - Next loop (below) but it doesn't work. I'm not sure how (or if) I can store variables like that between Userforms.
然后,当 UserForm2 弹出时,我希望能够输入更多包含有关订单的更具体信息的数据。因此,如果我在 UserForm1 上输入了 3,我希望必须将新数据提交到 UserForm2 3 次。我尝试使用 For - Next 循环(如下)但它不起作用。我不确定如何(或是否)可以在用户表单之间存储这样的变量。
Private Sub CommandButton1_Click()
Dim lRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Core Info")
OrderNum.Text = NewOrders
lRow = ws.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Row
For i = 1 To NewOrders
ws.Cells(lRow, 1).Value = TextBox1.Text
ws.Cells(lRow, 3).Value = TextBox2.Text
Next i
UserForm2.Hide
End Sub
The the second userform pops up as it should, but then nothing works after that. Can anyone tell me what I could do to fix this?
第二个用户表单按原样弹出,但之后没有任何效果。谁能告诉我我能做些什么来解决这个问题?
Note: I realize that those of the above start with CommandButton1 (default) but they are on different Userforms.
注意:我意识到上面的那些以 CommandButton1(默认)开头,但它们位于不同的用户表单上。
回答by David Zemens
It will be helpful (and is good coding practice) to give your form controls more easily recognizable names, instead of the ambiguous CommandButton1
nomenclature. Revise CommandButton1
's name on UserForm1
to Form1_SubmitButton
. Then, use this code to handle the form submission.
为您的表单控件提供更易于识别的名称而不是模棱两可的CommandButton1
命名法将很有帮助(并且是良好的编码实践)。修订CommandButton1
的上名字UserForm1
来Form1_SubmitButton
。然后,使用此代码来处理表单提交。
Sub Form1_SubmitButton_Click()
' a textbox control named OrderNum on UserForm1 has captured the value
'Assign the value from the OrderNum textbox to a named variable in the worksheet
ActiveWorkbook.Names.Add "OrderNum", Me.OrderNum.Text
UserForm2.Show
End Sub
Then, in UserForm2 change the name of your command button to something like Form2_SubmitButton
and use this code:
然后,在 UserForm2 中将命令按钮的名称更改为类似的名称Form2_SubmitButton
并使用以下代码:
Sub Form2_SubmitButton_Click()
Dim orderNum as Long
orderNum = Replace(ActiveWorkbook.Names("OrderNum").Value,"=",vbNullString)
'the rest of your code goes here.
End Sub
So, what we have done above is to create a Name
in the worksheet which contains the value you want to use on the several forms. You can always obtain this value by Replace(ActiveWorkbook.Names("OrderNum").Value,"=",vbNullString)
因此,我们上面所做的是Name
在工作表中创建一个包含要在多个表单上使用的值的工作表。您始终可以通过以下方式获得此值Replace(ActiveWorkbook.Names("OrderNum").Value,"=",vbNullString)
Alternatively, you could use a public variable in your code module.
或者,您可以在代码模块中使用公共变量。