object '_Worksheet' 的错误 1004 'Range' 尝试复制值时失败(明确设置工作簿和工作表,没有命名范围)

Error 1004 'Range' of object '_Worksheet' failed when trying to copy values (Workbook and worksheet explicitly set, no named ranges)

我的问题已在标题中说明。错误发生在第一行.Copy,但我和第二行一样,收到同样的错误。

我已经检查 Sheet 名称是否正确,甚至直接从 Sheet 标题中复制它们以防某些奇怪的角色偷偷溜进来。

我将在此处放置代码片段,然后在最后放置完整代码,以防出现不同的问题。

声明: (我曾尝试使用 Workbooks() 明确设置它,但没有帮助) 将 wb 调暗为工作簿

Set wb = ThisWorkbook' Or  Workbooks("collected.xlsm")
Dim sUser As Worksheet, sExceptions As Worksheet
Set sUser = wb.Sheets("User")
Set sExceptions = wb.Sheets("Exceptions")

正在复制:

sUser.Range(Cells(rS, 1)).Copy Destination:=sExceptions.Range(Cells(Count, 1))
sUser.Range(rS, 11).Copy Destination:=sExceptions.Range(Count, 2)

完整代码:

Option Explicit

Function FindExceptions()

    ' To run faster
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    ' Variable def
    Dim Count As Integer

    ' Variable def
    ' Worksheets
    Dim wb As Workbook
    Set wb = ThisWorkbook ' Or Workbooks("collected.xlsm")
    Dim sUser As Worksheet, sVCD As Worksheet, sFullExport As Worksheet
    Set sUser = wb.Sheets("User")
    Set sVCD = wb.Sheets("VCD")
    Set sFullExport = wb.Sheets("FullExport")
    ' r, f, c = Search, Find, Check
    ' For Each rows
    Dim rS As Integer, rF As Integer, rC As Integer
    'Set rS = sUser.Columns("A")
    'Set rF = sVCD.Columns("A")
    'Set rC = sFullExport("B")
    ' Vars used in execution
    'Dim cS As Range, cF As Range, cC As Range
    Dim secId As String, employeeNum As String, FoundVCD As Boolean, FoundFullExport As Boolean


    ' Go through User sheet
    For rS = 2 To sUser.UsedRange.Rows.Count
        secId = sUser.Cells(rS, "A").Value
        employeeNum = sUser.Cells(rS, "K").Value
        ' Search for in VCD
        FoundVCD = False
        For rF = 2 To sVCD.UsedRange.Rows.Count
            If sVCD.Cells(rF, "A").Value = secId And sVCD.Cells(rF, "K").Value = employeeNum Then
                FoundVCD = True
                Exit For
            End If
        Next
        'Search for in Full Export?
        If FoundVCD = True Then
            FoundFullExport = False
            For rC = 2 To sFullExport.UsedRange.Rows.Count
                If sFullExport.Cells(rC, "B").Value = secId Then
                    FoundFullExport = True
                    Exit For
                End If
            Next
        End If

        If FoundFullExport = False Then
            ' WriteExceptions sUser.Cells(rS, "A").Value, sUser.Cells(rS, "K").Value, sFullExport.Cells(rC, "A").Value, sFullExport.Cells(rC, "D").Value

            ' Worksheet var
            Dim sExceptions As Worksheet
            Set sExceptions = wb.Sheets("Exceptions")

            If Count = Null Or Count = 0 Then
                sExceptions.Cells(1, "A") = "Säk. Id"
                sExceptions.Cells(1, "B") = "Anst. Nr"
                sExceptions.Cells(1, "C") = "Unison Id"
                sExceptions.Cells(1, "D") = "Kort hex"
                Count = 2
            Else
                Count = Count + 1
            End If

            ' secId on col A, employeeNum on col B, unisonId on col C, cardHex on col D
            sUser.Range(Cells(rS, 1)).Copy _
                Destination:=sExceptions.Range(Cells(Count, 1))
            sUser.Range(rS, 11).Copy _
                Destination:=sExceptions.Range(Count, 2)
            sFullExport.Range(rC, 1).Copy _
                Destination:=sExceptions.Range(Count, 3)
            sFullExport.Range(rC, 4).Copy _
                Destination:=sExceptions.Range(Count, 4)
        End If

    Next

    Count = 0

    ' To end settings to run faster
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic

End Function

你混淆了 RangeCells

尝试

        sUser.Cells(rs, 1).Copy _
            Destination:=sExceptions.Cells(count, 1)
        sUser.Cells(rs, 11).Copy _
            Destination:=sExceptions.Cells(count, 2)
        sFullExport.Cells(rC, 1).Copy _
            Destination:=sExceptions.Cells(count, 3)
        sFullExport.Cells(rC, 4).Copy _
            Destination:=sExceptions.Cells(count, 4)