Excel VBA - Range.Copy 转置粘贴

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8852717/
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-08 12:25:33  来源:igfitidea点击:

Excel VBA - Range.Copy transpose paste

excel-vbatransposevbaexcel

提问by user1130306

I'm trying to something very simple, but I seem to be stuck. I am following the help menu for PasteSpecialbut I cannot seem to get my code to work without an error.

我正在尝试一些非常简单的事情,但我似乎被卡住了。我正在关注帮助菜单,PasteSpecial但我似乎无法让我的代码正常工作而不出错。

I want to take Worksheets("Sheet1").Range("A1","A5")and paste transpose to Worksheets("Sheet2").Range("A1","E1").

我想将Worksheets("Sheet1").Range("A1","A5")转置粘贴到Worksheets("Sheet2").Range("A1","E1").

What is the most simple way to accomplish this?

实现这一目标的最简单方法是什么?

回答by GSerg

Worksheets("Sheet1").Range("A1:A5").Copy
Worksheets("Sheet2").Range("A1").PasteSpecial Transpose:=True

回答by GollyJer

Here's an efficient option that doesn't use the clipboard.

这是一个不使用剪贴板的有效选项。

Sub transposeAndPasteRow(rowToCopy As Range, pasteTarget As Range)
    pasteTarget.Resize(rowToCopy.Columns.Count) = Application.WorksheetFunction.Transpose(rowToCopy.Value)
End Sub

Use it like this.

像这样使用它。

Sub test()
    Call transposeAndPasteRow(Worksheets("Sheet1").Range("A1:A5"), Worksheets("Sheet2").Range("A1"))
End Sub