繁体   English   中英

Excel VBA - 将行复制到新工作簿

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM