VBA Code Update Help - moving a row to a new sheet based on cell data

cmschmitz24

Board Regular
Joined
Jan 27, 2017
Messages
150
I tried reusing an existing code that works for a different spreadsheet to update it and use it with a new spreadsheet and data set but it's bugging out. Someone please help :) It bugs out on "Rng.EntireRow.Copy Worksheets("REH Annuitant").Range("A" & lastrow2 + 1)". The third to last row of code.

VBA Code:
Sub TEST()
'Moves REH Annuitants to a New Sheet
 Dim Rng As Range, r As Long, lastrow2 As Long, Lastrow As Long
    Application.ScreenUpdating = False

    Lastrow = Worksheets("MSC").UsedRange.Rows.Count
    lastrow2 = Worksheets("REH Annuitant").UsedRange.Rows.Count
    If lastrow2 = 1 Then lastrow2 = 0
        For r = Lastrow To 2 Step -1
            If Range("G" & r).Value = "S" 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("REH Annuitant").Range("A" & lastrow2 + 1)
        Rng.EntireRow.Delete
    Application.ScreenUpdating = True
End Sub

*Data in columns D/E has been removed.
1615933458646.png
 
Can you report the code as it is now?
 
Upvote 0

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
VBA Code:
'MOVES REHIRED ANNUITANTS TO NEW TAB
    Dim Rng As Range, r As Long, lastrow2 As Long
    Application.ScreenUpdating = False

    Lastrow = Worksheets("MSC").UsedRange.Rows.Count
    lastrow2 = Worksheets("REH Annuitant").UsedRange.Rows.Count
    If lastrow2 = 1 Then lastrow2 = 0
        For r = Lastrow To 2 Step -1
            If Range("L" & r).Value = "REH Annuitant" 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
      If Not Rng Is Nothing Then
         Rng.EntireRow.Copy Worksheets("REH Annuitant").Range("A" & lastrow2 + 1)
         Rng.EntireRow.Delete
      End If
    Application.ScreenUpdating = True
    
    
'MOVES GRAD WRS MOVEMENTS TO NEW TAB
    Application.ScreenUpdating = False
    
    Lastrow = Worksheets("MSC").UsedRange.Rows.Count
    lastrow2 = Worksheets("Grad WRS Movement").UsedRange.Rows.Count
    If lastrow2 = 1 Then lastrow2 = 0
        For r = Lastrow To 2 Step -1
            If Range("L" & r).Value = "Grad WRS Movement" Then
                If Rng Is Nothing Then
                    Set Rng = Range("A" & r)
                Else
                    Set Rng = Nothing
                End If
            End If
        Next r
      If Not Rng Is Nothing Then
         Rng.EntireRow.Copy Worksheets("Grad WRS Movement").Range("A" & lastrow2 + 1)
         Rng.EntireRow.Delete
      End If
    Application.ScreenUpdating = True
 
Upvote 0
You've put it in the wrong place, it should be like
VBA Code:
'MOVES REHIRED ANNUITANTS TO NEW TAB
    Dim Rng As Range, r As Long, lastrow2 As Long
    Application.ScreenUpdating = False

    LASTROW = Worksheets("MSC").UsedRange.Rows.Count
    lastrow2 = Worksheets("REH Annuitant").UsedRange.Rows.Count
    If lastrow2 = 1 Then lastrow2 = 0
        For r = LASTROW To 2 Step -1
            If Range("L" & r).Value = "REH Annuitant" 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
      If Not Rng Is Nothing Then
         Rng.EntireRow.Copy Worksheets("REH Annuitant").Range("A" & lastrow2 + 1)
         Rng.EntireRow.Delete
      End If
    Application.ScreenUpdating = True
    
   Set Rng = Nothing
    
'MOVES GRAD WRS MOVEMENTS TO NEW TAB
    LASTROW = Worksheets("MSC").UsedRange.Rows.Count
    lastrow2 = Worksheets("Grad WRS Movement").UsedRange.Rows.Count
    If lastrow2 = 1 Then lastrow2 = 0
        For r = LASTROW To 2 Step -1
            If Range("L" & r).Value = "Grad WRS Movement" 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
      If Not Rng Is Nothing Then
         Rng.EntireRow.Copy Worksheets("Grad WRS Movement").Range("A" & lastrow2 + 1)
         Rng.EntireRow.Delete
      End If
    Application.ScreenUpdating = True
 
Upvote 0
Solution
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,243
Members
449,075
Latest member
staticfluids

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