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
 

Some videos you may like

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,682
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
55,682
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,127,885
Messages
5,627,468
Members
416,250
Latest member
darius_rebelo

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