VBA to transfer a row of data based on 1 cell value

RangerRuss

New Member
Joined
Jul 22, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi all,

I am trying to write a VBA to copy a row of data off Sheet1 based on the word "Settled" in column D and then insert the data onto Sheet2 (into the exact same columns) and then delete the row of data off Sheet1. I would then like all the remaining rows left on Sheet1 move up and fill the row gaps left by the transferred Settled rows. This will be executed by pressing the button on Sheet1 called "UPDATE".

I would also like to have a message appear once the button above is pressed to warn users that "proceeding will delete information immediately" Yes or No (No will end the sub and yes will execute the sub)

Sorry, I cant download the XL2BB as I am working off a work computer.

Thank you in advance.
 

Attachments

  • Capture 1.PNG
    Capture 1.PNG
    52.8 KB · Views: 13
  • Capture2.PNG
    Capture2.PNG
    41.6 KB · Views: 10

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
try this on a copy of you file.

VBA Code:
Sub Move_Settled()

Dim rs As Worksheet
Set rs = Worksheets("Sheet2")

cnt = 0

x = MsgBox("proceeding will delete information immediately", vbQuestion + vbYesNo + vbDefaultButton2, "Move Settled to sheet 2")
If x <> vbYes Then Exit Sub

For r = 1 To Cells(Rows.Count, "D").End(xlUp).Row
If Cells(r, "D") = "Settled" Then

    lr = rs.Cells(Rows.Count, "D").End(xlUp).Row + 1
    Rows(r).EntireRow.Copy rs.Range("A" & lr)
cnt = cnt + 1
End If
Next r

'delete settled

On Error Resume Next
    With Range("D:D")
        .Replace "Settled", False, xlWhole
        .SpecialCells(xlCellTypeConstants, 4).EntireRow.Delete
    End With
On Error GoTo 0


MsgBox cnt & "-Record(s) Deleted"

End Sub

hth,

Ross
 
Upvote 0
try this on a copy of you file.

VBA Code:
Sub Move_Settled()

Dim rs As Worksheet
Set rs = Worksheets("Sheet2")

cnt = 0

x = MsgBox("proceeding will delete information immediately", vbQuestion + vbYesNo + vbDefaultButton2, "Move Settled to sheet 2")
If x <> vbYes Then Exit Sub

For r = 1 To Cells(Rows.Count, "D").End(xlUp).Row
If Cells(r, "D") = "Settled" Then

    lr = rs.Cells(Rows.Count, "D").End(xlUp).Row + 1
    Rows(r).EntireRow.Copy rs.Range("A" & lr)
cnt = cnt + 1
End If
Next r

'delete settled

On Error Resume Next
    With Range("D:D")
        .Replace "Settled", False, xlWhole
        .SpecialCells(xlCellTypeConstants, 4).EntireRow.Delete
    End With
On Error GoTo 0


MsgBox cnt & "-Record(s) Deleted"

End Sub

hth,

Ross
Thanks Ross,
This works great but the code deletes all the settled lines from the worksheet but only pastes the first record onto sheet 2.
Any ideas?

Your help is greatly appreciated.
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,438
Members
449,083
Latest member
Ava19

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