VBA - De-select pasted area

mnyankee

New Member
Joined
Mar 20, 2023
Messages
11
Office Version
  1. 365
Platform
  1. Windows
In the code below I am copying and pasting data from my pivot table into two other sheets. At the end of the process the pasted area remains selected on both sheets. How do I remove that selection and just have the cursor in say cell A1 or wherever I want to leave it on those two sheets? I tried Sheets("Current Report").Range("A1").Select but that results in an error. Surely it is something simple I am overlooking.

Sub CopyPivotTableData()

Application.ScreenUpdating = False 'speeds up the process by not refreshing the screen
Application.CutCopyMode = False 'added to keep clipboard cache empty

Sheets("Current Report").Activate
Sheets("Current Report").Range("A8:S50").ClearContents
Sheets("Proposed Report").Activate
Sheets("Proposed Report").Range("A10:S50").ClearContents

Sheets("Pivot Table").Activate
Sheets("Pivot Table").Range("A5:S5", Range("A" & Rows.Count).End(xlUp).Offset(-1, 0)).Copy

Sheets("Current Report").Range("A8").PasteSpecial xlPasteValues
Sheets("Proposed Report").Range("A10").PasteSpecial xlPasteValues

'Sheets("Current Report").Range("A1").Select
'Sheets("Proposed Report").Range("A1").Select

Application.ScreenUpdating = True 'turns refreshing back on
Application.CutCopyMode = True 'turns clipboard cache use back on


End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
I think adding another "Application.CutCopyMode = False" at the end should turn it off.
 
Upvote 0
"Sheets("Current Report").Range("A1").Select"
You would get an error with this line of code if Current Report is not the active sheet... which it is not in your example code. First select the sheet. Then select the cell..

Here is an alternative method without using PasteSpecial and changing the range selections.

VBA Code:
Sub CopyPivotTableData()
   
    Dim rngCopy As Range
   
    Application.ScreenUpdating = False 'speeds up the process by not refreshing the screen
   
    With Sheets("Pivot Table")
        Set rngCopy = .Range("A5:S5", .Range("A" & Rows.Count).End(xlUp).Offset(-1, 0))
    End With
   
    Sheets("Current Report").Range("A8:S50").ClearContents
    Sheets("Current Report").Range("A8").Resize(rngCopy.Rows.Count, rngCopy.Columns.Count).Value = rngCopy.Value
   
    Sheets("Proposed Report").Range("A10:S50").ClearContents
    Sheets("Proposed Report").Range("A10").Resize(rngCopy.Rows.Count, rngCopy.Columns.Count).Value = rngCopy.Value
   
    Application.ScreenUpdating = True 'turns refreshing back on
   
End Sub
 
Upvote 0
Solution
"Sheets("Current Report").Range("A1").Select"
You would get an error with this line of code if Current Report is not the active sheet... which it is not in your example code. First select the sheet. Then select the cell..

Here is an alternative method without using PasteSpecial and changing the range selections.

VBA Code:
Sub CopyPivotTableData()
  
    Dim rngCopy As Range
  
    Application.ScreenUpdating = False 'speeds up the process by not refreshing the screen
  
    With Sheets("Pivot Table")
        Set rngCopy = .Range("A5:S5", .Range("A" & Rows.Count).End(xlUp).Offset(-1, 0))
    End With
  
    Sheets("Current Report").Range("A8:S50").ClearContents
    Sheets("Current Report").Range("A8").Resize(rngCopy.Rows.Count, rngCopy.Columns.Count).Value = rngCopy.Value
  
    Sheets("Proposed Report").Range("A10:S50").ClearContents
    Sheets("Proposed Report").Range("A10").Resize(rngCopy.Rows.Count, rngCopy.Columns.Count).Value = rngCopy.Value
  
    Application.ScreenUpdating = True 'turns refreshing back on
  
End Sub
Thanks AlphaFrog. I am adopting your solution and appreciate the guidance.
 
Upvote 0

Forum statistics

Threads
1,215,103
Messages
6,123,112
Members
449,096
Latest member
provoking

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