Excel宏错误“-2147417848(80010108错误”:“Range”的“Copy”方法失败

fruv7luv  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(169)

我有一个宏,我目前运行在两个不同的工作簿,但它给我的上述错误只有一个工作簿。通过反复试验,我了解到当我对较小批次的数据进行排序时,它是有效的,但是当我尝试对数据(5000+)进行排序时,工作簿停止响应并弹出错误。
该宏的目的是将数据分布到基于第四列的不同命名选项卡中。用户选择他们想要排序的信息,宏完成其余的工作。有没有更好的方法来做到这一点,使它可以处理10000+行数据?

Sub Disperse_Data()
      For Each myCell In Selection.Columns(4).Cells
        If myCell.Value = "400" Then
        myCell.EntireRow.Copy Worksheets("SU400").Range("A" & Rows.Count).End(3)(2)
    End If
Next

Next触发下一个示例,其中值是不同的数字,工作表是不同的名称。谢谢大家的帮助!
我尝试重写宏,将宏从另一个工作簿复制到这个工作簿中,验证所有数据都是正确的数据(数字在数字是预期的地方,等等)。我期待/希望它能起作用。

gab6jxml

gab6jxml1#

使用自动筛选

Option Explicit

Sub Disperse_Data()

    Dim wb As Workbook, wsData As Worksheet, ws As Worksheet
    Dim rngData As Range, n As Long, lastrow As Long
    Dim s As String, c As Long, r as Long
    Dim t0 As Single: t0 = Timer
    
    ' check selection
    If Selection.Column <> 4 Then
        MsgBox "Select column D", vbCritical
        Exit Sub
    ElseIf vbNo = MsgBox(Selection.Rows.Count & " rows selected, OK", _
        vbYesNo, "Confirm") Then
        Exit Sub
    End If
    
    Set wsData = Selection.Parent
    With wsData
        Set rngData = Intersect(Selection, .UsedRange)
        ' last column
        c = .UsedRange.Column + .UsedRange.Columns.Count - 1
        'MsgBox rngData.Address & " " & c
    End With
    
    ' copy
   Application.ScreenUpdating = False
    Set wb = ThisWorkbook
    For Each ws In wb.Sheets
        If ws.Name Like "SU4##" Then
            s = Right(ws.Name, 3)
            'ws.Cells.Clear
            r = 1 + ws.Cells(ws.Rows.Count, 4).End(xlUp).Row
            With rngData.Offset(, -3).Resize(, c)
                .AutoFilter 4, s
                .SpecialCells(xlCellTypeVisible).Copy _
                  ws.Range("A" & r)
                .AutoFilter
                n = n + 1
            End With
            ' remove header
            If ws.Range("D" & r) <> s Then ws.Rows(r).Delete
        End If
    Next
    Application.ScreenUpdating = True
    
    MsgBox n & " sheets updated", vbInformation, _
           Format(Timer - t0, "0.0 secs")
    
End Sub

相关问题