需要在宏

时间:2015-12-01 15:33:20

标签: excel vba excel-vba

首次发帖,请善待。

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

我最初试图从一个独特的宏中做到这一点,但我遇到了问题,因为宏会在excel文件有时间打开之前继续运行。

我现在已经将我的宏拆分为2.在第一个宏的末尾,我使用指令和继续按钮调用userform。我的想法是,我会在打开用户窗体时下载文件,然后在打开文件时单击“继续”。

由于某种原因,该文件根本没有打开。似乎用户窗体或宏停止打开文件。但是,如果我使用调试功能运行它,它工作正常......

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 userform中:

Sub continue_Click()

Call update_traceable_items

End Sub

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

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")

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

1 个答案:

答案 0 :(得分:1)

UserForm以模态方式显示,这可能会阻止您打开&#34;打开&#34;最近下载的文件。当以模态方式显示UserForm时,将阻止用户进行交互&#34; Excel应用程序的任何部分都不是UserForm本身 - 因此您无法选择单元格或工作表,您无法打开文件或关闭文件等。

这是UserForms的默认行为,但幸运的是.Show方法有一个可选参数,允许您以无模式显示表格&#34;:

downloaditp.Show vbModeless

这允许您在表单打开时与Excel应用程序进行交互。

注意:如果文件位于共享网络位置,您可以使用FileDialog对象更好地处理此问题,以便您浏览&#34;浏览&#34;到主文件范围内的文件位置并打开它,如:

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