如何在Excel中使用VBA将列从一张表复制到另一张?

编程入门 行业动态 更新时间:2024-10-19 20:26:47
本文介绍了如何在Excel中使用VBA将列从一张表复制到另一张?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在尝试编写一个宏,它将第1列的内容从表1复制到表2中。这是模块的外观,但是当我运行它时,我得到

运行时错误9,下标超出范围。

<$ p $选择'选择列1 A1'范围(A1:A3)。选择 Selection.Copy 范围(B1:B3)。选择 ActiveSheet.Paste 表格(Sheet2)选择 Application.CutCopyMode = False End Sub

解决方案

在Excel 2007中,以下功能对我来说是非常正确的。很简单,执行完整的副本(保留所有格式等):

Sheets(Sheet1)。列(1).Copy Destination:= Sheets(Sheet2)。列(2) pre>

列返回一个Range对象,因此这是利用Range.Copy方法。 Destination是此方法的一个选项 - 如果没有提供默认值,则将其复制到粘贴缓冲区。但是当提供时,这是一种简单的复制方式。

当手动复制Excel中的项目时,目标的大小和几何形状必须支持复制的范围。 / p>

I'm trying to write a macro that copies the content of column 1 from sheet 1 to column 2 on sheet 2. This is how the module looks like but, when I run it, I get

Run time error 9, Subscript out of range.

Sub OneCell() Sheets("Sheet1").Select 'select column 1 A1' Range("A1:A3").Select Selection.Copy Range("B1:B3").Select ActiveSheet.Paste Sheets("Sheet2").Select Application.CutCopyMode = False End Sub

解决方案

The following works fine for me in Excel 2007. It is simple, and performs a full copy (retains all formatting, etc.):

Sheets("Sheet1").Columns(1).Copy Destination:=Sheets("Sheet2").Columns(2)

"Columns" returns a Range object, and so this is utilizing the "Range.Copy" method. "Destination" is an option to this method - if not provided the default is to copy to the paste buffer. But when provided, it is an easy way to copy.

As when manually copying items in Excel, the size and geometry of the destination must support the range being copied.

更多推荐

如何在Excel中使用VBA将列从一张表复制到另一张?

本文发布于:2023-11-22 06:41:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616360.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   Excel   VBA

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!