将数据复制到添加的工作表

以下代码为其他两个工作簿中的每个分页添加了一个新的工作表到一个摘要工作簿。 我想将其他工作簿中的数据复制到摘要工作簿中的每个添加的工作表。 所有的数据都只复制到sheet1。 预先感谢您的任何帮助。

For n = 1 To Workbooks(file1name).Worksheets("Sheet1").HPageBreaks.Count i = i + 1 'integer that tracks the pages being added 'SourceRange is a the range of data being copied Set SourceRange = Workbooks(file1name).Worksheets("Sheet1").Range("A" & pgBreak, "Q" & Workbooks(file1name).Worksheets("Sheet1").HPageBreaks(n).Location.Row - 1) SourceRange.Copy summary.Sheets.Add 'summary is the summary workbook I would like to copy the data to summary.Sheets(i).Activate ActiveSheet.Paste For j = 1 To Workbooks(file2name).Worksheets("Sheet1").HPageBreaks.Count If matchVar = matchVar2 Then 'If the variable in workbook 1 matches the variable in workbook 2 than copy the information in between the page breaks i = i + 1 Set SourceRange = Workbooks(file2name).Worksheets("Sheet1").Range("A" & pgBreak2, "Q" & Workbooks(file2name).Worksheets("Sheet1").HPageBreaks(j).Location.Row - 1) SourceRange.Copy summary.Sheets.Add summary.Sheets(i).Activate ActiveSheet.Paste End If Next 

Excel在活动工作表之前默认添加新工作表,所以每次添加一个新工作表时,您的“工作表1”就会被移动,因为这是活动工作表。
如果您看到worksheet.add方法的文档,您会看到新创build的工作表也被激活,因此不需要再次激活它,只需粘贴您的数据;)