循环浏览xlsm文件,得到错误438

时间:2019-04-25 08:52:09

标签: vba

我从这里获取了一些代码,但无法操作。 要遍历一个目录中的几个xlsm文件。这样,单个xlsm文件中的每一行都将显示在“摘要工作簿”中。

不幸的是,我收到错误438,可能是因为Dim方法。

请帮我弄清楚:)

Sub MergeAllWorkbooks()

Dim SummarySheet As Workbook
Dim FolderPath As String
Dim NRow As Long
Dim FileName As String
Dim WorkBk As Workbook
Dim SourceRange As Range
Dim DestRange As Range
Application.Calculation = xlCalculationAutomatic


' open summary workbook .
Set SummarySheet = ThisWorkbook

' Modify this folder path to point to the files you want to use.
FolderPath = "C:\Projects\SuperProject\BorotNisayon\"

' NRow keeps track of where to insert new rows in the destination workbook.
NRow = 2

' Call Dir the first time, pointing it to all Excel files in the folder path.
FileName = Dir(FolderPath & "*.xlsm")

' Loop until Dir returns an empty string.
Do While FileName <> ""

    ' Open a source workbook in the folder
    Set WorkBk = Workbooks.Open(FolderPath & FileName)

    ' Set the source range to be A2 through P2.
    ' Modify this range for your workbooks.
    ' It can span multiple rows.
    Set SourceRange = WorkBk.Sheets("Drill").Range("A2:P2")

    ' Set the destination range to start at column B and
    ' be the same size as the source range.
    Set DestRange = SummarySheet.Range("A2:P2" & NRow)
   

    ' Copy over the values from the source to the destination.
    DestRange.Value = SourceRange.Copy

    ' Increase NRow so that we know where to copy data next.
    NRow = NRow + DestRange.Rows.Count

    ' Close the source workbook without saving changes.
    WorkBk.Close savechanges:=False

    ' Use Dir to get the next file name.
    FileName = Dir()
Loop



   
 End Sub

0 个答案:

没有答案