VBA Code to Delete empty Rows after Loop has finished

Joined
Dec 8, 2021
Messages
26
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
I'm pretty happy with myself for getting this far on this idea of mine. When we write an employee up its based on a 12 month rolling period. So in other words they have that on their record for 12 month from the date. No one
was keeping track and it was a mess so I made this tracker. As of recent I figured out the code to remove entries that had expired of the sheet and onto a sheet I titled "Archives"

Here is the code:

VBA Code:
Private Sub Workbook_Open()

b = Worksheets("DPS Disciplinary Actions").Cells(Rows.Count, 3).End(xlUp).Row 'open workbook as trigger
    Dim answer As Integer
 
    answer = MsgBox("Excel is checking to see if there are any DPS Infractions that have expired. ", vbCritical + vbOKOnly, "One Moment Please...") 'msgbox1 to tell folks Excel is working
    'by clicking ok for the previous msgboxit triggers another allowing excel to finish the process
    answer = MsgBox("If Excel locates and moves these old records, you will be able to see them on the Sheet Titled ""Archives""", vbInformation + vbOKOnly, "Just so You Know...")




'loop to check expired entries and place them on the archives sheet

For i = 4 To b
    If Worksheets("DPS Disciplinary Actions").Cells(i, 4).Value = "EXPIRED" Then
 
    Worksheets("DPS Disciplinary Actions").Rows(i).Cut 'cut and paste to the Archives sheet
 
    Worksheets("Archives").Activate
 
    a = Worksheets("Archives").Cells(Rows.Count, 3).End(xlUp).Row

    Worksheets("Archives").Cells(a + 1, 1).Select
 
    ActiveSheet.Paste
 
 
    Worksheets("DPS Disciplinary Actions").Activate
 
    End If
  Next
 
   Application.CutCopyMode = False
 
   ThisWorkbook.Worksheets("DPS Disciplinary Actions").Cells(1, 1).Select
 
 
End Sub


So I went from this:

1651639120671.png


To this but I could not figure out how to add "Delete empty rows" to the code as the finishing touch. Anyone know what I need to add to get it to do that?
1651639221509.png
 

Attachments

  • 1651638531944.png
    1651638531944.png
    1.5 KB · Views: 6
Last edited by a moderator:

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
How about this?
VBA Code:
Private Sub Workbook_Open()

b = Worksheets("DPS Disciplinary Actions").Cells(Rows.Count, 3).End(xlUp).Row 'open workbook as trigger
Dim answer As Integer

answer = MsgBox("Excel is checking to see if there are any DPS Infractions that have expired. ", vbCritical + vbOKOnly, "One Moment Please...")
'msgbox1 to tell folks Excel is working
'by clicking ok for the previous msgboxit triggers another allowing excel to finish the process
answer = MsgBox("If Excel locates and moves these old records, you will be able to see them on the Sheet Titled ""Archives""", vbInformation + vbOKOnly, "Just so You Know...")

'loop to check expired entries and place them on the archives sheet
i = 4
Do While i <= b
    'If expired, then cut paste onto other sheet and then delete current row
    'Note that if current row is deleted, all rows move up and row index i remains same
    If Worksheets("DPS Disciplinary Actions").Cells(i, 4).Value = "EXPIRED" Then
        Worksheets("DPS Disciplinary Actions").Rows(i).Cut
        'cut and paste to the Archives sheet
        Worksheets("Archives").Activate
        a = Worksheets("Archives").Cells(Rows.Count, 3).End(xlUp).Row
        Worksheets("Archives").Cells(a + 1, 1).Select
        ActiveSheet.Paste
        Worksheets("DPS Disciplinary Actions").Activate
        Worksheets("DPS Disciplinary Actions").Rows(i).EntireRow.Delete
    Else
        'If not expired, check next row (increase i)
        i = i + 1
    End If
Loop
Application.CutCopyMode = False
ThisWorkbook.Worksheets("DPS Disciplinary Actions").Cells(1, 1).Select

End Sub

Note that we can use "quick-wrap selection as VBA code" button in this forum to make reading code easier.
 
Upvote 0
Solution

Forum statistics

Threads
1,214,834
Messages
6,121,876
Members
449,056
Latest member
ruhulaminappu

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