我希望将Excel中的默认粘贴设置为仅值或匹配目标格式。我知道如何为每个宏单独创建一个宏,但只有当您从工作簿中的单元格中复制时,xlPasteValues才能工作,而匹配目标格式则不起作用(但它在从网页复制时确实有效,这正是我想要的)。我的目标是创建一个将两者结合在一起的VBA宏,因此我只需要使用一个命令,不管我是从单元格还是网页复制,它都会粘贴,而不需要任何源格式。
伪码:
Sub SuperPaste()
if clipboard source is a cell (or vice versa, whatever is easier):
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
else:
ActiveSheet.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:= _
False, NoHTMLFormatting:=True
(maybe some error handling)
End Sub发布于 2021-11-16 20:10:24
下面是您想要做的事情:
Sub SuperPaste()
''' Clipboard source is a current-instance excel cut:
''' - only option is to paste all (or throw an error msg)
If Application.CutCopyMode = xlCut Then
ActiveSheet.Paste
''' Clipboard source is a current-instance excel copy:
''' - paste values only (keeps destination formats)
''' - am pasting to the activecell to avoid mis-matched source and destination selections
ElseIf Application.CutCopyMode = xlCopy Then
ActiveWindow.ActiveCell.PasteSpecial xlPasteValues
''' Clipboard is empty: report to user
ElseIf Application.ClipboardFormats(1) = -1 Then
MsgBox "Nothing has been copied."
''' Clipboard source is another application (including another instance of excel):
''' - paste text (same as paste values for excel)
''' - still retains numbers (if tabbed or tabled apart) from word, html, another instance of excel, etc.
Else: ActiveSheet.PasteSpecial Format:="Text"
End If
End Sub备注:
,从而完成更复杂的事情。
然而,上述
编辑:添加了对空剪贴板的处理
https://stackoverflow.com/questions/69994482
复制相似问题