[英]Excel VBA - Copy Rows to New Workbook
我編寫了一個宏,它將根據列中的值進行過濾,為每個不同的值創建一個由過濾器命名的新工作表,然后將包含該不同值的行復制到新工作表中。 我知道如何將整個工作表復制到新工作簿中(並根據源工作表的名稱命名工作簿),但我想省略中間步驟,直接創建新工作簿,因為我的一些數據集是如此之大,以至於 Excel 無法處理新工作表的數量。 我的原始代碼在下面創建了新的工作表,我想知道如何修改它以便創建新的工作簿並將它們保存到與原始主文件相同的目錄中
Sub parse_data()
Dim lr As Long
Dim ws As Worksheet
Dim vcol, i As Integer
Dim icol As Long
Dim myarr As Variant
Dim title As String
Dim titlerow As Integer
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''This macro splits data into multiple worksheets based on the variables on a column found in Excel.''''
'''''An InputBox asks you which columns you'd like to filter by, and it just creates these worksheets.'''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Application.ScreenUpdating = False
vcol = Application.InputBox(prompt:="Which column number would you like to filter by?", title:="Filter column", Default:="2", Type:=1)
Set ws = ActiveSheet
lr = ws.Cells(ws.Rows.Count, vcol).End(xlUp).Row
title = "A1"
titlerow = ws.Range(title).Cells(1).Row
icol = ws.Columns.Count
ws.Cells(1, icol) = "Unique"
For i = 2 To lr
On Error Resume Next
If ws.Cells(i, vcol) <> "" And Application.WorksheetFunction.Match(ws.Cells(i, vcol), ws.Columns(icol), 0) = 0 Then
ws.Cells(ws.Rows.Count, icol).End(xlUp).Offset(1) = ws.Cells(i, vcol)
End If
Next
myarr = Application.WorksheetFunction.Transpose(ws.Columns(icol).SpecialCells(xlCellTypeConstants))
ws.Columns(icol).Clear
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''This section filters the data in the specified column, then copies it into a new worksheet''''''''''''
'''''The new worksheet is named after the filtered value'''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
For i = 2 To UBound(myarr)
ws.Range(title).AutoFilter field:=vcol, Criteria1:=myarr(i) & ""
If Not Evaluate("=ISREF('" & myarr(i) & "'!A1)") Then
Sheets.Add(after:=Worksheets(Worksheets.Count)).Name = myarr(i) & ""
Else
Sheets(myarr(i) & "").Move after:=Worksheets(Worksheets.Count)
End If
ws.Range("A" & titlerow & ":A" & lr).EntireRow.Copy Sheets(myarr(i) & "").Range("A1")
Sheets(myarr(i) & "").Columns.AutoFit
Next
ws.AutoFilterMode = False
ws.Activate
Application.ScreenUpdating = True
End Sub
昨晚我一直在玩它,並想出了一個可行的解決方案。 它可能不是最優雅的解決方案,但它在合理的時間內完成了工作。 在底部,我將副本 function 移動到for
循環中,所以現在代碼如下所示:
Sub Split_to_Workbooks()
Dim lr As Long
Dim ws As Worksheet
Dim vcol, i As Long
Dim icol As Long
Dim myarr As Variant
Dim title As String
Dim titlerow As Long
Dim FPath As String
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''This macro splits data into multiple workbooks based on the variables in a column found in Excel.''''''
'''''An InputBox asks you which columns you'd like to filter by, and it just creates these worksheets.'''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Application.ScreenUpdating = False
FPath = Application.ActiveWorkbook.Path
vcol = Application.InputBox(prompt:="Which column number would you like to filter by?", title:="Filter column", Default:="2", Type:=1)
Set ws = ActiveSheet
lr = ws.Cells(ws.Rows.Count, vcol).End(xlUp).Row
title = "A1"
titlerow = ws.Range(title).Cells(1).Row
icol = ws.Columns.Count
ws.Cells(1, icol) = "Unique"
For i = 2 To lr
On Error Resume Next
If ws.Cells(i, vcol) <> "" And Application.WorksheetFunction.Match(ws.Cells(i, vcol), ws.Columns(icol), 0) = 0 Then
ws.Cells(ws.Rows.Count, icol).End(xlUp).Offset(1) = ws.Cells(i, vcol)
End If
Next
myarr = Application.WorksheetFunction.Transpose(ws.Columns(icol).SpecialCells(xlCellTypeConstants))
ws.Columns(icol).Clear
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''This section filters the data in the specified column, then copies it into a new workbook''''''''''''
'''''The new workbook is named after the filtered value'''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
For i = 2 To UBound(myarr)
ws.Range(title).AutoFilter field:=vcol, Criteria1:=myarr(i) & ""
If Not Evaluate("=ISREF('" & myarr(i) & "'!A1)") Then
Dim NewBook As Workbook
Workbooks.Add.SaveAs Filename:=FPath & "\" & myarr(i) & "" & ".xlsx"
Set NewBook = ActiveWorkbook
ws.Range("A" & titlerow & ":A" & lr).EntireRow.Copy NewBook.Sheets(1).Range("A1")
NewBook.Save
NewBook.Close False
Else
End If
Next
ws.AutoFilterMode = False
ws.Activate
Application.ScreenUpdating = True
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.