How to unselect an area in a macro

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,691
Office Version
  1. 365
Platform
  1. Windows
The macro below makes a copy of a range of cells just before updating them so I can see the changes. It was leaving the range selected, so I added the last statement, but the range is still left selected (walking dotted border).

How do I cancel the selection?

Code:
Sub CopyErrorRow(inRowError As Long, inColLeft As Long, inColRight As Long)
Dim RngErrCurr As String  'The current error stats (source)
Dim RngErrPrev As String  'The previous error stats (destination)
RngErrCurr = Cells(inRowError, inColLeft).Address & ":" & Cells(inRowError, inColRight).Address
RngErrPrev = Cells(inRowError + 1, inColLeft).Address & ":" & Cells(inRowError + 1, inColRight).Address

Range(RngErrCurr).Select
Selection.Copy
Range(RngErrPrev).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Cells(1, 1).Select  'Cancel the selection

End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
If you mean take it out of copy mode then...

Code:
Application.CutCopyMode = False

Yup, that's what I meant to say. I was also about to post that I used the macro recorder to discover the solution that you posted.

Thanks, J
 
Upvote 0
Happy you found a solution, btw I wouldn't use Select/Selection in the code (it only slows it down)....

Rich (BB code):
Sub CopyErrorRow(inRowError As Long, inColLeft As Long, inColRight As Long)
    Dim RngErrCurr As String    'The current error stats (source)
    Dim RngErrPrev As String    'The previous error stats (destination)
    RngErrCurr = Cells(inRowError, inColLeft).Address & ":" & Cells(inRowError, inColRight).Address
    RngErrPrev = Cells(inRowError + 1, inColLeft).Address & ":" & Cells(inRowError + 1, inColRight).Address

    Range(RngErrCurr).Copy
    With Range(RngErrPrev)
        .PasteSpecial Paste:=xlPasteValues, Operation:=xlNone
        .PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone
    Application.CutCopyMode = False
    End With

End Sub
 
Upvote 0
Thanks for the improved code. My macro is so simple that I can't tell if it's faster, but it's certainly more compact. And, even better, it works!

Cheers
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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