vba code to sum a selected range and then copy for pasting

DeFacto

New Member
Joined
Jul 29, 2004
Messages
33
Hi! Is there a way to use vba code to sum a selected range and then copy this value for use in pasting to a different part of a workbook? This is the code I tried:

Sub SumRangeCopy()
Set CurrSum = Application.WorksheetFunction.Sum(Selection)
CurrSum.Copy
End Sub

When I try that code I get the error message: "Compile Error: Object Required"

Any suggestions would be greatly appreciated!
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Code:
Sub SumRangeCopy()
[A1] = Application.WorksheetFunction.Sum(Selection)
End Sub
 
Upvote 0
Thanks! However, is there a way to store the value such that I can paste it into a cell of my choice? I.e., the cell or e-mail I paste the value into will change every time.
 
Upvote 0
I guess another way of saying it is that I'd like to copy the summed value of the selected range to the clipboard such that I can paste said value into a different tab, different workbook, or different application (which will vary each time).
 
Upvote 0
Hi, DeFacto,

try this
Code:
Option Explicit

Sub put_in_get_from_clipboard()
'set reference to Microsoft Forms x.x Object Library
Dim MyData As Object
Set MyData = New DataObject
MyData.SetText Application.WorksheetFunction.Sum(Selection)
MyData.PutInClipboard
'MsgBox MyData.GetText
End Sub
in the VBAmenu Tools / References set the required reference

kind regards,
Erik
 
Upvote 0
Code:
Sub Store_in_Clipboard()
Dim theData As New DataObject
theData.SetText Format(Application.WorksheetFunction.Sum(Selection))
theData.PutInClipboard
End Sub
 
Upvote 0
I can't get the clipboard to paste anything other than some question marks in my cells. Everything seems to work. Is there an update with my version of excel. This post does seem outdated. I'm on office 2013. Any help would be appreciated.

This is what I see when I paste.
1601920155286.png


I read that I need to select this reference.

-- corrupted image removed --

Any help would be appreciated.
 
Last edited by a moderator:
Upvote 0
I restarted my excel. I then was able to test it again and it worked. Then I tried it on a different book and it gave me the two ?? again. I went back to the book it worked on and it won't work now. Any reason why it would revert back?
 
Upvote 0
I can't get the clipboard to paste anything other than some question marks in my cells. Everything seems to work. Is there an update with my version of excel. This post does seem outdated. I'm on office 2013. Any help would be appreciated.

This is what I see when I paste.
View attachment 23655

I read that I need to select this reference.

-- corrupted image removed --

Any help would be appreciated.
Hi :)
did you find any solution for this problem ?
Would be kind your answer
 
Upvote 0

Forum statistics

Threads
1,215,390
Messages
6,124,667
Members
449,178
Latest member
Emilou

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top