简体   繁体   English

使用excel-vba宏将信息从前6行的特定列复制并粘贴到一个工作簿中

[英]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 sourceColumntargetColumn需要定义为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.

相关问题 如何使用 VBA 宏从一张工作表中复制“特定”行并粘贴到另一个工作表中 - How to copy "specific" rows from one sheet and paste in to another in an excel using VBA Macros Excel VBA-当某些名称与另一本工作簿中的某些名称匹配时,复制并粘贴一系列单元格 - Excel VBA - Copy and paste a range of cells when certain names match from one work book another Excel-VBA将数据从一个工作表复制到另一工作表并粘贴到新行 - Excel-VBA Copy data from one worksheet to another worksheet and paste in new row Excel VBA-将两列从一个工作簿复制并粘贴到另一个工作簿 - Excel VBA - Copy and Paste two columns from one workbook into another Excel-VBA:将列值从一个工作簿复制到另一个工作簿 - Excel-VBA: to copy column value from one workbook to another Excel:将多个行和列从一个工作表复制并粘贴到另一个工作表 - Excel : Copy and paste multiple rows and columns from one worksheet to another Excel VBA如何使用超链接在工作表之间将信息从一个单元格复制到另一个单元格 - Excel VBA how to copy information from one cell to another between work sheets using a hyperlink Excel VBA宏将信息从一本书导入到另一本书 - excel vba macro to import information from one book to another Excel Vba-如何将匹配的行从一个工作表复制并粘贴到另一工作表中的完全匹配的行以下 - Excel Vba - How to copy and paste matched rows from one sheet to below exact matched rows in another sheet 从所有行复制一个工作表,然后粘贴到另一行的另一行中(Excel vba) - Copy from all rows one Worksheet and paste in to Another in alternate rows(Excel vba)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM