vba 使用vba在excel中将一列复制并粘贴到一行中

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

Copy and Paste a Column into a Row in excel using vba

vbacopy-paste

提问by Mike

I am trying to write a macro that copies a range of cells (AA4:AA15)(e.g. AA4, AA5,AA6...AA15) and pastes these values into a new range (C3:N3)(e.g. C3, D3, E3,...N3). The values are found using a formula. I tried using the code seen below, but it only pasted the first value in my copy range, not all of the values. Any help is appreciated.

我正在尝试编写一个宏来复制一系列单元格 (AA4:AA15)(例如 AA4、AA5、AA6...AA15)并将这些值粘贴到新范围(C3:N3)中(例如 C3、D3、E3 ,...N3)。这些值是使用公式找到的。我尝试使用下面看到的代码,但它只粘贴了我复制范围中的第一个值,而不是所有值。任何帮助表示赞赏。

Range("C3:N3").Value = Range("AA4:AA15").Value

回答by PowerUser

If you did this manually, you would use Paste Special->Transpose. So try:

如果您手动执行此操作,您将使用选择性粘贴->转置。所以尝试:

Sub Macro1()
    Range("AA4:AA15"). Select
    Selection.Copy
    Range("C3").Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=True
    Application.CutCopyMode = False
End Sub

(Note that I'm only selecting the first cell C3, not the entire range C3:N3)

(请注意,我只选择了第一个单元格 C3,而不是整个范围 C3:N3)

Excel has a great macro recorder that can help you learn VBA. Simply turn it on and do some stuff and the recorder will create a VBA macro with those exact actions.

Excel 有一个很棒的宏记录器,可以帮助您学习 VBA。只需打开它并做一些事情,记录器就会创建一个包含这些确切动作的 VBA 宏。