I'm trying to work on a code which copies a range's address to the clipboard and when I paste it, it pastes the sum of the copied range. Below code works when I'm copying and pasting my range to same worksheet. It also works when I'm copying and pasting my range to a different worksheet of same workbook as long as my range is in sequence.
BUT, if my range is for example A1,A3 i.e. I want to sum cell A1 & A3 ignoring cell A2, then my code only works in same worksheet. If I try to copy and paste a non-sequential range to a different worksheet, my result is =+SUM(Sheet1!A1,A3) and I want my result to be =+SUM(Sheet1!A1,Sheet1!A3).
Here's my partially working code:
Sub CopySum()
Dim ObjData As New DataObject
ObjData.Clear
ObjData.SetText "+sum('" & Selection.Parent.Name & "'!" & Selection.Address(0, 0) & ")"
ObjData.PutInClipboard
End Sub
Please let me know what changes should I make to get the desired result.