Excel VBA: New range if copied to new location.

What VBA code would I use to determine the new range if a user copied/pasted the range to a new location. (See Examples)


Starting Range: C20:F30
User Moves Range to: H4
Solution: In this case, if the user cut C20:F30 and pasted it into cell H4 the new range would be: H4:K14.


Starting Range: AB42:AD60
User Moves Range to:DD50
Solution in this case, if the user cut AB42:AD60 and pasted it to cell DD50 the new range would be: DD50:DF68


Starting Range: AB42:AD60
User Moves Range to:A7
Solution in this case, if the user cut AB42:AD60 and pasted it to cell A7 the new range would be: A7:C25
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieAnalyst Assistant Commented:
To get the new range you could resize the destination range by the no of columns/rows in the source range, something like this.
Dim rngSrc As Range
Dim rngDst As Range
Dim rngNew As Range

    Set rngSrc = Range("AB42:AD60")
    Set rngDst = Range("A7")

    rngSrc.Copy  rngDst

   Set rngNew = rngDst.Resize(rngSrc.Rows.Count, rngSrc.Columns.Count)

   MsgBox rngNew.Address(0,0)

Open in new window

How is the copying/pasting being done?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fabrice LambertFabrice LambertCommented:
Maybe by using the worksheet change and worksheetSelectionChange event.
Try the following in a worksheet module:
Option Explicit

Private moveDetection As Boolean

Private Sub Worksheet_Change(ByVal Target As Range)
    moveDetection = True
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If moveDetection Then
        moveDetection  = False
        Debug.Print "Range moved to: " & rng.Address
    End If
End Sub

Open in new window

ouestqueAuthor Commented:
Thank You! I didn't even know there was a 're-size' function. Pretty cool!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.