需要在macros中间手动打开一个excel文件

第一次发布,请善待。

从模板文件中,我正在运行一个macros来创build一个新的文件夹,其中有一个模板文件的副本。 然后我重命名并更新它。 有一点,我需要手动从网站下载文件并打开它,然后启动另一个macros来完成更新。

我最初试图从一个独特的macros做到这一点,但我得到的问题,因为在Excel文件有时间打开之前,macros将继续运行。

我现在已经把我的macros分成了两个。在第一个macros的末尾,我调用了一个带有指令和继续button的用户窗体。 这个想法是我打开用户窗体的时候下载文件,打开文件时点击“继续”。

出于某种原因,文件根本不能打开。 看起来像是用户窗体或macros停止打开文件。 但是,如果我运行它使用debuggingfunction,它工作正常…

Public strSN As String, strPart As String, strPath As String Sub create_new() ' Create Folder if it doesn't exist 'Dim strSN As String, strPart As String, strPath As String 'strSN = SerialNumber.Value 'strPart = PartNumber.Value 'strPath = "M:\Quality\QUALITY ASSURANCE\DOC\Rental Folder\Scanned MRB's\" ' close userform welcomeform.Hide 'set Microsoft scription runtime reference to allow creation of folder macro On Error Resume Next ThisWorkbook.VBProject.References.AddFromGUID "{420B2830-E718-11CF-893D- 00A0C9054228}", 1, 0 On Error GoTo 0 If Not FolderExists(strSN) Then 'Serial Number folder doesn't exist, so create full path FolderCreate strPath & strSN End If ' Create new file in new folder On Error Resume Next ActiveWorkbook.SaveCopyAs Filename:=strPath & strSN & "\" & strPart & " " & strSN & " " & "SNR.xlsm" If Err.Number <> 0 Then MsgBox "Copy error: " & strPath & "TEMPLATE SNR.xlsm" End If On Error GoTo 0 ' open new file without showing it or opening macros Application.EnableEvents = False 'disable Events Workbooks.Open Filename:=strPath & strSN & "\" & strPart & " " & strSN & " " & "SNR.xlsm" Application.EnableEvents = True 'enable Events ' Modify serial number and part number in traceability summary form Sheets("Traceability Summary Form").Activate Sheets("Traceability Summary Form").Unprotect Range("A7").Value = strSN Range("C7").Value = strPart ' update file with ITP Call Download_itp End Sub Sub Download_itp() downloaditp.Show End Sub 

在download_itp用户窗体中:

 Sub continue_Click() Call update_traceable_items End Sub 

然后第二个macros以代码开始:

 Sub update_traceable_items() ' ' Macro to update the SNR tab with the traceable items from the ITP ' downloaditp.Hide ' copy ITP in file Application.ActiveProtectedViewWindow.Edit ActiveSheet.Name = "ITP" ActiveSheet.Copy after:=Workbooks(strPart & " " & strSN & " " & "SNR.xlsm").Sheets("SNR template") 

任何帮助,将不胜感激! 谢谢

用户窗体正在模态显示,这可能会阻止您“打开”最近下载的文件。 当用户窗体以模态方式显示时,用户被阻止与Excel应用程序中不是用户窗体本身的任何部分“进行交互”,因此您无法select单元格或工作表,无法打开文件或closures文件等。

这是UserForms的默认行为,但幸运的是.Show方法有一个可选参数,它允许您以“无模式”的方式显示表单:

 downloaditp.Show vbModeless 

这使您可以在表单处于打开状态时与Excel应用程序进行交互。

注意:如果文件位于共享的networking位置,则可以通过使用FileDialog对象来更好地处理这个问题,以允许您“浏览”到文件的位置并打开它,所有这些都在主程序的范围内,喜欢:

 With Application.FileDialog(msoFileDialogFilePicker) .AllowMultiSelect = False .Show If .SelectedItems.Count <> 1 Then MsgBox "No file selected!", vbCritical Exit Sub Else Dim NewWorkbook as Workbook Set NewWorkbook = Workbooks.Open(.SelectedItems(0)) End If End With