Move entire row to new worksheet based on cell data

cmschmitz24

Board Regular
Joined
Jan 27, 2017
Messages
150
Hello, I need help refining a VBA code -
I need to have the entire row for a specific data value in column I moved into a new worksheet.

I am currently using this code, however it also deletes all the data rows underneath the rows that are being moved and that cannot happen.

Code:
Dim Check As Range, r As Long, lastrow2 As Long, lastrow As Long
Application.ScreenUpdating = False
lastrow = Worksheets("Export Worksheet").UsedRange.Rows.Count
lastrow2 = Worksheets("Sheet2").UsedRange.Rows.Count
If lastrow2 = 1 Then lastrow2 = 0
    For r = lastrow To 2 Step -1
        If Range("I" & r).Value = "UWMSN" Then
            Rows(r).Cut Destination:=Worksheets("Sheet2").Range("A" & lastrow2 + 1)
            lastrow2 = lastrow2 + 1
            Else:
        End If
    Next r
Application.ScreenUpdating = True

Thanks,
Christina
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Does this do what you're after?
Code:
Sub chk()

    Dim Rng As Range, r As Long, lastrow2 As Long, lastrow As Long
Application.ScreenUpdating = False

    lastrow = Worksheets("Export Worksheet").UsedRange.Rows.Count
    lastrow2 = Worksheets("Sheet2").UsedRange.Rows.Count
    If lastrow2 = 1 Then lastrow2 = 0
        For r = lastrow To 2 Step -1
            If Range("I" & r).Value = "UWMSN" Then
                If Rng Is Nothing Then
                    Set Rng = Range("A" & r)
                Else
                    Set Rng = Union(Rng, Range("A" & r))
                End If
            End If
        Next r
        Rng.EntireRow.Copy Worksheets("Sheet2").Range("A" & lastrow2 + 1)
        Rng.EntireRow.Delete
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

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