gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 20:52:30 25 4
gpt4 key购买 nike

我想从文件夹(包含相同的标题和布局)中的不同工作簿中提取数据(列 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

最佳答案

如果您知道要从中提取数据的每个工作簿都使用 A 到 I 列,则将复制行更改为 Range(Cells(3, 1), Cells(lastrow, 9)).Copy这应该让它工作。它不会那么有弹性,但它会起作用。

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

关于excel - VBA 将数据从多个工作簿复制到 Mastercopy excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55045700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com