将工作簿传递到另一个子目录时,DIR找不到下一个文件

时间:2018-03-04 01:40:04

标签: excel vba excel-vba

有人可以帮帮我吗?我的DIR函数找到指定文件夹中的第一个文件(在" loopThroughMNFiles子中),但之后它无法找到下一个文件。这与我将工作簿传递给另一个工作簿有关...&INSINQ_Macro"但我不确定如何纠正。提前致谢!

    Sub loopThroughMNFiles()

    Application.ScreenUpdating = False

    Dim MnLoopingFolder As String

    Dim MnWbk As Workbook

    Dim MnFile As String

    Dim sheetName As String

    MnLoopingFolder = "C:\Users\xxxxxx\Desktop\outlook-attachments\MN Reports\2-26-18\INSINQ\"

    MnFile = Dir(MnLoopingFolder) 'DIR gets the first file of the folder

'Loop through all files in a folder until DIR cannot find anymore

    Do While MnFile <> “”

'Opens the file and assigns to the wbk variable for future use

        Set MnWbk = Workbooks.Open(FileName:=MnLoopingFolder & MnFile)

        Call INSINQ_Macro(MnWbk)

'MnWbk.Close

        MnFile = Dir 'DIR gets the next file in the folder

    Loop

    Application.ScreenUpdating = True

    MsgBox "Task Finished"
End Sub


    Sub INSINQ_Macro(InsinqSourceBook As Workbook)

        Application.DisplayAlerts = False

        Application.ScreenUpdating = False

        Dim OutlookApp As Outlook.Application

        Dim MItem As Outlook.MailItem

        Dim insinqWorkbookName As String

        Dim fileDate As String

        Dim folderDate As String

        fileDate = Format(Date, "yymmdd")

        folderDate = Format(Date, "mm-dd-yy")

        Dim sUserName As String

        sUserName = Environ$("username")

        Dim ws As Worksheet

        Dim success As Boolean

        Dim localDesktopMnPath As String

        For Each ws In Sheets

            sheetName = LCase(ws.Name)

            If sheetName Like "*high" Or sheetName Like "*mark" Then

                InsinqSourceBook.ws.Delete

            End If

        Next

        localDesktopMnPath = "C:\Users\" + sUserName + "\Desktop\MN Weekly\" + folderDate + "\"

        If Dir(localDesktopMnPath, vbDirectory) = "" Then

            MkDir localDesktopMnPath

        End If

        success = True

        insinqWorkbookName = LCase(InsinqSourceBook.Name)

        If InStr(insinqWorkbookName, "tenv2") <> 0 Then

            InsinqSourceBook.SaveAs FileName:=localDesktopMnPath + fileDate + " Minnesota Users on INSINQ Security Tables TENV2.csv", fileFormat:=xlCSV, CreateBackup:=False

            Set InsinqSourceBook = ActiveWorkbook

        ElseIf InStr(insinqWorkbookName, "tenv3") <> 0 Then

            InsinqSourceBook.SaveAs FileName:=localDesktopMnPath + fileDate + " Minnesota Users on INSINQ Security Tables TENV3.csv", fileFormat:=xlCSV, CreateBackup:=False

            Set InsinqSourceBook = ActiveWorkbook

        ElseIf InStr(insinqWorkbookName, "tenv4") <> 0 Then

            InsinqSourceBook.SaveAs FileName:=localDesktopMnPath + fileDate + " Minnesota Users on INSINQ Security Tables TENV4.csv", fileFormat:=xlCSV, CreateBackup:=False

            Set InsinqSourceBook = ActiveWorkbook

        ElseIf InStr(insinqWorkbookName, "tenv5") <> 0 Then

            InsinqSourceBook.SaveAs FileName:=localDesktopMnPath + fileDate + " Minnesota Users on INSINQ Security Tables TENV5.csv", fileFormat:=xlCSV, CreateBackup:=False

            Set InsinqSourceBook = ActiveWorkbook

        ElseIf InStr(insinqWorkbookName, "tenv6") <> 0 Then

            InsinqSourceBook.SaveAs FileName:=localDesktopMnPath + fileDate + " Minnesota Users on INSINQ Security Tables TENV6.csv", fileFormat:=xlCSV, CreateBackup:=False

            Set InsinqSourceBook = ActiveWorkbook

        ElseIf InStr(insinqWorkbookName, "tenv7") <> 0 Then

            InsinqSourceBook.SaveAs FileName:=localDesktopMnPath + fileDate + " Minnesota Users on INSINQ Security Tables TENV7.csv", fileFormat:=xlCSV, CreateBackup:=False

            Set InsinqSourceBook = ActiveWorkbook

        ElseIf InStr(insinqWorkbookName, "tenvb") <> 0 Then

            InsinqSourceBook.SaveAs FileName:=localDesktopMnPath + fileDate + " Minnesota Users on INSINQ Security Tables TENVB.csv", fileFormat:=xlCSV, CreateBackup:=False

            Set InsinqSourceBook = ActiveWorkbook

        ElseIf InStr(insinqWorkbookName, "tenvc") <> 0 Then

            InsinqSourceBook.SaveAs FileName:=localDesktopMnPath + fileDate + " Minnesota Users on INSINQ Security Tables TENVC.csv", fileFormat:=xlCSV, CreateBackup:=False

            Set InsinqSourceBook = ActiveWorkbook

        ElseIf InStr(insinqWorkbookName, "prod") <> 0 Then

            InsinqSourceBook.SaveAs FileName:=localDesktopMnPath + fileDate + " Minnesota Users on INSINQ Security Tables PROD.csv", fileFormat:=xlCSV, CreateBackup:=False

            Set InsinqSourceBook = ActiveWorkbook

        Else

            success = False

            InsinqSourceBook.Close savechanges:=False

'send failure email - filename wasn't named correctly

            Exit Sub

        End If

        InsinqSourceBook.Close savechanges:=False

        Application.ScreenUpdating = True

' send succesful email

    End Sub

1 个答案:

答案 0 :(得分:2)

以下是我在评论中的意思:

<input id="fileUpload" type="file">