将数据复制到添加的工作表
Copying data to a added worksheet
下面的代码为其他 2 个工作簿中的每个分页符向摘要工作簿添加了一个新工作sheet。我想将其他工作簿中的数据复制到摘要工作簿中每个添加的 sheet。所有数据都只复制到 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 默认情况下在活动 sheet 之前添加新的 sheet,因此每次添加新的 sheet 时,您的 "Sheet1" 都会移动是活跃的
如果您查看 worksheet.add 方法的文档,您会看到新创建的 sheet 也已激活,因此无需再次激活它,只需粘贴您的数据即可;)
下面的代码为其他 2 个工作簿中的每个分页符向摘要工作簿添加了一个新工作sheet。我想将其他工作簿中的数据复制到摘要工作簿中每个添加的 sheet。所有数据都只复制到 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 默认情况下在活动 sheet 之前添加新的 sheet,因此每次添加新的 sheet 时,您的 "Sheet1" 都会移动是活跃的
如果您查看 worksheet.add 方法的文档,您会看到新创建的 sheet 也已激活,因此无需再次激活它,只需粘贴您的数据即可;)