简体   繁体   English

使用从一个子过程到另一个子过程的输入框数据

[英]using input box data from one sub procedure to another

Need a little bit of help with a problem and I hope this all makes sense. 需要一点帮助来解决问题,我希望这一切都有意义。

I have a sub procedure that sorts a form and then has an input box display asking for the users name. 我有一个子过程,对表单进行排序,然后有一个输入框显示,询问用户名。

I have the following code that does this then adds it to a specific cell on the form : 我有以下执行此操作的代码,然后将其添加到表单上的特定单元格中:

Dim UserName as string

UserName = Application.InputBox(Prompt:="Please Enter your Name", Title:="Name Of User", Type:=2)

Range("A" & LastRow).Select

ActiveCell.FormulaR1C1 = "Name      " & UserName

This works perfectly but I also have another workbook open with some other data that gets sorted from the sub procedure after this one, which also works fine but what I would like is for the UserName variable to be passed to this other sub procedure so I don't have to have the input box display a second time. 这工作正常,但我还打开了另一本工作簿,该工作簿中有其他数据是在子过程之后从子过程中排序的,这也可以正常工作,但我希望将UserName变量传递给该其他子过程,所以我不不必让输入框再次显示。

I have tried declaring the variable as public and placing it both inside the sub procedure and outside all sub procedures with no luck. 我试过将变量声明为public,并将其放置在子过程内和所有子过程外,但没有运气。 I have tried assigning the UserName variable inside the sub procedure to a public variable UserName2 outside all other sub procedures and then trying to use the UserName2 variable again with no luck. 我尝试将子过程内的UserName变量分配给所有其他子过程外的公共变量UserName2,然后尝试再次使用UserName2变量,但没有运气。

So any help would be greatly appreciated 因此,任何帮助将不胜感激

Thanks Gareth 谢谢加雷斯

Place the following in a Standard Module : 将以下内容放入标准模块中

Dim UserName As String

Sub MAIN()
    Call FirstSub
    Call SecondSub
End Sub

Sub FirstSub()
    Dim LastRow As Long
    LastRow = 3
    UserName = Application.InputBox(Prompt:="Please Enter your Name", Title:="Name Of User", Type:=2)
    Range("A" & LastRow).Select
    ActiveCell.FormulaR1C1 = "Name      " & UserName
End Sub

Sub SecondSub()
    MsgBox UserName
End Sub

then run MAIN 然后运行主

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

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