VBA find and Cut

Siyanna

Well-known Member
Joined
Nov 7, 2011
Messages
1,146
Hi All

what is the best way to find a specific value called Leaver in say range (A2:A50) and cut the entire row when found and paste it to the bottom deleting and blank cells in between?

my aim is to ensure that i have all leavers at the bottom of the data
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
I don't not know if this is the best way (probably not), but here is one way to do what you asked for...
Code:
[table="width: 500"]
[tr]
	[td]Sub MoveLeaverToBottom()
  Dim LastRow As Long
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  Application.ScreenUpdating = False
  With Range("A2:A" & LastRow)
    .Replace "Leaver", "=Leaver", xlWhole, , False, , False, False
    On Error GoTo NoLeaver
    Intersect(.SpecialCells(xlFormulas, xlErrors).EntireRow, Columns("A").Resize(, 3)).Copy Cells(LastRow + 1, "A")
    .SpecialCells(xlFormulas).EntireRow.Delete
  End With
  Columns("A").SpecialCells(xlConstants, xlErrors).Value = "Leaver"
NoLeaver:
  Application.ScreenUpdating = True
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
I don't not know if this is the best way (probably not), but here is one way to do what you asked for...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Sub MoveLeaverToBottom()
  Dim LastRow As Long
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  Application.ScreenUpdating = False
  With Range("A2:A" & LastRow)
    .Replace "Leaver", "=Leaver", xlWhole, , False, , False, False
    On Error GoTo NoLeaver
    Intersect(.SpecialCells(xlFormulas, xlErrors).EntireRow, Columns("A").Resize(, 3)).Copy Cells(LastRow + 1, "A")
    .SpecialCells(xlFormulas).EntireRow.Delete
  End With
  Columns("A").SpecialCells(xlConstants, xlErrors).Value = "Leaver"
NoLeaver:
  Application.ScreenUpdating = True
End Sub
[/TD]
[/TR]
</tbody>[/TABLE]

Hi Rick

Ive amended the code slightly so it copies to the last column

Only issue I have is that it shows an error(NAME)ERROR Message where it had the word Leaver where I still need to have that word there

here is the amendedcode
 
Upvote 0
for some reason its not allowing me to copy and paste the code here but all I did was that added a last column variable and in the resize bit change the 3 to lastcol
 
Upvote 0
Only issue I have is that it shows an error(NAME)ERROR Message where it had the word Leaver where I still need to have that word there
Try changing this line of code...

Columns("A").SpecialCells(xlConstants, xlErrors).Value = "Leaver"

to this...

Columns("A").SpecialCells(xlFormulas, xlErrors).Value = "Leaver"
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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