Delete Rows of Data in Given Date Range

Nathan95

New Member
Joined
Mar 1, 2020
Messages
34
Office Version
  1. 2010
Platform
  1. Windows
Hey All I have created a userform where users need to input Start Date (tbstartdate), End Date (tbenddate) and employer number (tbPnumber), based on this user form I want the code to be able to remove/Delete all the rows of data from the table that is located in sheet "Mark Leave" that falls between the date range and matches the employer number that was inputted in the user form. The Employer number gets inputted in Column B and Date gets inputted in Column D in the "Mark Leave" sheet.

Any help will be greatly appreciated :)
 
Given what I read in this post and see that @bebo021999 has put some effort into this, and looking at the code he submitted in post #13, he had to pick up those variable names from something that I did not see. All that being said, I tried to make this so it can be easily converted to pick up the required data from some other source and substituted in. I used an InputBox to enter the variables in the interim and essentially used his code with some minor changes. This code tested on your supplied data, which I believe to be a Range of data as opposed to a Table.

VBA Code:
Sub remove()

    Dim lRow As Long, cel As Range, i As Long, rw As String, spl
   
    rw = Application.InputBox("Please enter P Number, Start Date, End Date separated by comma's only", _
        "Data Entry", "P Number,Start Date,End Date", , , , , 2)
    spl = Split(rw, ",")
    If UBound(spl) <> 2 Or rw = "" Then
        MsgBox "Please make sure all three pieces of data are separated by a comma", vbCritical
        Exit Sub
    End If
    With Worksheets("Mark Leave")
        lRow = .Cells(Rows.Count, "B").End(xlUp).Row
        For Each cel In .Range("B2:B" & lRow)
            If cel.Value = Trim(spl(0)) And CDate(cel.Offset(0, 2)) >= Trim(spl(1)) _
            And CDate(cel.Offset(0, 2)) <= Trim(spl(2)) Then cel.Value = ""
        Next
        .Range("B2:B" & lRow).SpecialCells(4).EntireRow.Delete
    End With
   
End Sub
Perfect thanks alot this worked :)
 
Upvote 0

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
You're welcome, we were both happy to help. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,749
Members
448,989
Latest member
mariah3

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