Vba 是否要将所有工作簿的Sheet3中的数据导入主工作簿?

Vba 是否要将所有工作簿的Sheet3中的数据导入主工作簿?,vba,excel,Vba,Excel,代码工作正常,但从活动工作表而不是工作簿的第3页复制数据。如果有人指导我,我将活动工作表替换为第3页,但这也不起作用 Sub copyDataFromMultipleWorkbooksIntoMaster() Dim FolderPath As String, Filepath As String, Filename As String FolderPath = "D:\Copy Multiple Excel to One master\" Filepath = FolderPath &a

代码工作正常,但从活动工作表而不是工作簿的第3页复制数据。如果有人指导我,我将活动工作表替换为第3页,但这也不起作用

Sub copyDataFromMultipleWorkbooksIntoMaster()

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

FolderPath = "D:\Copy Multiple Excel to One master\"

Filepath = FolderPath & "*.xls*"

Filename = Dir(Filepath)

Dim LastRow As Long, lastcolumn As Long

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(5, 1), Cells(LastRow, lastcolumn)).Copy
Application.DisplayAlerts = False
ActiveWorkbook.Close

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

Filename = Dir

Loop
Application.DisplayAlerts = True
End Sub

Public Function ModDate()
ModDate = Format(FileDateTime(ThisWorkbook.FullName), "m/d/yy h:n ampm")
End Function
Sub-copyDataFromMultipleWorkbooksIntoMaster()
Dim FolderPath为字符串,Filepath为字符串,Filename为字符串
FolderPath=“D:\Copy Multiple Excel to One master\”
Filepath=FolderPath&“*.xls*”
Filename=Dir(文件路径)
将LastRow变长,lastcolumn变长
文件名“”时执行此操作
工作簿.打开(文件夹路径和文件名)
LastRow=ActiveSheet.Cells(Rows.Count,1).End(xlUp).Row
lastcolumn=ActiveSheet.Cells(1,Columns.Count).End(xlToLeft).Column
范围(单元格(5,1),单元格(LastRow,lastcolumn))。复制
Application.DisplayAlerts=False
活动工作簿。关闭
erow=Sheet1.单元格(Rows.Count,1).结束(xlUp).偏移量(1,0).行
lastcolumn=ActiveSheet.Cells(1,Columns.Count).End(xlToLeft).Column
ActiveSheet.Paste目标:=工作表(“Sheet1”)。范围(单元格(erow,1),单元格(erow,1))
Filename=Dir
环
Application.DisplayAlerts=True
端接头
公共功能ModDate()
ModDate=格式(FileDateTime(thispoolk.FullName),“m/d/yy h:n ampm”)
端函数
子copyDataFromMultipleWorkbooksIntoMaster()
Dim FolderPath为字符串,Filepath为字符串,Filename为字符串
FolderPath=“D:\Copy Multiple Excel to One master\”
Filepath=FolderPath&“*.xls*”
调暗lastRow为长,lastCol为长,eRow为长
将wb设置为工作簿,ws设置为工作表
Application.DisplayAlerts=False
Filename=Dir(文件路径)
文件名“”时执行此操作
eRow=Sheet1.单元格(Rows.count,1).结束(xlUp).偏移量(1,0).行
设置wb=Workbooks.Open(文件夹路径和文件名)
转到下一个文件时出错
设置ws=wb.工作表(“表3”)
与ws
lastRow=.Cells(.Rows.count,1).End(xlUp).Row
lastCol=.Cells(1,.Columns.count).End(xlToLeft).Column
.Range(.Cells(5,1),.Cells(lastRow,lastCol)).Copy
表1.单元格(eRow,1).粘贴特殊xlPasteValues
以
下一个文件:
错误转到0
wb.关闭错误
Filename=Dir
环
Application.DisplayAlerts=True
端接头

a有两个不同的“名称”。。。一个显示在Excel->
本工作簿中。工作表(“Sheet3”)…
。。。和一个“指针”(vba中的sheetlist)->ThisWorkbook.Sheet3…您可以将数据从
ActiveSheet
复制到
ActiveSheet
(在不同的工作簿中)。。。但是,您需要更改行
范围(单元格(5,1),单元格(LastRow,lastcolumn))。复制
Sheet3开始。
cuz如果没有工作表指针,它将始终选择活动工作表。执行此操作后,将显示超出范围的下标?@MFaizanFarooq您确定您的代码与活动工作表一起工作吗,每个工作簿都有一个名为“Sheet3”的工作表?另外,您可以尝试在代码中查找错误发生的位置吗?是的,现在我在每个工作簿中添加了sheet3,但现在出现了自动错误,并将此行lastcolumn=ws.Cells(1,Columns.Count).End(xlToLeft).columnum。。嗯,我怀疑这是在接下来的路线。好的,给我几分钟时间,我会尝试编辑整件事。非常感谢它工作得很好,但它也在复制源格式?
Sub copyDataFromMultipleWorkbooksIntoMaster()

    Dim FolderPath As String, Filepath As String, Filename As String
    FolderPath = "D:\Copy Multiple Excel to One master\"
    Filepath = FolderPath & "*.xls*"

    Dim lastRow As Long, lastCol As Long, eRow As Long
    Dim wb As Workbook, ws As Worksheet
    Application.DisplayAlerts = False

    Filename = Dir(Filepath)
    Do While Filename <> ""
        eRow = Sheet1.Cells(Rows.count, 1).End(xlUp).Offset(1, 0).Row
        Set wb = Workbooks.Open(FolderPath & Filename)
        On Error Goto NextFile 
        Set ws = wb.Worksheets("Sheet3")
        With ws
            lastRow = .Cells(.Rows.count, 1).End(xlUp).Row
            lastCol = .Cells(1, .Columns.count).End(xlToLeft).Column
            .Range(.Cells(5, 1), .Cells(lastRow, lastCol)).Copy
            Sheet1.Cells(eRow, 1).PasteSpecial xlPasteValues
        End With
NextFile:
        On Error Goto 0
        wb.Close False
        Filename = Dir
    Loop
    Application.DisplayAlerts = True
End Sub