我想将一系列单元格(仅限值/文本)复制到剪贴板,这样用户只有在将它们粘贴到另一个电子表格时才需要粘贴特殊值。
这是我到目前为止所做的:
Private Sub CommandButton1_Click()
With New DataObject
.SetText Range("A32:Q32").Text
.PutInClipboard
End With
'Range("A32:Q32").Copy
End Sub
这给了我一个运行时错误
94无效使用Null
如果我只使用已注释的代码Range.("A32:Q32").Copy
,则会复制公式,除非用户执行特殊粘贴,否则会出现各种参考错误。
答案 0 :(得分:4)
这有点令人费解,但获取文字>清除剪贴板>放回文字:
[A32:Q32].Copy
With New DataObject
.GetFromClipboard
s = .GetText
.Clear
.SetText s
.PutInClipboard
End With
当范围中的单个单元格文本不同时, Range.Text
会返回Null
。
答案 1 :(得分:2)
我不知道dataobject,所以我建议让用户选择目标单元格
Private Sub CommandButton1_Click()
Dim userRng As Range
With ActiveSheet 'reference currently active sheet, before the user could change it via inputbox
Set userRange = GetUserRange()
If Not userRange Is Nothing Then ' if the user chose a valid range
With .Range("A32:Q32")
userRange.Resize(.Rows.Count, .Columns.Count).Value =.Value ' paste values only
End With
End If
End With
End Sub
Function GetUserRange() As Range
' adapted from http://spreadsheetpage.com/index.php/tip/pausing_a_macro_to_get_a_user_selected_range/
Prompt = "Select a cell for the output."
Title = "Select a cell"
' Display the Input Box
On Error Resume Next
Set GetUserRange = Application.InputBox( _
Prompt:=Prompt, _
Title:=Title, _
Default:=ActiveCell.Address, _
Type:=8) 'Range selection
' Was the Input Box canceled?
If GetUserRange Is Nothing Then MsgBox “Canceled!”
End Function