VBA将数据从多个工作簿复制到Mastercopy excel

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

我想从一个文件夹(包含相同的标题和布局)中的不同工作簿中提取数据(列A:I)到Mastercopy excel。

我的代码运行但它只从不同的工作簿中复制A:E中的数据。这些工作簿中的每一个只有3行数据(第1行:时间戳A:E,第2行:实际标题A:I,第3行:要提取的实际值)。我怀疑它的第1行引起了问题,因为它只有A:E中的数据。关于如何克服这个问题的任何想法?任何建议都非常感谢。

Sub copyDataFromMultipleWorkbooksIntoMaster()

Dim FolderPath As String, Filepath As String, Filename As String

FolderPath = "C:\Users\AlexP\Desktop\Folder\Downloads\"

Filepath = FolderPath & "*.csv"

Filename = Dir(Filepath)

Dim lastrow As Long, lastcolumn As Long

Dim erow

Do While Filename <> ""
Workbooks.Open (FolderPath & Filename)

lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row

lastcolumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column

Range(Cells(3, 1), Cells(lastrow, lastcolumn)).Copy
Application.DisplayAlerts = False
ActiveWorkbook.Close

erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Paste Destination:=Worksheets("Sheet1").Range(Cells(erow, 1), Cells(erow, 9))

Filename = Dir

Loop

End Sub
excel vba
1个回答
0
投票

如果您知道从中提取数据的每个工作簿都使用A列到I列,那么请将您的副本行更改为Range(Cells(3, 1), Cells(lastrow, 9)).Copy,这样可以使其工作。它不会那么有弹性,但它会起作用。

为了使其具有弹性,请确保每个工作表在每列的第一个单元格中都有一些内容,然后您可以返回使用Columns.Count来定义复制的范围。

© www.soinside.com 2019 - 2024. All rights reserved.