通过UserForm处理文件而不会卡在后面

时间:2016-01-24 00:11:02

标签: excel-vba vba excel

我尝试启动工作簿,扫描数据,添加或更改,保存和关闭。 问题是,当工作簿打开时,它将获得焦点,并且UserForm将移至后台。功能“工作簿(.........)。激活”无法正常工作,以获得焦点 甚至“Application.Run”也不起作用

我将不胜感激任何建议 谢谢!

我的问题的一个例子:

Private Sub CommandButton1_Click() 
    MyFilename = Application.GetOpenFilename("Excel Files(*.xls*),excel", 1, "Select file", "open", False)
    Workbooks.Open (MyFilename)

    For i = Len(MyFilename) To 1 Step -1
        If Mid(MyFilename, i, 1) = "\" Then MyFilename = Mid(MyFilename, i + 1, Len(MyFilename) - i): Exit For
    Next

    Label1.Caption = Workbooks(MyFilename).Sheets(1).Cells(1, 1)
    Label2.Caption = Workbooks(MyFilename).Sheets(1).Cells(2, 1)
    Label3.Caption = Workbooks(MyFilename).Sheets(1).Cells(3, 1)
End Sub

1 个答案:

答案 0 :(得分:0)

只需将Me.Activate行放在CommandButton1_Click()子。

的末尾

将行Workbooks("UserFormWorkbook.xls").Activate放在由Workbook_Activate打开的工作簿的UserFormWorkbook事件中。

Private Sub Workbook_Activate()
    Workbooks("UserFormWorkbook.xls").Activate
End sub