Copy pasting macro fails to copy the needed data if column/row is added

Guims

New Member
Joined
Jan 28, 2020
Messages
17
Office Version
  1. 365
Platform
  1. Windows
Hi, I'm trying to create a macro that copy-pastes data from one worksheet to another. The macro has to copy four cells from one sheet and paste them in 4 cells in another. the issue is that, post macro recording, I am changing both sheets (adding or deleting rows and columns) so the macro copy-pastes the cells I initially inserted (say B1:B4) despite the wanted data having moved due to the new columns or rows that have been added/deleted (i.e now in C1:C4 if I added a column). Is it possible to make macros that "follow" the cells you copy the same way a formula does (formulas adapt the referred cells when you add/remove a column or row) so that it still copies the 4 cells I want to copy (now C1:C4 in the example) even if they are not located at the same place they were while I was creating the macro (B1:B4 in the example? The same applies to the place where the macro pasts in the pasting worksheet if I add/remove columns or rows.

Thanks in advance.
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Hi and welcome to MrExcel!

It could be done in 2 ways.
1. You can name the 4 cells in named range. That way in the macro you would refer to the named range, eg .:

VBA Code:
Sub copynamed()
  Range("MyCells").Copy
End Sub

1580228627959.png



2. The other option is more complex, you would have to put a title above the 4 cells, the macro would look for that title on the sheet and take the 4 cells below. eg.:

VBA Code:
Sub copycells()
  Dim f As Range
  Set f = Cells.Find("Mytitle", , xlValues, xlWhole)
  If Not f Is Nothing Then
    f.Offset(1).Resize(4).Copy
  End If
End Sub

1580228659624.png
 
Upvote 0
Hi Dante,

Thank you so much this works very well! I'll definitely be using this site a lot as of now ;)

Guillaume
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,590
Messages
6,120,423
Members
448,961
Latest member
nzskater

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