SamCramphorn
SamCramphorn

Reputation: 143

Copy Variable's Contents to Clipboard VBA (Excel 2013)

I know this should be a super easy solution but I can't fathom it out.

I'm just making a simple button which copies the values in the range of 'Output' to my clipboard.

Private Sub CommandButton1_Click()
Dim Output As String
    Output = Range("Output")
    MsgBox (Output + " Text has been copied")
End Sub

I want Output to copy to my clipboard.

Edit:

Many Thanks Sam

Upvotes: 2

Views: 35080

Answers (3)

SamCramphorn
SamCramphorn

Reputation: 143

I did a 180 on it and changed it around,

Solution below:

Private Sub CommandButton1_Click()
Dim myData As DataObject
Dim Output As String

Output = UCase(InputBox("Type Text Here"))

If Output = "" Then
End
Else
End If
Set myData = New DataObject
myData.SetText Output
myData.PutInClipboard
MsgBox (Output + " text has been copied")
End Sub

Upvotes: 5

Jean-Pierre Oosthuizen
Jean-Pierre Oosthuizen

Reputation: 2693

I assume you want the Values of a Range which is called "Output"

The script below will add join (like the WorkSheetFunction "Concantenate") and then display it in the MsgBox and copy it to the ClipBoard.

Private Sub CommandButton1_Click()

    Dim Output As Range
    Dim MyString As String
    Dim OutputText As DataObject

    Set OutputText = New DataObject
    Set Output = Range("A1:A5")

    MyString = ""

    For Each cell In Output

        MyString = MyString & cell.Value & " "

    Next cell

    OutputText.SetText MyString
    OutputText.PutInClipboard

    MsgBox (MyString + " Text has been copied")

End Sub

Upvotes: 0

luke_t
luke_t

Reputation: 2975

You would be better declaring Output as an object variable (or, more specifically, as a range).

You can then use the value of the range within the messagebox, whilst also copying the value to the clipboard.

Private Sub CommandButton1_Click()
    Dim Output As Range
    Set Output = Range("Output")

    Output.Copy
    MsgBox (Output + " Text has been copied")
End Sub

Upvotes: 0

Related Questions