Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AlunHewinson/5388042 to your computer and use it in GitHub Desktop.
Save AlunHewinson/5388042 to your computer and use it in GitHub Desktop.
A shortcut key to paste special value in Excel is incredibly useful to me. Sometimes, though, the code would break if I was pasting from text copied from a website. This tries the second type of paste if the first errors. You just need to assign a shortcut key to the macro; I use ctrl-m.
Sub PasteValues()
On Error GoTo txt
Selection.PasteSpecial Paste:=xlPasteValues
Exit Sub
txt:
If ActiveCell.MergeCells Then
Application.DisplayAlerts = True
Dim DObj As MSForms.DataObject
Dim s As String
Set DObj = New MSForms.DataObject
DObj.GetFromClipboard
s = DObj.GetText
ActiveCell.Formula = s
Else
Application.DisplayAlerts = False
ActiveSheet.PasteSpecial Format:="Text", Link:=False, DisplayAsIcon:=False
End If
End Sub
@AlunHewinson
Copy link
Author

A shortcut key to paste special value in Excel is incredibly useful to me. Sometimes, though, the code would break if I was pasting from text copied from a website. This tries the second type of paste if the first errors. You just need to assign a shortcut key to the macro; I use ctrl-m.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment