简体   繁体   English

通过VBA在Excel中复制数据

[英]Copying the data in Excel through VBA

As you can see down, I am copy 4 columns of data from one workbook to another. 正如您所看到的,我将4列数据从一个工作簿复制到另一个工作簿。 I am stuck at a case where the destination has 8 columns and my area of columns are 1,2,5,7. 我被困在一个目的地有8列,我的区域是1,2,5,7的情况下。 can you suggest me some changes in the code please. 你能告诉我代码中的一些变化吗? The one below will work only for first 4 columns. 下面的内容仅适用于前4列。 Thanks. 谢谢。

Sub Copymc()

Dim x As Workbook
Dim y As Workbook
Dim rng As Range

Set x = Workbooks.Open("H:\testing\Q4 2014\US RMBS Q4.xlsx")
Set y = Workbooks.Open("H:\testing\demo\test1.xlsx")

Dim LastRow As Long
Dim NextRow As Long

x.Worksheets("RL Holdings").Activate
Range("A65536").Select
ActiveCell.End(xlUp).Select
LastRow = ActiveCell.Row
Range("A2:D" & LastRow).Copy   

y.Worksheets("Sheet1").Range("a65536").End(xlUp).Offset(1, 0)
Application.CutCopyMode = False

End sub

The line Range("A2:D" & LastRow).Copy has column D hardcoded into it. 行范围(“A2:D”和LastRow).Copy将D列硬编码到其中。 This means that it will always copy A2 to D65536. 这意味着它将始终将A2复制到D65536。 If you want specific columns(A, B, E, G) then I would recommend simply repeating your code for each column. 如果你想要特定的列(A,B,E,G),那么我建议你只需重复每列的代码。

For example 例如

Range("A65536").Select
ActiveCell.End(xlUp).Select
Selection.Copy
y.Worksheets("Sheet1").Range("a65536").End(xlUp).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

then copy the code four times and replace the A to whatever column you want to copy or paste to. 然后复制代码四次,并将A替换为要复制或粘贴到的任何列。 If this isn't what you are looking for please elaborate on what you want. 如果这不是您想要的,请详细说明您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM