[英]copy and paste information, from specific columns of the first 6 rows, from one work book to another, using excel-vba macros
So, i need to copy information from specific columns of one workbook and paste this information in specific columns of another workbook. 因此,我需要从一个工作簿的特定列中复制信息,并将此信息粘贴到另一工作簿的特定列中。 I have done this successfully by writing the following code: 我通过编写以下代码成功完成了此操作:
Sub test()
Dim wb As Workbook
Dim mysh As Worksheet
Dim sourceColumn As Range
Dim targetColumn As Range
Dim i As Long
Set wb = Workbooks("WorkbookA.xlsm")
'Above code is same as yours
Set mysh = wb.Sheets(1) 'if only one sheet, use loop otherwise
mysh.Range("J1").AutoFilter Field:=10, Criteria1:=">=" & Date
Application.ScreenUpdating = False
Set sourceColumn = Workbooks("WorkbookA.xlsm").Worksheets(1).Columns("D")
Set targetColumn = Workbooks("WorkbookB.xlsm").Worksheets(1).Columns("B")
sourceColumn.Copy Destination:=targetColumn
Set sourceColumn = Workbooks("WorkbookA.xlsm").Worksheets(1).Columns("C")
Set targetColumn = Workbooks("WorkbookB.xlsm").Worksheets(1).Columns("C")
sourceColumn.Copy Destination:=targetColumn
Set sourceColumn = Workbooks("WorkbookA.xlsm").Worksheets(1).Columns("G")
Set targetColumn = Workbooks("WorkbookB.xlsm").Worksheets(1).Columns("D")
sourceColumn.Copy Destination:=targetColumn
Set sourceColumn = Workbooks("WorkbookA.xlsm").Worksheets(1).Columns("J")
Set targetColumn = Workbooks("WorkbookB.xlsm").Worksheets(1).Columns("E")
sourceColumn.Copy Destination:=targetColumn
Set sourceColumn = Workbooks("WorkbookA.xlsm").Worksheets(1).Columns("K")
Set targetColumn = Workbooks("WorkbookB.xlsm").Worksheets(1).Columns("F")
sourceColumn.Copy Destination:=targetColumn
Set sourceColumn = Workbooks("WorkbookA.xlsm").Worksheets(1).Columns("L")
Set targetColumn = Workbooks("WorkbookB.xlsm").Worksheets(1).Columns("G")
sourceColumn.Copy Destination:=targetColumn
Application.ScreenUpdating = True
End Sub
What i specifically want to do is to copy and paste this information from only the first 6 visible records of WorkbookA.These records are not from ( cell numbers 1 to 6) 我特别想做的是仅从WorkbookA的前6个可见记录中复制并粘贴此信息,而这些记录并非来自(单元格编号1至6)
At the moment information from all rows are getting copied and pasted. 目前,所有行中的信息都将被复制和粘贴。
How do i modify the code in an appropriate manner to perform this correctly? 我如何以适当的方式修改代码以正确执行此操作?
Instead of the columns
use range
to define the number of cells that you want to copy. 取而代之的是的columns
使用range
来定义要复制细胞的数量。
Set sourceColumn = Workbooks("WorkbookA.xlsm").Worksheets(1).Range("D1:D6")
Set targetColumn = Workbooks("WorkbookB.xlsm").Worksheets(1).Range("B1:B6")
sourceColumn.Copy Destination:=targetColumn
Using Columns
is not recommend because it's a time-consuming task for VBA. 不建议使用Columns
因为这对于VBA是一项耗时的任务。 A single column can be a collection +1M cells. 单个列可以是+ 1M单元的集合。 That will take Excel a lot of time to process. 这将需要大量的Excel处理时间。
sourceColumn
and targetColumn
need to be defined as range
sourceColumn
和targetColumn
需要定义为range
Edit 1: Now that you have filtering according to your comments, you might want to try this: 编辑1:既然您已经根据评论进行了过滤,则可能需要尝试以下操作:
Define a variable to get the last row number of data from the sourcecolumn. 定义一个变量以从源列中获取数据的最后一行。 Something like for column D: 类似于D列:
Dim LastRow as Long
With Workbooks("WorkbookA.xlsm").Worksheets(1)
LastRow = .Cells(.Rows.count, "D").End(XlUp).Row
End With
Now we're going to get the range of the visible cells from the source column. 现在,我们将从源列中获取可见单元格的范围。 This the step that will just the visible cells after you apply the filter.( I've excluded D1 because it should be the header of your column ) 这是应用滤镜后将仅显示可见单元格的步骤。( 我排除了D1,因为它应该是列的标题 )
Set sourceColumn = Workbooks("WorkbookA.xlsm").Worksheets(1).Range("D2:D" & lastrow).SpecialCells(xlCellTypeVisible)
We'll simply run through the cell collection of sourcecolumn and tell to paste it just 6 cells in the target workbook: 我们将简单地遍历sourcecolumn的单元格集合,并告诉它仅将6个单元格粘贴到目标工作簿中:
Dim counter as integer: counter = 1
With Workbooks("WorkbookB.xlsm").Worksheets(1)
For each cell in sourcecolumn
if counter = 7 then
Exit for
end if
.Range("B" & counter) = cell.value
counter = counter + 1
Next
End With
'Counter = 0 do forget to reset it if you're going to use it for the other columns
Tested and working :) 经过测试和工作:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.