input框范围归因问题

我一直在试图build立一个实用工具,允许用户select列(或行)中的公式,并使用PasteSpecialTranspose方法将这些公式转换为行(或列)。 我有一个特定的目标单元格的工作,但只要我试图让用户控制什么单元格的目标,我收到一个运行时错误1004“的PasteSpecial方法的范围类失败”。

这里是工作代码(假定没有选定的公式在Range("E3")

 Dim rSelection As Range Set rSelection = Selection Call MakeSelectionAbsolute(rSelection) rSelection.Copy Dim rMoveTo As Range Set rMoveTo = Range("E3") With rMoveTo .PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _ False, Transpose:=True Call MakeSelectionRelative(Selection) End With rSelection.Delete 

我已经尝试了几种方法(包括将返回值的UDF),但我认为这个问题源于使用input框。 我目前的尝试是用这个replaceSet rMoveTo = Range("E3")

  On Error Resume Next Application.DisplayAlerts = False Set rMoveTo = Application.InputBox(Prompt:= _ "Select the first cell of the range you want to transpose to.", _ Title:="SPECIFY RANGE", Type:=8) Application.DisplayAlerts = True On Error GoTo 0 

在我看来,这样的替代应该起作用。 我尝试在rMoveTo上添加一个手表,它看起来好像为我select的单元格设置了一个值(但我承认我还不知道如何读取范围内的所有手表数据)。

Run-tim错误1004发生在.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True

为什么这种方法不起作用,如何解决? 这似乎是一件简单的事情,但我对此感到茫然。

剪贴板对UI操作非常敏感。 当您使用Application.InputBox剪贴板select范围时,清除并粘贴特定失败,因为没有粘贴。

更改您的操作顺序:首先select范围,然后复制/粘贴:

 Dim rSelection As Range Set rSelection = Selection Call MakeSelectionAbsolute(rSelection) On Error Resume Next Application.DisplayAlerts = False Set rMoveTo = Application.InputBox(Prompt:= _ "Select the first cell of the range you want to transpose to.", _ title:="SPECIFY RANGE", Type:=8) Application.DisplayAlerts = True On Error GoTo 0 'if user select nothing - exit sub If rMoveTo Is Nothing Then Exit Sub 'copy AFTER user select range rSelection.Copy With rMoveTo .PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _ False, Transpose:=True Call MakeSelectionRelative(Selection) End With rSelection.Delete