VBA 复制和转置数据范围

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27695660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 05:39:49  来源:igfitidea点击:

VBA Copy & Transpose Data Range

excelvbaexcel-vba

提问by FH Yousif

I am working on setuping a few spreadsheets at work to streamline my work. I'm still new to VBA.

我正在工作中设置一些电子表格以简化我的工作。我还是 VBA 的新手。

I am trying to cut a range of data in a column (E6:E14) from Sheet1 and transposing the data before pasting the data in the next available row in Column A of Sheet2. Here is the code that I have written so far from trial and error. Everytime I run the code I get a Run-time error '1004'. I am trying to create a "database" in Sheet2. Any help is appreciate it.

我试图从 Sheet1 中的列 (E6:E14) 中剪切一系列数据并在将数据粘贴到 Sheet2 的 A 列中的下一个可用行之前转置数据。这是我迄今为止从反复试验中编写的代码。每次运行代码时,都会收到运行时错误“1004”。我正在尝试在 Sheet2 中创建一个“数据库”。任何帮助都是感激的。

Sub Test()
    Worksheets("Sheet1").Range("E6:E14").Cut
    Worksheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Transpose:=True
End Sub

Thanks for the help!

谢谢您的帮助!

FHY

FHY

采纳答案by iliketocode

PasteSpecial is unavailable with the .Cut method, but not the .Copy method. When I changed

PasteSpecial 不适用于 .Cut 方法,但不适用于 .Copy 方法。当我改变

Worksheets("Sheet1").Range("E6:E14").Cut

to

Worksheets("Sheet1").Range("E6:E14").Copy

everything worked fine. If you want everything deleted afterwards, you could always just do:

一切正常。如果您想之后删除所有内容,您可以随时执行以下操作:

Sub Test()

Worksheets("Sheet1").Range("E6:E14").Copy

Worksheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Transpose:=True

Worksheets("Sheet1").Range("E6:E14").Clear 

End Sub