2015-06-09 54 views
0

下面的代碼爲其他兩個工作簿中的每個分頁添加了一個新的工作表到一個摘要工作簿。我想將其他工作簿中的數據複製到摘要工作簿中的每個添加的工作表。所有數據都只能複製到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 

回答

1

Excel將通過活動工作表之前默認情況下,新的工作表,讓你的「工作表Sheet1」會在每次添加新表因爲這是處於活動狀態時移。
如果您看到worksheet.add方法的文檔,您會看到新創建的工作表也被激活,因此不需要再次激活它,只需粘貼您的數據;)

+0

謝謝!我錯誤地認爲自從我在工作簿之間切換後,我需要激活它 – user1026987

相關問題