Array Copy - Paste Special Add Help

T

TysonE

Here is the code I'm working with:

Basically what this allows me to do is copy and paste multiple
selections, but I want to make one tweak to it. I want it to copy,
and them paste special add.

I believe the line of code that is hanging me up is this:

SelAreas(i).Copy PasteRange.Offset(RowOffset, ColOffset)

I'm not sure how to modify it to accumulate another section on top of
it, and not just paste over it. So what I want this to do, is just
keeping adding the same range together as many times as I run the
macro.

Anyone want to take a stab at it?

Thanks,

Tyson


Option Explicit

Sub CopyMultipleSelection()
Dim SelAreas() As Range
Dim PasteRange As Range
Dim UpperLeft As Range
Dim NumAreas As Integer, i As Integer
Dim TopRow As Long, LeftCol As Integer
Dim RowOffset As Long, ColOffset As Integer
Dim NonEmptyCellCount As Integer

' Exit if a range is not selected
If TypeName(Selection) <> "Range" Then
MsgBox "Select the range to be copied. A multiple selection is
allowed."
Exit Sub
End If

' Store the areas as separate Range objects
NumAreas = Selection.Areas.Count
ReDim SelAreas(1 To NumAreas)
For i = 1 To NumAreas
Set SelAreas(i) = Selection.Areas(i)
Next

' Determine the upper left cell in the multiple selection
TopRow = ActiveSheet.Rows.Count
LeftCol = ActiveSheet.Columns.Count
For i = 1 To NumAreas
If SelAreas(i).Row < TopRow Then TopRow = SelAreas(i).Row
If SelAreas(i).Column < LeftCol Then LeftCol =
SelAreas(i).Column
Next
Set UpperLeft = Cells(TopRow, LeftCol)

' Get the paste address
On Error Resume Next
Set PasteRange = Range("H10")
On Error GoTo 0
' Exit if canceled
If TypeName(PasteRange) <> "Range" Then Exit Sub

' Make sure only the upper left cell is used
Set PasteRange = PasteRange.Range("A1")


' Copy and paste each area
For i = 1 To NumAreas
RowOffset = SelAreas(i).Row - TopRow
ColOffset = SelAreas(i).Column - LeftCol
SelAreas(i).Copy PasteRange.Offset(RowOffset, ColOffset)

Next i
End Sub
 
P

Per Jessen

Hi Tyson

Change the line to theese two lines:

SelAreas(i).Copy
PasteRange.Offset(RowOffset, ColOffset).PasteSpecial Operation:=xlAdd

Regards,
Per
 

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