2017-07-19 196 views
0

我有一個工作簿90000線和三個工作表(Sheet 1中Sheet 2中,表Sheet 3)Excel宏:分流工作簿

表1具有主數據(90000個系)

表2有一些數據

表3有一些數據

我要的是將表1中的數據拆分爲5000行,拷貝表2和表3原樣,然後將其另存爲「filename-1」。我想爲所有線路做到這一點。我還需要所有分割文件中的標題。我想將它保存爲xml格式。

如果任何人能幫助將是偉大的!

我目前來到這裏,只拆分sheet1,不復制標題和sheet2和3.並不保存爲xml。 [爲了示例的目的,我已經在每5行之後保留這一點]

Sub Macro1() 
    Dim rLastCell As Range 
    Dim rCells As Range 
    Dim strName As String 
    Dim lLoop As Long, lCopy As Long 
    Dim wbNew As Workbook 

    With ThisWorkbook.Sheets(1) 
     Set rLastCell = .Cells.Find(What:="*", After:=[A1], SearchDirection:=xlPrevious) 

     For lLoop = 1 To rLastCell.Row Step 5 
      lCopy = lCopy + 1 
      Set wbNew = Workbooks.Add 
      .Range(.Cells(lLoop, 1), .Cells(lLoop + 5, .Columns.Count)).EntireRow.Copy _ 
       Destination:=wbNew.Sheets(1).Range("A1") 
      wbNew.Close SaveChanges:=True, Filename:="Chunk" & lCopy & "Rows" & lLoop & "-" & lLoop + 5 
     Next lLoop 
    End With 
End Sub 
+0

你在哪裏複製表2和表3到 – jsotola

+0

表2和3副本將所有拆分工作表。因此,一個新的工作簿將在表格1中包含標題和5000行,整個表格2和整個表格3. – woollen19

+0

您知道如何「新工作簿」,「複製行」和「複製工作表」,我認爲這不應該一個問題來完成整個任務。對於保存爲xml,您可以在[搜索]或谷歌搜索,它有很多樣本供您參考。 – Prisoner

回答

0

下面是代碼的伎倆!可能對某人有幫助。

Sub Macro1() 
Dim inputFile As String, inputWb As Workbook 
    Dim lastRow As Long, row As Long, n As Long 
    Dim newCSV As Workbook 

With ActiveWorkbook.Worksheets(1) 
    lastRow = .Cells(Rows.Count, "A").End(xlDown).row 

    Set newCSV = Workbooks.Add 

    n = 0 
    For row = 2 To lastRow Step 5 
     n = n + 1 
     .Rows(1).EntireRow.Copy newCSV.Worksheets(1).Range("A1") 
     .Rows(row & ":" & row + 5 - 1).EntireRow.Copy newCSV.Worksheets(1).Range("A2") 

     'Save in same folder as input workbook with .xlsx replaced by (n).csv 
     newCSV.SaveAs Filename:=n & ".CSV", FileFormat:=xlCSV, CreateBackup:=False 
    Next 
End With 

newCSV.Close saveChanges:=False 

End Sub 
相關問題