简体   繁体   English

VBA Excel标记选择,但复制下一列

[英]VBA Excel Mark selection but copy next column

I want to copy range from B2 to the last filled in Cell in the column. 我想将范围从B2复制到该列中最后一个填写的单元格中。 However there are breaks in the column that causes the xlDown function to stop prematurely, therefore I want to copy column A from A2 to the last filled in cell instead, since column A doesn't have any breaks in it. 但是,列中有中断导致xlDown函数过早停止,因此我想将A2列中的A复制到最后一个填充的单元格中,因为A列中没有任何中断。

Is it possible to somehow do this, and then offset the selected Range by One column, giving me the data I am after? 是否可以执行此操作,然后将选定的“范围”偏移一列,从而获得我想要的数据?

I am thinking something like this: 我在想这样的事情:

Worksheets("Sheet1").Range("A2").End (xlDown)

But how do I cause an offset so I end up with Range B2:xlDown (but the xlDown is the position of the previous selection of column A)? 但是,如何引起偏移,所以最终出现范围B2:xlDown(但是xlDown是列A的上一个选择的位置)?

Thanks, 谢谢,

Use this: 用这个:

With Worksheets("Sheet1")

.Range("A2", .Range("A2").End(xlDown)).Offset(0, 1).Select

End With

You can use Offset Function, to shift the Column. 您可以使用偏移功能来移动列。 Also Change Select to Copy or whatever else do you want. 同样,将“ Select更改为“复制”或其他所需的内容。

Don't go down from the top, go up from the bottom: 不要从上往下走,从下往上走:

Worksheets("Sheet1").Range(Worksheets("Sheet1").Cells(2,2), Worksheets("Sheet1").Cells(Worksheets("Sheet1").Rows.Count,2).End(xlUp)).Copy

We use Worksheets("Sheet1").Range( to define a range by 2 corners of a rectangle. Our first corner is Cell B2, aka Row 2 Column 2: Worksheets("Sheet1").Cells(2,2) 我们使用Worksheets("Sheet1").Range(定义矩形的2个角的范围。我们的第一个角是单元格B2,又名第2行第2列2: Worksheets("Sheet1").Cells(2,2)

The other corner will be the bottommost cell in Column B with data in it. 另一个角将是B列中最底部的单元格,其中包含数据。 So, we start with the very bottom of the entire column, and move up until we hit data: 于是,我们开始与整个塔的最底部 ,并移动直到我们打数据:

Worksheets("Sheet1").Cells(Worksheets("Sheet1").Rows.Count,2).End(xlUp)

Then, we copy the resulting rectangular range: ).Copy 然后,我们复制生成的矩形范围).Copy

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

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