Move rows to other sheets using a command button

NeedExclHlp

New Member
Joined
Aug 12, 2016
Messages
9
I have a workbook with multiple worksheets and want to create a command button that moves rows in one sheet to another.

In the “Master” worksheet, if “Column U” has an “X” in it, I want to move the entire row to the next empty row in “Scrap” worksheet and delete the original row in the “Master” worksheet.

Both worksheets have the same 28 columns with the same headings.

Is this possible?
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
To attach to button, click Developer, Insert and choose button from Form Controls tool box. When you add the button to the sheet, the 'Assign Macro' dialog box should appear. Click this macro name and then click OK. The macro will now run when you click the command button. The macro is to be copied to your standard code module 1.
Code:
Sub moveToScrap()
Dim sh1 As Worksheet, sh2 As Worksheet
Set sh1 = Sheets("Master")
Set sh2 = Sheets("Scrap")
sh1.UsedRange.AutoFilter 21, "X", xlOr, "x"
sh1.UsedRange.Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Copy sh2.Cells(Rows.Count, 1).End(xlUp)(2)
sh1.UsedRange.Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
sh1.AutoFilterMode = False
End Sub
 
Upvote 0
Yes, its possible. Try this on sample workbook first.

Code:
Sub Cut_Paste_DeleteRow_If_X()


    Dim ws As Worksheet, ws2 As Worksheet
    Dim LR As Long, LR2 As Long
    Dim DataRange As Range, C As Range, rngG As Range

    Application.ScreenUpdating = False

    Set ws = ThisWorkbook.Worksheets("Master")
    Set ws2 = ThisWorkbook.Worksheets("Scrap")

    LR = ws.Cells(ws.Rows.Count, 21).End(xlUp).Row
    LR2 = ws2.Cells(ws.Rows.Count, 21).End(xlUp).Row + 1
    Set DataRange = ws.Range("U1:U" & LR)

    For Each C In DataRange
        If C.Value = "x" Then
            If rngG Is Nothing Then Set rngG = Range(C, C.End(xlToRight))
            Set rngG = Union(rngG, Range(C, C.End(xlToRight)))
        End If
    Next C

    rngG.Copy
    ws2.Range("U" & LR2).PasteSpecial xlPasteValues
    Application.CutCopyMode = False

    ws.Activate
    For x = LR To 1 Step -1
        If Cells(x, 21).Value = "x" Then
            Rows(x).Delete
        End If
    Next x

    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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