Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 如何将特定列从一行复制到另一个工作表_Vba_Excel - Fatal编程技术网

Vba 如何将特定列从一行复制到另一个工作表

Vba 如何将特定列从一行复制到另一个工作表,vba,excel,Vba,Excel,基本上,我正在编译一份报告,可以按正确的顺序将每一行复制到另一张表中,但我不需要每一行的每一列。我只需要每行42列中的3列就可以转移到新工作表。以下是我目前的代码: For k = 2 To coor2(2) If Cells(k, 5).Value > Cells(k, 16).Value Then Cells(k, 5).EntireRow.Copy Sheets("test").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)

基本上,我正在编译一份报告,可以按正确的顺序将每一行复制到另一张表中,但我不需要每一行的每一列。我只需要每行42列中的3列就可以转移到新工作表。以下是我目前的代码:

For k = 2 To coor2(2)
 If Cells(k, 5).Value > Cells(k, 16).Value Then
 Cells(k, 5).EntireRow.Copy Sheets("test").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
 End If
Next k

蒂姆几乎有复制单个细胞的要领。。。将列出我的评论中列出的所有3个变体

1) 对于整行:

Sheets("Source").Rows(k).Copy Sheets("Destination").Rows(lastrow+1)
2) 对于特定单元格(或非连续范围)

3) 对于连续范围

Sheets("Source").Range(Sheets("Source").Cells(k,3),Sheets("Source").Cells(k,7)).Copy Sheets("Destination").Range(Sheets("Destination").Cells(k,1),Sheets("Destination").Cells(k,5))

在每个场景中,假设您之前找到了目标工作表的最后一行,并粘贴到最后一行之后的行中。

因此您得到了代码。。。你的问题(标题)在比较两者时没有多大意义。您是希望根据一对单元格值复制一行,还是希望复制一个连续的单元格区域?行(k)。复制前面的语句,复制中间的语句,或者您需要为后面的语句指定源和目标范围。忽略If语句,这部分就可以了。我需要的帮助是弄清楚如何复制该行中的特定单元格,而不是整个行。
Sheets("Source").Cells(k,3).Copy Sheets("Destination").Cells(lastrow+1,1)
Sheets("Source").Cells(k,5).Copy Sheets("Destination").Cells(lastrow+1,2)
Sheets("Source").Cells(k,7).Copy Sheets("Destination").Cells(lastrow+1,3)
Sheets("Source").Range(Sheets("Source").Cells(k,3),Sheets("Source").Cells(k,7)).Copy Sheets("Destination").Range(Sheets("Destination").Cells(k,1),Sheets("Destination").Cells(k,5))