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

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
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
 
Upvote 0
Solution
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,650
Messages
6,120,734
Members
448,987
Latest member
marion_davis

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