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 :)
 
I saw you have 2 userforms, in which "Input Leave" with tbstartdate and tbenddate (are they for input leave, not remove?)
and "Remove Lease" with rtbstartdate and rtbenddate. Are these textboxes for deleting?
(In #1 you said tbstaartdate and tbenddate)
 
Upvote 0

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
I saw you have 2 userforms, in which "Input Leave" with tbstartdate and tbenddate (are they for input leave, not remove?)
and "Remove Lease" with rtbstartdate and rtbenddate. Are these textboxes for deleting?
(In #1 you said tbstaartdate and tbenddate)
Hey Bebo,

Yes thats correct the input leave is for adding the leave, And the remove leave is for removing the leave between the desired dates and that match the Employee number (Pnumber) entered
 
Upvote 0
Try this:
VBA Code:
Private Sub remove()
Dim lr&, cell As Range
With Worksheets("Mark Leave")
    lr = .Cells(Rows.Count, "B").End(xlUp).Row
    For Each cell In .Range("B2:B" & lr - 1)
        If cell.Value = rtbPnumber.Value And cell.Offset(0, 2).Value >= rtbstartdate.Value _
        And cell.Offset(0, 2).Value <= rtbstartdate.Value Then cell.Value = ""
    Next
    .Range("B2:B" & lr - 1).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End With
End Sub
 
Upvote 0
Try this:
VBA Code:
Private Sub remove()
Dim lr&, cell As Range
With Worksheets("Mark Leave")
    lr = .Cells(Rows.Count, "B").End(xlUp).Row
    For Each cell In .Range("B2:B" & lr - 1)
        If cell.Value = rtbPnumber.Value And cell.Offset(0, 2).Value >= rtbstartdate.Value _
        And cell.Offset(0, 2).Value <= rtbstartdate.Value Then cell.Value = ""
    Next
    .Range("B2:B" & lr - 1).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End With
End Sub

Thanks for the above bepo,

When i try and run it I get a "Run time error '1004': No Cells were found.

And then it highlights this line of code:

.Range("B2:B" & lr - 1).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
 
Upvote 0
Thanks for the above bepo,

When i try and run it I get a "Run time error '1004': No Cells were found.

And then it highlights this line of code:

.Range("B2:B" & lr - 1).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Bump
 
Upvote 0
Copy of Sick Leave Tracker V4.xlsm
ABCDEF
1IDP NumberNameDateLeave Type
2W07805344743W07805301-07-22AL
3W07805344746W07805304-07-22AL
4W07805344747W07805305-07-22AL
5W07805344748W07805306-07-22AL
6W07805344749W07805307-07-22AL
7W07805344750W07805308-07-22AL
8W07805344753W07805311-07-22AL
9W07805344754W07805312-07-22AL
10W07805344755W07805313-07-22AL
11W07805344756W07805314-07-22AL
12W07805344757W07805315-07-22AL
13W07805344760W07805318-07-22AL
14W07805344761W07805319-07-22AL
15W07805344762W07805320-07-22AL
16P65446444747P65446405-07-22AL
17P65446444748P65446406-07-22AL
18P65446444749P65446407-07-22AL
19P65446444750P65446408-07-22AL
20
21
22
23
24
25
26
27
28
29
30
Mark Leave
Cells with Data Validation
CellAllowCriteria
D2:D1048576Date>1/01/2021
E2:E1048576List=Settings!$B$2:$B$4
 
Upvote 0
I read through this post and an now that you have your "Mark Leave" sheet posted, perhaps you could post the other significant data using XL2BB as well. Or maybe give us some sample variables that could be bounced against this sheet to delete the desired rows.
 
Upvote 0
I read through this post and an now that you have your "Mark Leave" sheet posted, perhaps you could post the other significant data using XL2BB as well. Or maybe give us some sample variables that could be bounced against this sheet to delete the desired rows.
There isnt really any other significant data, as the other sheet just uses a Vlookup from this sheet. the only other thing as mentioned above is the user would need to input the P Number and the start and end date of the leave in a userform, which will then delete it from the table above.

As an example you can use P654464 with the date range 05/07/2022 - 08/07/2022
 
Upvote 0
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
 
Upvote 0
Solution

Forum statistics

Threads
1,215,063
Messages
6,122,934
Members
449,094
Latest member
teemeren

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