【发布时间】:2019-11-22 14:24:21
【问题描述】:
我需要将值从一个表格的单元格复制到另一个表格的单元格。单元格行由变量定义,列不是。我相信我只需要以下代码的底线的帮助。解决后,我将多次复制该解决方案,将几个不同的单元格复制到新位置。
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
我不断收到一条错误消息,提示 Type mismatch。
【问题讨论】:
-
Trans_Queue_Row是一个范围,所以Cells(Trans_Queue_Row, 2).Value不正确,因为范围不是行引用。你可能需要Cells(Trans_Queue_Row.row, 2).Value -
我做出了改变。现在我收到一个错误,它突出显示整个最后一行代码并说
Object doesn't support this property or method? -
可能与
Trans_NPD_Row相同——同样,您需要.Row它,因为您在两张不同的工作表之间工作,请确保Cells(该语句的 LHS 和 RHS) 方面都是完全限定的 -
或者更简单地说:
Trans_Queue_Row.Cells(2,1).Value = Trans_NPD_Row.Cells(1,1).Value,尽管根据您在代码中的评论“这是我需要帮助的地方...”,我认为您有这两个倒退。 -
所以
Trans_NPD_Row.Cells(1,1).Value = Trans_Queue_Row.Cells(2,1).Value可能会将 Trans_Queue 中的值放入 Trans_NPD
标签: vba copy paste listobject