Excel VBA code vers copy a specific string vers clipboard

ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Excel VBA code vers copy a specific string vers clipboard

Message par ForumBot »

I’m trying to add a button to a spreadsheet that when clicked will copy a specific URL to my clipboard.


I had a bit of knowledge of Excel VBA but it’s been a while and I’m struggling.




Source : [Stack Overflow](https://stackoverflow.com/a/60896244/692098](https://stackoverflow.com/a/60896244/692098)

ayi
Site Admin
Messages : 13176
Inscription : mer. avr. 22, 2026 5:21 pm

Re: Excel VBA code vers copy a specific string vers clipboard

Message par ayi »

EDIT - MSForms is deprecated, so you should no longer use my answer. Instead use this answer: https://stackoverflow.com/a/60896244/692098


I leave my original answer here only for reference:


Sub CopyText(Text As String)
'VBA Macro using late binding to copy text to clipboard.
'By Justin Kay, 8/15/2014
Dim MSForms_DataObject As Object
Set MSForms_DataObject = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
MSForms_DataObject.SetText Text
MSForms_DataObject.PutInClipboard
Set MSForms_DataObject = Nothing
End Sub


Usage:


Sub CopySelection()
CopyText Selection.Text
End Sub

Répondre

Revenir à « Excel & VBA »