Vba 将特定单元格从一个区域复制到另一个区域的不同位置

Vba 将特定单元格从一个区域复制到另一个区域的不同位置,vba,copy,paste,listobject,Vba,Copy,Paste,Listobject,我需要将值从一个表上的单元格复制到另一个表上的单元格。单元格行由变量定义,而列不是。我相信我只需要以下代码的底线的帮助。一旦解决,我将多次复制该解决方案,将几个不同的单元格复制到新位置 Sub Transition_Queue_to _NPD() Dim QueueSheet As Worksheet Set QueueSheet = ThisWorkbook.Worksheets("Project Queue") Dim TableQueue As ListObjec

我需要将值从一个表上的单元格复制到另一个表上的单元格。单元格行由变量定义,而列不是。我相信我只需要以下代码的底线的帮助。一旦解决,我将多次复制该解决方案,将几个不同的单元格复制到新位置

Sub Transition_Queue_to _NPD()
    Dim QueueSheet As Worksheet
    Set QueueSheet = ThisWorkbook.Worksheets("Project Queue")

    Dim TableQueue As ListObject
    Set TableQueue = QueueSheet.ListObjects("TableQueue")

    Dim TransColumn As Range
    Set TransColumn = QueueSheet.Range("TableQueue[Transition]")

    Dim TransCell As Range
    Dim TransQty As Double

For Each TransCell In TransColumn
    If Not IsEmpty(TransCell.Value) Then
        TransQty = TransQty + 1
    End If
Next TransCell


If TransQty > 0 Then
    Dim Trans_Queue_Row As Range
    Dim i As Integer

With TransColumn
    For i = 1 To .Count
        If InStr(1, .Rows(i).Value, "NPD") > 0 Then
            Set Trans_Queue_Row = TableQueue.DataBodyRange.Rows(i)
        End If

            Dim NPDSheet As Worksheet
            Set NPDSheet = ThisWorkbook.Worksheets("NPD")

            Dim TableNPD As ListObject
            Set TableNPD = NPDSheet.ListObjects("TableNPD")

            Dim Trans_NPD_Row As ListRow
            Set Trans_NPD_Row = TableNPD.ListRows.Add
'Here is where I need help.  I need to copy individual cells from Trans_Queue_Row to Trans_NPD_Row.  I have tried copying the cell in Column 2 to the cell in Column 1 via the following with no success.

            Cells(Trans_Queue_Row, 2).Value = Cells(Trans_NPD_Row, 1).Value

    Next i
End With
End If
End Sub
我一直收到一个错误,说
类型不匹配

这里有一种方法:

Sub Transition_Queue_to_NPD()

    Dim TableQueue As ListObject, TableNPD As ListObject, i As Long
    Dim TransColumn As Range, Trans_Queue_Row As Range, Trans_NPD_Row As Range

    Set TableQueue = ThisWorkbook.Worksheets("Project Queue").ListObjects("TableQueue")
    Set TableNPD = ThisWorkbook.Worksheets("NPD").ListObjects("TableNPD")

    Set TransColumn = TableQueue.ListColumns("Transition").DataBodyRange

    For i = 1 To TransColumn.Cells.Count
        If InStr(1, TransColumn.Cells(i).Value, "NPD") > 0 Then

            'get the source and destination row ranges
            Set Trans_Queue_Row = TableQueue.DataBodyRange.Rows(i)
            Set Trans_NPD_Row = TableNPD.ListRows.Add.Range

            Trans_NPD_Row.Cells(2).Value = Trans_Queue_Row.Cells(1).Value
            Trans_NPD_Row.Cells(3).Value = Trans_Queue_Row.Cells(4).Value
            'etc etc

        End If
    Next i

End Sub
这里有一种方法:

Sub Transition_Queue_to_NPD()

    Dim TableQueue As ListObject, TableNPD As ListObject, i As Long
    Dim TransColumn As Range, Trans_Queue_Row As Range, Trans_NPD_Row As Range

    Set TableQueue = ThisWorkbook.Worksheets("Project Queue").ListObjects("TableQueue")
    Set TableNPD = ThisWorkbook.Worksheets("NPD").ListObjects("TableNPD")

    Set TransColumn = TableQueue.ListColumns("Transition").DataBodyRange

    For i = 1 To TransColumn.Cells.Count
        If InStr(1, TransColumn.Cells(i).Value, "NPD") > 0 Then

            'get the source and destination row ranges
            Set Trans_Queue_Row = TableQueue.DataBodyRange.Rows(i)
            Set Trans_NPD_Row = TableNPD.ListRows.Add.Range

            Trans_NPD_Row.Cells(2).Value = Trans_Queue_Row.Cells(1).Value
            Trans_NPD_Row.Cells(3).Value = Trans_Queue_Row.Cells(4).Value
            'etc etc

        End If
    Next i

End Sub

Trans\u Queue\u Row
是一个范围,因此
单元格(Trans\u Queue\u Row,2)的值不正确,因为范围不是行引用。您可能需要
单元格(Trans\u Queue\u Row.Row,2)。Value
我做了更改。现在我得到一个错误,它突出显示了整个代码的最后一行,并表示
对象不支持此属性或方法
?可能与
Trans\u NPD\u Row
的情况相同--同样,您需要
.Row
它,因为您在两个不同的工作表之间工作,请确保
单元格
(在LHS和RHS中)语句的一侧是完全限定的或更简单的:
Trans_-Queue_-Row.Cells(2,1).Value=Trans_-NPD_-Row.Cells(1,1).Value
,尽管根据您在代码中的评论“我需要帮助…”,我认为这两个是反向的。因此它可能是
Trans_-NPD_-Row.Cells(1,1).Value=Trans-u-Row.Cells(2,1).Value
将Trans_队列中的值放入Trans_NPD
Trans_队列行
是一个范围,因此
单元格(Trans_队列行,2)。Value
不正确,因为范围不是行引用。您可能需要
单元格(Trans_队列行,2).Value
我做了更改。现在我得到一个错误,它突出显示了整个代码的最后一行,并说
对象不支持此属性或方法
?可能与
Trans\u NPD\u Row
的情况相同——同样,您需要
.Row
它,因为您在两个不同的工作表之间工作,请确保该语句的
单元格
(在LHS和RHS上)是完全限定的,或者更简单地说:
Trans\u Queue\u Row.Cells(2,1).Value=Trans\u NPD\u Row.Cells(1,1).Value
,尽管根据您在代码中的注释“我需要帮助…”,但我认为这两个单元格是反向的。因此它可能
Trans NPD\u Row.Cells(1,1).Value=Trans_Queue_Row.Cells(2,1).Value
将Trans_Queue中的值放入Trans_NPD