[英]Copy data from one sheet to another using VBA
在 excel 2007 中尝试循环遍历(非固定长度)列(例如 C)和行值匹配的位置(例如“高”),然后将单元格 Dx 和 Bx 的值转移到新行中的“转移”表中,其中 x 是找到匹配项的行号。 假设存在“转移”。
到目前为止,我有这个:
Public Sub CopyRows()
Sheets("Sheet1").Select
'Find the last row of data
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
'Loop through each row
For x = 1 To FinalRow
'Decide if to copy based on column C
ThisValue = Cells(x, 3).Value
If ThisValue = "High" Then
Cells(x, 1).Resize(1, 33).Copy
Sheets("Transfer").Select
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(NextRow, 1).Select
ActiveSheet.Paste
Sheets("Sheet1").Select
End If
Next x
End Sub
我不会那样做 go 。 我相信首先设置过滤器并仅复制可见行会更快并且更容易编码。
事实上,最好使用高级过滤器来完成。
创建两个范围 - 一个用于条件(标题和单元格):
*C column header* | ------------------- High |
一个用于所需数据(仅标题):
*B column header* | *D column header* -------------------------------------
然后使用
range("A:D").advancedfilter xlFilterCopy, range("criteria range"), range("copy range")
显然,使用 Excel 接口(数据 - 高级过滤器)更容易做到这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.