Copy contents of variable into clipboard

S

Simka

Hi everyone,

My head has gone blank. I am trying to copy the contents of a variable into
the clipboard.

ie:
value in clipboard = MyVariable

Can someone please remind me?
 
W

Wigi

What do you intend to do with the variable?

Wouldn't there be better methods to use, then?
 
R

ron

Hi everyone,

My head has gone blank. I am trying to copy the contents of a variable into
the clipboard.

ie:
value in clipboard = MyVariable

Can someone please remind me?

Try the following...ron

Dim mystring As New DataObject

my_var= "assign something to the variable"

mystring.SetText my_var
mystring.PutInClipboard
ActiveSheet.PasteSpecial Format:="Text"
 
S

Simka

It's basically a follow-on from these statements:

Sub Simka1()
'This routine adds the values of selected cells and
'places the result in cell A5000 then copies the value to clipboard.

With Range("A5000")

.Value = Application.WorksheetFunction.Sum(Selection)
.Copy

End With
End Sub

and

Sub Simka2()

'This routine adds the values of selected cells and
'places the result in the variable 'SumOfRange'.

Dim SumOfRange As Double

SumOfRange = 0

SumOfRange = Application.WorksheetFunction.Sum(Selection) 'Add the Range

Debug.Print SumOfRange 'Check result in Immediate window

End Sub

Where routine Simka1 places the value into cell A5000 and then copies the
value into the clipboard, this is the long run makes the worksheet size
(file) much bigger.

Routine Simka2 does much the same thing except it places the value in the
variable SumOfRange so that I could then use a different routine to place
that value later.

I want a mixture of the two. I want routine Simka2 but then place the value
into the clipboard ready for pasting elsewhere. So I am after something like:

Sub Simka2()
Dim SumOfRange As Double
SumOfRange = 0
SumOfRange = Application.WorksheetFunction.Sum(Selection) 'Add the Range

Value in Clipboard = SumOfRange

Debug.Print SumOfRange 'Check result in Immediate window
End Sub
 
S

Simka

Hi ron,

I'm not sure how to understand what you have written below, but I think you
are on the right lines and understanding what I am trying to achieve.

I'm not too familiar with Class Modules, I find them a little confusing. Can
you write the code in full so that I can copy it to modules/class modules and
then I can follow what you are thinking?

Cheers,

Simka.
 
R

ron

Hi ron,

I'm not sure how to understand what you have written below, but I think you
are on the right lines and understanding what I am trying to achieve.

I'm not too familiar with Class Modules, I find them a little confusing. Can
you write the code in full so that I can copy it to modules/class modulesand
then I can follow what you are thinking?

Cheers,

Simka.









- Show quoted text -

Hi Simka...Just copy and paste the following code into a module.
Assign whatever it is that you want to move to the clipboard to
"my_var". The ActiveSheet.Paste line simply demonstrates that you
have copied the information to the clipboard. Change or delete this
line depending on what you want to do with the information once it is
in the clipboard...ron

Sub CopyToClipboard()

Dim mystring As New DataObject

my_var = "assign something to the variable"

mystring.SetText my_var
mystring.PutInClipboard

ActiveSheet.PasteSpecial Format:="Text"

End Sub
 
S

Simka

ron said:
Hi Simka...Just copy and paste the following code into a module.
Assign whatever it is that you want to move to the clipboard to
"my_var". The ActiveSheet.Paste line simply demonstrates that you
have copied the information to the clipboard. Change or delete this
line depending on what you want to do with the information once it is
in the clipboard...ron

Sub CopyToClipboard()

Dim mystring As New DataObject

my_var = "assign something to the variable"

mystring.SetText my_var
mystring.PutInClipboard

ActiveSheet.PasteSpecial Format:="Text"

End Sub

Hi ron,

Ah-ha, this is better. this is what I'm nearly trying to get to. I vaguely
seem to remember years ago I had the same sort of problem but I used a
totally different approach, maybe one day I'll remember how I did it.

Anyway I've slightly adjusted your code to accommodate my needs.

Have a look at my reply to Wigi, this may help you understand more or less
what I was trying to achieve. This was so that I could select a number of
different cells (with values), add all the values and and hold the result in
a variable for later use and I also wanted to hold the result in the
clipboard instead of placing the result in a cell.

Cheers & thanks,

Simka.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top