如何为“复制列vba”指定开始行? [英] How to specify start row for a "copy column vba"?

查看:454
本文介绍了如何为“复制列vba”指定开始行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下字符串将列从一张纸复制到另一张纸

I'm using the following string to copy a column from one sheet to another

Dim wsCore As Worksheet
Dim wsData As Worksheet

Set wsCore = Sheets("R2 Data Dump")
Set wsData = Sheets("Active")

Dim rowNumber As Integer
Dim cellFormulas As Variant
Dim cellFormulas1 As Variant
Dim counter As Integer
    counter = 0
Dim currentCell As String
Dim importSheetCell As Variant
Dim importSheetOffset As Variant
Dim contractnum As Integer
Dim pt As PivotTable
Dim ws As Worksheet
Dim pc As PivotCache
Dim lastrow As Long

Set wsCore = Sheets("R2 Data Dump")
Set wsData = Sheets("Active")

    Sheets("R2 Data Dump").Visible = True
    Sheets("R2 Data Dump").Select
    rowNumber = ActiveSheet.UsedRange.Rows.Count

 With wsCore
 Startrow = 3

    wsCore.Columns("W").Copy Destination:=wsData.Columns("A")
    wsCore.Columns("B").Copy Destination:=wsData.Columns("B")
    wsCore.Columns("C").Copy Destination:=wsData.Columns("C")
    wsCore.Columns("D").Copy Destination:=wsData.Columns("D")

End With.






所以这样做是从一张纸并将其粘贴到目标纸的指定列上。我的问题是,该操作开始将复制的单元格从源工作表粘贴到目标单元格的第一行。我想发现的是如何设置行号,而不是行1。


So what is does is copy the particular columns from one sheet and paste it on the specified column of the destination sheet. My problem is, the action starts pasting the copied cell from the source sheet to the first row on the destination cell. What I would like to find out is how can I set a row number where it would start pasting instead of row 1.

推荐答案

通常,您将转到列中的第一个空白单元格(从下往上看)。

Typically, you would go to the first blank cell in the column (looking from the bottom up).

With Intersect(wsCore.Columns("A:W"), wsCore.UsedRange)
    .Columns("W").Copy Destination:=wsData.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
    .Columns("B").Copy Destination:=wsData.Cells(Rows.Count, "B").End(xlUp).Offset(1, 0)
    .Columns("C").Copy Destination:=wsData.Cells(Rows.Count, "C").End(xlUp).Offset(1, 0)
    .Columns("D").Copy Destination:=wsData.Cells(Rows.Count, "D").End(xlUp).Offset(1, 0)
End With

如果要指定一行,则声明一个长变量并使用它。

If you wanted to specify a row, declare a long variable and use that.

Dim rw As Long
rw = 10
With Intersect(wsCore.Columns("A:W"), wsCore.UsedRange)
    .Columns("W").Copy Destination:=wsData.Cells(rw, "A")
    .Columns("B").Copy Destination:=wsData.Cells(rw, "B")
    .Columns("C").Copy Destination:=wsData.Cells(rw, "C")
    .Columns("D").Copy Destination:=wsData.Cells(rw, "D")
End With

我使用了 Intersect 来防止试图将完整的列复制到不是从第一个开始的列中行。完整的列不适合。

I've used Intersect to guard against trying to copy a full column into a column that doesn't start at the first row. A full column won't fit.

这篇关于如何为“复制列vba”指定开始行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆