ActiveSheet.PasteSpecial 有效,Transpose 出错:=True

ActiveSheet.PasteSpecial works, error with Transpose:=True

我正在做一个宏,复制当前活动工作表中的选择并将其粘贴到另一个工作表中,并进行转置。我看过与类似问题相关的问题,但似乎都复杂得多,而这个问题对我来说似乎很简单(而且令人费解)。

以下代码有效(没有转置):

Sub sbCopyRangeToAnotherSheet2()
  Selection.Copy
  Sheets("snippets").Activate
  ActiveSheet.PasteSpecial 
End Sub

下面的代码抛出一个

1004 error - application-defined or object-defined error

(仅更改:添加转置):

Sub sbCopyRangeToAnotherSheet2()
  Selection.Copy
  Sheets("snippets").Activate
  ActiveSheet.PasteSpecial transpose:=true
End Sub

它不起作用,因为 Worksheet.PasteSpecial 方法没有 Transpose 参数。 请注意,有 2 种不同的 PasteSpecial 方法:

  1. Range.PasteSpecial method

    PasteSpecial(Paste, Operation, SkipBlanks, Transpose)
    
  2. Worksheet.PasteSpecial method

    PasteSpecial(Format, Link, DisplayAsIcon, IconFileName, IconIndex, IconLabel, NoHTMLFormatting)
    

并且您使用了第二个(没有 Transpose 参数,因此有错误)。

正确的语法是:

Option Explicit

Public Sub sbCopyRangeToAnotherSheet2()
    Selection.Copy
    ThisWorkbook.Sheets("snippets").Range("A1").PasteSpecial Transpose:=True
End Sub

您可能会从阅读中受益 How to avoid using Select in Excel VBA.