复制单元格范围和粘贴到第一个空行更有效的代码?高强 [英] copying range of cells and pasting to the first empty row more effective code? Excel

查看:121
本文介绍了复制单元格范围和粘贴到第一个空行更有效的代码?高强的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个宏,从新搜索中选择单元格,并将范围粘贴到过去搜索中的第一个打开的单元格我认为我的代码非常无效。有人有什么想法可以改进吗?

  Sub Macro5()
范围(A3:J3)选择
范围(选择,选择
Selection.Copy
表格(过去搜索)选择
范围(A1)。End(xlDown).Offset(1,0 )。选择
ActiveSheet.Paste
工作表(新建搜索)。激活
Application.CutCopyMode = False
End Sub
/ pre>

解决方案

首先,您不必使用。选择或。激活您将操纵的单元格/工作表只需直接引用它们。这样可以减慢你的代码。



.Copy方法可以使用Destination参数,因此您不必在其他地方使用.Paste。



此外,.End属性不是很可靠。最好使用UsedRange代替。



你可以在只有一行中做到这一点。

  Worksheets(New Search)。Range(A3:J3)。复制目的地:=工作表(过去搜索)UsedRange.Columns(1).Offset(1,0)


I have a Macro that takes a selection of cells from "New Search" and paste the range to the first open cell in "Past Searches" I'm thinking that my code is very ineffective. Does anyone have any ideas of how I can improve this?

Sub Macro5()
Range("A3:J3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Past Searches").Select
Range("A1").End(xlDown).Offset(1, 0).Select
ActiveSheet.Paste
Worksheets("New Searches").Activate
Application.CutCopyMode = False
End Sub

解决方案

First, you don't have to use .Select or .Activate on cells/worksheets that you'll manipulate, just reference them directly. That can really slow down your code.

The .Copy method can take a Destination parameter, so you don't have to use .Paste somewhere else.

Also, the .End property isn't very reliable. It's better to use UsedRange instead.

You can do that pretty much in only 1 line.

Worksheets("New Search").Range("A3:J3").Copy Destination:=Worksheets("Past Searches").UsedRange.Columns(1).Offset(1, 0)

这篇关于复制单元格范围和粘贴到第一个空行更有效的代码?高强的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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