excel vba - 根据列中的数字创建新工作表

时间:2017-03-10 17:53:29

标签: excel excel-vba vba

我在一张表中有一堆数据按部门编号排序。部门编号长四位,存储在AI列(vcol = 35)

每行数据都与部门编号相关联。我需要遍历文档,并为每个部门编号在工作簿中创建一个新工作表,该工作簿以部门编号命名,该部门编号包含与该部门编号对应的所有数据。我做了一堆搜索,并在一段时间后在StackOverflow上找到了一个例子,我试图修改为我需要的东西。这就是我的宏到目前为止的样子:

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
    vcol = 35
    Set ws = Sheets("Summary")
    lr = ws.Cells(ws.Rows.Count, vcol).End(xlUp).Row
    title = "A1:AH1"
    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
    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
End Sub

但是当我跑这两件事时。第一个问题是它将所有数据从第一张纸复制到所有其他纸张(不仅仅是与该部门号码相关联的数据),第二个错误可能背叛了 - 它给出了一个错误,说我的PC没有'有足够的内存来进行操作。任何人都可以看看这个,看看我可能会出错吗?谢谢!

0 个答案:

没有答案