找不到对象-将Excel范围从一张纸复制到另一张纸

时间:2018-09-10 09:56:24

标签: excel vba

Private Sub CommandButton1_Click()

    Dim cel As Range, lRow As Long

    'next line determines the last row in column 1 (A), of the first Worksheet
    lRow = Worksheets("Delta").UsedRange.Columns(5).Rows.Count

    'iterate over every cell in the UsedRange 
    For Each cel In Worksheets("Delta").Range("E10:E" & lRow)

        'cel represents the current cell
        'being processed in this iteration of the loop

        'Len() determines number of characters in the cell
        If Len(cel.Value2) > 0 Then

            'if cel is not empty, copy the value to the cell range (D1,D2,D3...) mentioned

            Sheets("Traceability").Select
            Traceability.Range("D3:D100").Select = cel.Value2    '--->Object not defined

        End If

    Next    'move on the next (lower) cell in column 1

End Sub

对于复制一系列数据,我遇到未定义对象的错误。我复制单元格值的方法正确吗?

2 个答案:

答案 0 :(得分:1)

这就是我最终想到的

Private Sub CommandButton1_Click()

    Dim cel As Range, lRow As Long
    Dim i As Integer


    lRow = Worksheets("Delta").UsedRange.Columns(5).Rows.Count
    rw = 3
    'iterate over every cell in the UsedRange
    For Each cel In Worksheets("Delta").Range("E10:E" & lRow)
    If Len(cel.Value2) > 0 Then

        'if cel is not empty, copy the value to the cell
        Sheets("Traceability").Range("D" & rw).Value = cel.Value2
        rw = rw + 1
    End If
    Next

End Sub

答案 1 :(得分:0)

尝试: 删除:

Sheets("Traceability").Select

更改:

Traceability.Range("D3:D100").Select = cel.Value2

 Sheets("Traceability").Range("D3:D100") = cel.Value2

已经有一段时间了,因为我必须这样做,但是如果我没记错的话,选择工作表不会将其分配给变量。 您已经选择了“可跟踪性”工作表,然后尝试在“可跟踪性”上执行操作而不告诉它“可跟踪性”是什么。 如果这样的话。