VBA pasting to wrong location

riley454

Board Regular
Joined
Apr 21, 2010
Messages
52
Office Version
  1. 2010
Platform
  1. Windows
I have a macro that (in part)
-copies a range (A2:B27) from a source sheet
-selects all sheets except 2 that don't meet my criteria
-pastes the copied range to the same locations as the source on all the selected sheets
-copies a different range (E1:KW28) from the source
-pastes to the still selected destination sheets at the same locations as the source

This almost works perfectly however instead of pasting the 2nd range to range("E1") it pastes to E2 for some reason

I have tried adding 'Selection.Range("E1").Select' prior to pasting to E1 but that offsets the paste by several columns

I have also tried reversing the order of copy/paste using the E1 range first which pastes correctly but then the A2 range is pasted to E3 instead of A2

VBA Code:
Sheets("Source").Range("A2:B27").Copy

'selects all sheets except those named
For i = 2 To ThisWorkbook.Sheets.Count
    If Sheets(i).Name <> "mainData" And Sheets(i).Name <> "Source" Then Sheets(i).Select Replace:=False
Next i

Selection.Range("A2").PasteSpecial Paste:=xlPasteValuesAndNumberFormats

Sheets("Source").Range("E1:KW28").Copy
Selection.Range("E1").PasteSpecial Paste:=xlPasteFormulasAndNumberFormats
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Selection.Range("E1")

is quite different with:

Activesheet.Range("E1")

Try to use Activesheet instead of Selection
 
Upvote 0
Solution
Thanks Bebo

I only changed the 2nd "Selection.Range" to "Activesheet.Range" and this worked perfectly

Just curious why using Selection works ok for the first paste but not the 2nd and why it offsets it incorrectly?
 
Upvote 0
Selection.Range("A2") refers to the cell one row below and in the same column as the first cell of whatever is selected. If A1 happens to be selected, you'll get A2. If it's any other cell, you will not be pasting to A2.
 
Upvote 0
Awesome! Thanks for the explanation RoryA. I was trying to goog it but couldn't find anything that related to the usage of "Selection" as I was attempting. It also explains why my original code "worked" but wasn't really doing what I thought it was. Just got lucky :sneaky:

Thanks again to all
 
Upvote 0

Forum statistics

Threads
1,214,411
Messages
6,119,356
Members
448,888
Latest member
Arle8907

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