Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 调试合并多个工作簿的宏_Excel_Vba - Fatal编程技术网

Excel 调试合并多个工作簿的宏

Excel 调试合并多个工作簿的宏,excel,vba,Excel,Vba,我尝试在.csv文件上使用以下宏,目的是将多个工作簿中的所有数据合并到一个工作簿中的一张工作表中 Sub cons_data() Dim Master As Workbook Dim sourceBook As Workbook Dim sourceData As Worksheet Dim CurrentFileName As String Dim myPath As String Dim LastRow As Long Dim lRow As Long Dim i As Long App

我尝试在.csv文件上使用以下宏,目的是将多个工作簿中的所有数据合并到一个工作簿中的一张工作表中

Sub cons_data()

Dim Master As Workbook
Dim sourceBook As Workbook
Dim sourceData As Worksheet
Dim CurrentFileName As String
Dim myPath As String
Dim LastRow As Long
Dim lRow As Long
Dim i As Long

Application.ScreenUpdating = False
Application.DisplayAlerts = False

'The folder containing the files to be recap'd
myPath = "C:\FakePath"

'Finds the name of the first file of type .csv in the current directory
CurrentFileName = Dir(myPath & "\*.csv*")

'Create a workbook for the recap report
Set Master = ThisWorkbook

For i = 1 To Master.Worksheets.Count
    With Master.Worksheets(i)
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row
        If lRow > 1 Then .Rows("2:" & lRow).ClearContents
    End With
Next i

Do
    Workbooks.Open (myPath & "\" & CurrentFileName)
    Set sourceBook = Workbooks(CurrentFileName)
    For i = 1 To sourceBook.Worksheets.Count
        Set sourceData = sourceBook.Worksheets(i)

        With sourceData
            LastRow = Master.Worksheets(.Name).Range("A" & Rows.Count).End(xlUp).Row
            lRow = .Range("A" & .Rows.Count).End(xlUp).Row
            .Rows("2:" & lRow).Copy Master.Worksheets(.Name).Rows(LastRow + 1)
        End With
    Next i

    sourceBook.Close

'Calling DIR w/o argument finds the next .csv file within the current directory.
CurrentFileName = Dir()
Loop While CurrentFileName <> ""

MsgBox "Done"

Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub
也就是说,我得到了错误“运行时错误'9':下标超出范围”


通常情况下,系统会提示我打开一个文件夹并能够选择所有所需的书籍,但由于某些原因,这种情况不会发生。

您收到下标超出范围错误,因为您的主工作簿中没有与csv文件同名的工作表


请记住,当您在Excel中打开csv文件时,它只有一个工作表,而该工作表的名称就是csv文件的名称(不带文件扩展名)。

我想我明白您的意思-这是.name的问题吗?以前,当我使用它打开.xls文件时,它将使用Dir()来允许我选择所需的文件。试图访问.csv文件会因为任何原因改变这一点吗?@114是的,问题在于
.Name
;您的主文件中没有此名称的工作表。经进一步审查,我认为这不是问题所在。当从给定目录中获取.xls文件时,名称作为一种方法进行访问,而不存在任何问题,尽管图纸的名称不是“Sheet1”。只有当这些文件是.csv文件时,问题才会发生。将数据复制到新文件并将其保存为.xls文件后,问题自行解决。我感觉问题与数据如何存储在.csv文件中有关,但我不确定。因此,您正在将数据复制到主文件中的Sheet1。当源文件具有名为Sheet1的图纸时,此选项有效。但除非文件名为Sheet1.csv,否则csv文件将不会有Sheet1。尝试单步查看代码以了解我的意思。尝试将问题行更改为:
LastRow=Master.Worksheets(“Sheet1”).Range(“A”&Rows.Count)。End(xlUp)。Row
。并更改此行:
.Rows(“2:”&lRow)。复制Master.Worksheets(“Sheet1”)。Rows(LastRow+1)
 LastRow = Master.Worksheets(.Name).Range("A" & Rows.Count).End(xlUp).Row