0

感谢另一个有效的帮助,在下面创建了一个宏。

基本上,它采用 A 列中单元格的值,如果不存在具有该单元格名称的工作表,则创建它。然后它将具有相应单元格值的所有数据行粘贴到该工作表。IE。如果一个单元格包含以下内容:

column a  column b
dc00025   data value

如果 dc00025 不存在,它将生成工作表。并将所有带有 dc00025 的行粘贴到 A 中。

这完美地工作。但是,我注意到,当您在创建工作表后运行此宏时,由于某种原因,它添加了数千列,大大降低了 excel 的速度。

要解决此问题,是否可以修改脚本以仅复制列 b:o 而不是整行?从 A3 开始粘贴它们会更好,但我不知道如何解决这个问题。

提前致谢。

 Sub CopyCodes()

    Application.ScreenUpdating = False
    Dim rCell As Range
    Dim lastrow As Long
    lastrow = Sheets("Data").UsedRange.Rows.Count
    For Each rCell In Worksheets("Data").Range("A2:A" & lastrow).SpecialCells(xlCellTypeConstants)
        If Not SheetExists(rCell.Value) Then
            With Worksheets.Add(, Worksheets(Worksheets.Count))
            .Name = rCell.Value
            End With
        End If

        Worksheets("Data").Rows(1).EntireRow.Copy Worksheets(rCell.Value).Rows(1)
        Worksheets(rCell.Value).Range("A" & Rows.Count).End(xlUp)(2).EntireRow.Value = _
        rCell.EntireRow.Value

    Next rCell
    Application.ScreenUpdating = True

End Sub
Function SheetExists(wsName As String)
    On Error Resume Next
    SheetExists = Worksheets(wsName).Name = wsName
End Function
4

1 回答 1

0

建议修复:

Sub CopyCodes()

    Application.ScreenUpdating = False
    Dim rCell As Range
    Dim lastrow As Long
    Dim shtData as worksheet, shtDest as worksheet
    Dim sheetName as string

    set shtData=worksheets("Data")

    lastrow = shtData.cells(rows.count,1).end(xlup).row        
    For Each rCell In shtData.Range("A2:A" & lastrow).SpecialCells(xlCellTypeConstants)

        sheetName = rCell.Value
        If Not SheetExists(sheetName) Then
            set shtDest = Worksheets.Add(, Worksheets(Worksheets.Count))
            shtDest.Name = sheetName
            shtData.Rows(1).EntireRow.Copy shtDest.Rows(1)
        Else
            set shtDest = Worksheets(sheetName)              
        End If

        shtDest.Range("A" & Rows.Count).End(xlUp).offset(1,0).EntireRow.Value = _
                                                            rCell.EntireRow.Value

    Next rCell
    Application.ScreenUpdating = True

End Sub
于 2012-12-19T01:04:12.453 回答