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

问题描述 投票:1回答:1

有人可以帮帮我吗?我的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
excel vba excel-vba
1个回答
2
投票

这是我在评论中的意思:

Sub loopThroughMNFiles()

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

    Dim colFiles As New Collection, f
    Dim MnWbk As Workbook, MnFile As String, sheetName As String

    'add the files to a collection
    MnFile = Dir(MnLoopingFolder)
    Do While MnFile <> ""
        colFiles.Add MnFile
        MnFile = Dir()
    Loop

    Application.ScreenUpdating = False
    'loop over the filenames from the collection
    For Each f In colFiles
        INSINQ_Macro Workbooks.Open(Filename:=MnLoopingFolder & f)
    Next
    Application.ScreenUpdating = True
    MsgBox "Task Finished"
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.