How to search for the value in a list and cut and paste it to the end of another list on a different sheet?

I have 3 worksheets.  On “sheet1” is a form that lists a value in cell “A1”.  I need search for that value in a list in column “A” on sheet2.  If found, I need to delete that cell and paste the value at the bottom of the list in column “A” on sheet3.
kbay808Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

Could you send a dummy?

Regards
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
What is the trigger for the search? A form button, or whenever the value changes, or ...?
kbay808Author Commented:
On sheet 1 there is a command butting that will trigger the code.  I attached an example file.
Eample.xlsm
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
you can try this:

Private Sub CommandButton21_Click()
    Dim v As String
    Dim r As Range
    Dim r2 As Range
    Dim lastR As Long
    Dim idx As Double
    v = Worksheets("Sheet1").Cells(1, 1)
    Set r = Worksheets("Sheet2").Range("A:A")
    If v = "" Then
        MsgBox "Pls select an item to continue", vbExclamation, "Item not selected"
        Worksheets("Sheet1").Cells(1, 1).Select
        Exit Sub
    End If
    On Error Resume Next
    idx = Application.WorksheetFunction.Match(v, r, 0)
    If Err.Number = 0 Then
        Set r2 = Worksheets("Sheet2").Cells(idx, 1)
        lastR = Worksheets("Sheet3").Cells(Worksheets("Sheet3").Rows.Count, "A").End(xlUp).Row
        r2.Cut Worksheets("Sheet3").Cells(lastR + 1, 1)
        Worksheets("Sheet2").Rows(idx).EntireRow.Delete Shift:=xlUp
    Else
        MsgBox v & " not found in the list", vbExclamation, "Item not found"
        Worksheets("Sheet1").Cells(1, 1).Select
        Exit Sub
    End If
    On Error GoTo 0
End Sub

Open in new window

Eample_b.xlsm
kbay808Author Commented:
The code works, but it's causing an issue with another function.  The list on sheet2 is the source for a dynamic list.  When a name is moved to sheet3 the dynamic list source changes to sheet3 with the name.  Can you change the code to copy and paste the value to sheet3 and then change the value found on sheet2 to ""?
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
>>Can you change the code to copy and paste the value to sheet3 and then change the value found on sheet2 to ""?
sure,.... try this:

Private Sub CommandButton21_Click()
    Dim v As String
    Dim r As Range
    Dim r2 As Range
    Dim lastR As Long
    Dim idx As Double
    v = Worksheets("Sheet1").Cells(1, 1)
    Set r = Worksheets("Sheet2").Range("A:A")
    If v = "" Then
        MsgBox "Pls select an item to continue", vbExclamation, "Item not selected"
        Worksheets("Sheet1").Cells(1, 1).Select
        Exit Sub
    End If
    On Error Resume Next
    idx = Application.WorksheetFunction.Match(v, r, 0)
    If Err.Number = 0 Then
        'Set r2 = Worksheets("Sheet2").Cells(idx, 1)
        lastR = Worksheets("Sheet3").Cells(Worksheets("Sheet3").Rows.Count, "A").End(xlUp).Row
        'r2.Cut Worksheets("Sheet3").Cells(lastR + 1, 1)
        Worksheets("Sheet3").Cells(lastR + 1, 1) = Worksheets("Sheet2").Cells(idx, 1)
        Worksheets("Sheet2").Cells(idx, 1) = ""
        'Worksheets("Sheet2").Rows(idx).EntireRow.Delete Shift:=xlUp
    Else
        MsgBox v & " not found in the list", vbExclamation, "Item not found"
        Worksheets("Sheet1").Cells(1, 1).Select
        Exit Sub
    End If
    On Error GoTo 0
End Sub

Open in new window

but as you can see, your list in Sheet1 could left with blank values if no rows were deleted in sheet2.
empty itemsAre you going to handle this? Or you want anyhow the items where migrated from Sheet2 to Sheet3, and to have a "perfect list" in Sheet1, and to retain the dynamic list source in Sheet2? I believe this can be done too.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
Or you want anyhow the items where migrated from Sheet2 to Sheet3, and to have a "perfect list" in Sheet1, and to retain the dynamic list source in Sheet2? I believe this can be done too.
try add this to achieve that:

Worksheets("Sheet2").Names("Players").RefersToR1C1 = "=Sheet2!R2C1:R11C1"

for example:
Private Sub CommandButton21_Click()
    Dim v As String
    Dim r As Range
    Dim r2 As Range
    Dim lastR As Long
    Dim idx As Double
    v = Worksheets("Sheet1").Cells(1, 1)
    Set r = Worksheets("Sheet2").Range("A:A")
    If v = "" Then
        MsgBox "Pls select an item to continue", vbExclamation, "Item not selected"
        Worksheets("Sheet1").Cells(1, 1).Select
        Exit Sub
    End If
    On Error Resume Next
    idx = Application.WorksheetFunction.Match(v, r, 0)
    If Err.Number = 0 Then
        Set r2 = Worksheets("Sheet2").Cells(idx, 1)
        lastR = Worksheets("Sheet3").Cells(Worksheets("Sheet3").Rows.Count, "A").End(xlUp).Row
        r2.Cut Worksheets("Sheet3").Cells(lastR + 1, 1)
        Worksheets("Sheet2").Rows(idx).EntireRow.Delete Shift:=xlUp
        Worksheets("Sheet2").Names("Players").RefersToR1C1 = "=Sheet2!R2C1:R11C1"
    Else
        MsgBox v & " not found in the list", vbExclamation, "Item not found"
        Worksheets("Sheet1").Cells(1, 1).Select
        Exit Sub
    End If
    On Error GoTo 0
End Sub

Open in new window

kbay808Author Commented:
Also, is there a way to reverse it?  If possible, I would like to create an undo button.
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
>>Also, is there a way to reverse it?  If possible, I would like to create an undo button.
it seems that sheet2 and sheet3 can be edited manually, hence to a certain extend, to track the moving item activities is a bit difficult in this scenario. Of course, we can make the Excel to remember those items that had been moved in sequence but when the list was edited manually, this could make the customized "undo" feature fails.
kbay808Author Commented:
This worked perfect!!!  Thank you very much.  As far as the dropdown list goes, I only had a dynamic dropdown list in my working file.  That's why it did not look correct in the example.  As far is the undo button goes, I just posted another question to address that issue.  Instead of undoing the last move, It would be easier to create a new code to simply copy of the last entry on the list and paste it to the end of the original list.  Then deleting the copied value.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.