答:确保Sheet1为活动工作表时,运行下面代码。 Sub Demo() Dim MyRng As Range Dim DesRng As Range Set MyRng = Range(B6:G10) Set DesRng = Sheets(Sheet2).Cells(Rows.Count, 4).End(xlUp).Offset(1, 0) MyRng.Copy With DesRng .PasteSpecial xlPasteValues .Offset(0, -3).Resize(MyRng.Rows.Count, 1) = Range(F1) .Offset(0, -2).Resize(MyRng.Rows.Count, 1) = Range(B1) .Offset(0, -1).Resize(MyRng.Rows.Count, 1) = Range(B2) End With Application.CutCopyMode = FalseEnd Sub
下面是一个示例的VBA代码,可以帮助您批量提取指定文件夹中所有Excel表格中特定工作表的内容,并将其汇总到一张表中: vba Sub ExtractDataFromSheets() Dim FolderPath As String Dim FileName As String Dim wbSource As Workbook Dim wsSource As Worksheet Dim wsDestination As Worksheet Dim LastRow As Long Dim SourceRange As Range Dim DestinationRange As Range
Do While FileName Set wbSource = Workbooks.Open(FolderPath & FileName)
' 提取每个工作表中的数据 For Each wsSource In wbSource.Worksheets If wsSource.Name = 钢筋出库量 Then ' 更改为实际的工作表名称 LastRow = wsSource.Cells(Rows.Count, 1).End(xlUp).Row Set SourceRange = wsSource.Range(A5:Z & LastRow) ' 更改为实际的数据范围 Set DestinationRange = wsDestination.Cells(Rows.Count, 1).End(xlUp).Offset(1) SourceRange.Copy DestinationRange End If Next wsSource
wbSource.Close SaveChanges:=False FileName = Dir Loop