userform内部的子功能 [英] userform inside the subfunction

查看:92
本文介绍了userform内部的子功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





这是我的代码

  Sub Macro6 )

Dim lines As Long
Dim letter As String
Dim no As String
Dim count As Integer
Dim i As Integer
Dim xRow As Long
Dim TargetFiles As FileDialog
Dim xDirect $,xFname $,InitialFoldr $
Dim DataBook As Workbook,OutBook As Workbook
Dim DataSheet As Worksheet,OutSheet As Worksheet

count = Sheets.count

如果count> 1然后
对于i = 1 To Sheets.count
如果Sheets(i).Name<> Sheet1和Sheets(i).Name<> 执行和表(i).Name<> DBCONNECTORS和表(i).Name<> Cil连接器然后
表单(i)。选择
设置OutBook = ActiveWorkbook
设置OutSheet = OutBook.Sheets(i)
Dim myValue,myValue1,myValue2,myValue3,myValue4 ,Myvalue5,myValue6,myValue7,myValue8,myValue9,myValue10,myValue11 As Variant
UserForm1.TextBox1.Value = OutSheet.Name
UserForm1.Show
Windows(DB.xlsm)。
行(1:1)。选择

我打电话<$ c $在 UserForm 中输入详细信息后,在子功能内的c> UserForm1.Show 。
问题:一旦我发出了 UserForm1.Show ,可以在子函数中执行nextline吗?如果是这样,那么如何?

解决方案

发出命令 UserForm1.Show 在子程序中,子程序停止运行,窗体上的代码(初始化,显示,等待窗体上的用户输入等)将运行。



这并不意味着其余部分被忽略或掉落。其余的代码只是被搁置了。一旦窗体关闭,您的子代码中的其余代码就应该运行。然而,在您启动 UserForm 的时候,焦点从您的子代转移,并将 Form 与其所有代码并且事件滑入之间。



如果您希望工作表在表单之前被激活(并选择第一行) code>被显示,那么你应该将 UserForm1.Show 行移到子结尾,并运行行 Windows(DB)。 xlsm)激活行(1:1)。之前选择



如果您希望在表单显示后发生这种情况,您将需要:


  1. 使表单 无模式

  2. 您将不得不将其余的代码转移到表单显示的地方(例如UserForm_Initialize)。

所以,上面两个选项的代码是:



替代1

 '...只复制最后几行或上面的子
UserForm1.TextBox1.Value = OutSheet.Name
UserForm1.Show(False)
Windows(DB.xlsm)。激活
行(1:1)。选择

替代方案2

 '...在代码模块中UserForm我们以下
Private Sub UserForm_Initialize()
Windows(DB.xlsm)。激活
行(1:1)。选择
End Sub

注意:




  • modal 功能不仅可以确保您的其余代码运行。此外 - 也许最重要的是 - 这允许用户同时与纸张和表单交互。该表格不再具有专属的重点。阅读上述引用的链接,并确保这是您想要的。

  • 将其余的代码从您的子文件移动到 UserForm_Inizialize 只是一个命题。还有其他地方可以把代码如 UserForm_Activate 或者甚至可以决定休息,因为第一个甚至发生在 UserForm


I have created the following userform in workbook .

This is my code

Sub Macro6()

Dim lines As Long
Dim letter As String
Dim no As String
Dim count As Integer
Dim i As Integer
Dim xRow As Long
Dim TargetFiles As FileDialog
Dim xDirect$, xFname$, InitialFoldr$
Dim DataBook As Workbook, OutBook As Workbook
Dim DataSheet As Worksheet, OutSheet As Worksheet

    count = Sheets.count

    If count > 1 Then
    For i = 1 To Sheets.count
    If Sheets(i).Name <> "Sheet1" And Sheets(i).Name <> "Execute" And Sheets(i).Name <> "DBCONNECTORS" And Sheets(i).Name <> "Cil Connectors" Then
        Sheets(i).Select
Set OutBook = ActiveWorkbook
Set OutSheet = OutBook.Sheets(i)
Dim myValue, myValue1, myValue2, myValue3, myValue4, Myvalue5, myValue6, myValue7, myValue8, myValue9, myValue10, myValue11 As Variant
UserForm1.TextBox1.Value = OutSheet.Name
UserForm1.Show
Windows("DB.xlsm").Activate
Rows("1:1").Select

I am calling UserForm1.Show inside sub function after entering the details in UserForm. Question: is it possible to execute nextline in sub function once I have issued the UserForm1.Show? If so, how?

解决方案

Once you have issued the command UserForm1.Show in the sub, the sub stops running and the code on the form (initialize, show, waiting for user input on the form, etc.) will run.

This does not mean that the rest of the is neglected or just dropped. The remainder of the code merely got put on hold. Once the form is closed the rest of the code in your sub should run. Yet, at the point that you initiate the UserForm the focus shifts away from your sub and the Form with all its code and events gets "slid in between".

If you want the sheet to get activated (and the first row selected) before the form is shown then you should move the line UserForm1.Show to the end of the sub and run the lines Windows("DB.xlsm").Activate and Rows("1:1").Select before.

If you want this to happen once the form is shown then you'll have to either:

  1. make the form modeless or
  2. you'll have to shift the rest of your code into a place that gets run once the form is shown (for example UserForm_Initialize).

So, the code for the above two alternatives are:

Alternative 1

'... only copied over the last few rows or your above sub
UserForm1.TextBox1.Value = OutSheet.Name
UserForm1.Show (False)
Windows("DB.xlsm").Activate
Rows("1:1").Select

Alternative 2

'... in the code module of the UserForm us the following
Private Sub UserForm_Initialize()
Windows("DB.xlsm").Activate
Rows("1:1").Select
End Sub

Notes:

  • The modal functionality does not only ensures that the rest of your code runs. Also - and perhaps most importantly - this allows a user to interact with the sheet and the form simultaneously. The form no longer has the exclusive focus. Read the above referenced link and make sure that this is what you want.
  • Moving the rest of the code from your sub to UserForm_Inizialize is just a proposition. There are other places you could put the code such as UserForm_Activate or you could even decide for the rest to happen as the first even occurs on the UserForm.

这篇关于userform内部的子功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆