复制直到最后一条数据

发布于 2024-10-22 03:50:05 字数 831 浏览 5 评论 0原文

是否可以选择一个范围来复制到最后一项?

Sheets("Design").Select
Range("A1:C200").Copy

'My data only runs until E48 and Im left with 152 blancs.

现在它从 A1 复制到 E200,但是我如何编辑上面的代码,以便它只选择直到最后一条数据,在我的例子中是 E48? (这是可变的)

谢谢!

@Jean

在我的 Excel 工作表中,我有从 A1-A18 运行的数据,B 为空,C1-C2 。现在我想复制所有包含值的单元格。

 With Range("A1")
     Range(.Cells(1, 1), .End(xlDown).Cells(20, 3)).Copy
 End With

这将复制 A1-C20 中的所有内容,但我只希望 A1-A18 和 C1-C2 看起来好像它们包含数据。但它需要以一种方式形成,一旦我在 B 中有数据或者我的范围扩展,这些数据也会被复制。

这也许更清楚一点吗?

关于复制/粘贴情况;

我目前使用这个粘贴

appWD.Selection.PasteSpecial ' So it copies the formats too?
'In your case, do you mean I should do it like this?
Range(.Cells(1, 1), .End(xlDown).Cells(20, 3)).Copy Destination:=appWD.Selection.PasteSpecial

Is it possible to select a range to copy to the last item?

Sheets("Design").Select
Range("A1:C200").Copy

'My data only runs until E48 and Im left with 152 blancs.

So now it copies from A1 to E200, but how can I edit the above code so it only selects until the last piece of data, which in my case is E48? (this is variable)

Thanks!

@Jean

In my excel sheet I have data running from A1-A18, B is empty and C1-C2. Now I would like to copy all the cells that contain a value.

 With Range("A1")
     Range(.Cells(1, 1), .End(xlDown).Cells(20, 3)).Copy
 End With

This will copy everything from A1-C20, but I only want A1-A18 and C1-C2 seen as though these contain data. But it needs to be formed in a way that once I have data in B or my range extends, that these get copied too.

Is this perhaps a bit clearer?

About the Copy/Paste situation;

I currently paste using this

appWD.Selection.PasteSpecial ' So it copies the formats too?
'In your case, do you mean I should do it like this?
Range(.Cells(1, 1), .End(xlDown).Cells(20, 3)).Copy Destination:=appWD.Selection.PasteSpecial

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你的往事 2024-10-29 03:50:05

这是有效的:

With Range("A1")
    Range(.Cells(1, 1), .End(xlDown).Cells(1, 5)).Copy
End With

如果您需要不同的列数,请更改“5”。

另外,我通过惨痛的教训学到了这一点:如果可能的话,避免复制/粘贴!复制和粘贴使用剪贴板。当您的代码运行时,其他程序可能会读取/写入剪贴板,这将导致疯狂的、不可预测的结果。但是,在同一行上复制到目标范围是安全的,即执行此操作

Range(.Cells(1, 1), .End(xlDown).Cells(1, 5)).Copy myDestinationRange

而不是执行此操作

Range(.Cells(1, 1), .End(xlDown).Cells(1, 5)).Copy
myDestinationRange.Paste

。后者使用剪贴板,而前者不使用剪贴板。

This works:

With Range("A1")
    Range(.Cells(1, 1), .End(xlDown).Cells(1, 5)).Copy
End With

Change the "5" if you need a different number of columns.

Also, this I learned the hard way: Avoid Copy/Paste if at all possible! Copy and Paste use the clipboard. Other programs may read from / write to the clipboard while your code is running, which will cause wild, unpredictable results. However, it's safe to copy to your target range on the same line, i.e. do this

Range(.Cells(1, 1), .End(xlDown).Cells(1, 5)).Copy myDestinationRange

and not this

Range(.Cells(1, 1), .End(xlDown).Cells(1, 5)).Copy
myDestinationRange.Paste

The latter uses the clipboard while the former does not.

影子的影子 2024-10-29 03:50:05

这应该有效:

a1 = Range("a1").Address
lastcell = Range("E1").End(xlDown).Address
Range(a1, lastcell).Copy

This should work:

a1 = Range("a1").Address
lastcell = Range("E1").End(xlDown).Address
Range(a1, lastcell).Copy
南…巷孤猫 2024-10-29 03:50:05

如果这适合您,我最喜欢的是 CurrentRegion:

Range("A1").CurrentRegion.Copy Destination:=Sheet2.Range("a1")

If that suits you, my favorite is CurrentRegion:

Range("A1").CurrentRegion.Copy Destination:=Sheet2.Range("a1")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文