VBA Copy/Paste Error

MBM45445

New Member
Joined
Nov 19, 2020
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
Hi,
I am working on a macro that searches for text, then cuts the row that contains the text and pastes it towards the top of the sheet. I get an error when it attempts to paste in the row that already contains the data (ex: if "Atlanta is already in row 15). Below is what I currently have coded. Any ideas?

Range("A1:J1").Select
Set f = Cells.Find(What:="Atlanta")
Rows(f.Row).Select
Rows(f.Row).Activate
Selection.Cut
Rows("15:15").Select
Selection.Insert Shift:=xlDown
Range("A16").Select

Range("A1:J1").Select
Set f = Cells.Find(What:="Boston")
Rows(f.Row).Select
Rows(f.Row).Activate
Selection.Cut
Rows("16:16").Select
Selection.Insert Shift:=xlDown
Range("A17").Select
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,610
Office Version
  1. 365
Platform
  1. Windows
Hi & welcome to MrExcel.
How about
VBA Code:
Set f = Cells.Find(What:="Atlanta")
If f.Row <> 15 Then
   f.EntireRow.Cut
   Rows(15).Insert Shift:=xlDown
End If
Range("A16").Select
 
Solution

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,610
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,129,660
Messages
5,637,626
Members
416,977
Latest member
kdoederlein

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
Top