How to unselect an area in a macro

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,687
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

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
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,223,715
Messages
6,174,065
Members
452,542
Latest member
Bricklin

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