简体   繁体   English

从文件夹中的最新excel文件导入图纸? 第一次使用VBA

[英]Import sheets from the most recent excel file in a folder? first time using VBA

I spent all day trying to understand VBA and I tried so many different websites to find the right code, but i just cant make it work. 我花了整整一天的时间试图了解VBA,并尝试了许多不同的网站来找到正确的代码,但是我无法使其正常工作。 The code i am using right now imports all the worksheets on all the excel files I have on my folder. 我现在使用的代码现在将导入我文件夹中所有excel文件中的所有工作表。 I only need to import the most recent one. 我只需要导入最新的。 I added a command button with it just as suggested in the website where i got this code. 正如我在获得此代码的网站中所建议的那样,我在其中添加了一个命令按钮。 In the long run i would like to be able to apply the data imported to a table i already have on the main worksheet, following by printing the template and then deleting the information so i can start over with the next recent spreadsheet. 从长远来看,我希望能够将导入的数据应用到我已经在主工作表上拥有的表中,接下来是打印模板,然后删除信息,以便我可以使用下一个最近的电子表格重新开始。 BUT for now i only want to know how to get ONLY the most recent file imported to my worksheet. 但是现在我只想知道如何仅将最新文件导入到我的工作表中。

Private Sub CommandButton1_Click()

Dim directory As String, fileName As String, sheet As Worksheet, total As Integer

directory = "C:\ExcelPract\"
fileName = Dir(directory & "*.xl??")


Do While fileName <> ""
    Workbooks.Open (directory & fileName)

    For Each sheet In Workbooks(fileName).Worksheets
        total = Workbooks("Docket .xls").Worksheets.count
        Workbooks(fileName).Worksheets(sheet.Name).Copy _
        after:=Workbooks("Docket .xls").Worksheets(total)
    Next sheet

    Workbooks(fileName).Close
    fileName = Dir()
Loop

Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub

You could use the below code and call the NewestFile function from the CommandButton1_Click() . 您可以使用以下代码,并从CommandButton1_Click()调用NewestFile函数。 I have only replaced the below line in your Sub. 我仅在您的Sub中替换了以下行。

fileName = NewestFile(directory, "*.xls") fileName = NewestFile(目录,“ * .xls”)

Function NewestFile(directory, FileSpec)
' Returns the name of the most recent file in a Directory
' That matches the FileSpec (e.g., "*.xls").
' Returns an empty string if the directory does not exist or
' it contains no matching files
    Dim fileName As String
    Dim MostRecentFile As String
    Dim MostRecentDate As Date
    If Right(directory, 1) <> "\" Then directory = directory & "\"

    fileName = Dir(directory & FileSpec, 0)
    If fileName <> "" Then
        MostRecentFile = fileName
        MostRecentDate = FileDateTime(directory & fileName)
        Do While fileName <> ""
            If FileDateTime(directory & fileName) > MostRecentDate Then
                 MostRecentFile = fileName
                 MostRecentDate = FileDateTime(directory & fileName)
             End If
             fileName = Dir
        Loop
    End If
    NewestFile = MostRecentFile
End Function


Private Sub CommandButton1_Click()

Dim directory As String, fileName As String, sheet As Worksheet, total As Integer

directory = "C:\ExcelPract\"
fileName = NewestFile(directory, "*.xls")


Do While fileName <> ""
    Workbooks.Open (directory & fileName)

    For Each sheet In Workbooks(fileName).Worksheets
        total = Workbooks("Docket .xls").Worksheets.Count
        Workbooks(fileName).Worksheets(sheet.Name).Copy _
        after:=Workbooks("Docket .xls").Worksheets(total)
    Next sheet

    Workbooks(fileName).Close
    fileName = Dir()
Loop

Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM