How to automatically delete rows in Excel based in day of week and date?

pr0no

New Member
Joined
Aug 4, 2012
Messages
1
Consider the following (partial) Excel worksheet:
Code:
    A  |   B         |  C    |  D
    ---+-------------+-------+-------
    id |  date       | var_a | var_b
    1  |  2011-03-12 | 200   | 34.22
    1  |  2011-03-13 | 203   | 35.13
    1  |  2011-03-14 | 205   | 34.14
    1  |  2011-03-15 | 207   | 54.88
    1  |  2011-03-16 | 208   | 12.01
    1  |  2011-03-17 | 208   | 12.01
    1  |  2011-03-18 | 203   | 76.10
    1  |  2011-03-19 | 210   | 14.86
    1  |  2011-03-20 | 200   | 25.45
    .  |  .          |  .    |  .
    .  |  .          |  .    |  .
    2  |  2011-03-12 | 200   | 34.22
    2  |  2011-03-13 | 203   | 35.13
    2  |  2011-03-14 | 205   | 34.14
    2  |  2011-03-15 | 207   | 54.88
    2  |  2011-03-16 | 208   | 12.01
    2  |  2011-03-17 | 208   | 12.01
    2  |  2011-03-18 | 203   | 76.10
    2  |  2011-03-19 | 210   | 14.86
    2  |  2011-03-20 | 200   | 25.45
    .  |  .          |  .    |  .
    .  |  .          |  .    |  .
In reality, there are over (the same) 500 dates for each of 100 companies (identified by id). I need to delete all rows which date falls on a saturday or sunday. In the example, March 12 and 13 (2011-03-12/13) and March 19 and 20 are Saturdays and Sundays. Second, I need to delete all rows with date 2011-07-04 (independence day; monday) and 2011-12-23 (friday before christmas).


Is this possible to do with either a formula or VBScript? I have never written a VBScript macro before (I have never had a use for it) so I would appreciate some help.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Upvote 0
If you would like to try using VBA, this assumes your dates are in column B with either a header or date in B1:
Code:
Sub RemoveCertainDates()
Dim lR As Long, R As Range, dRw As Range, vA As Variant, i As Long
Const H1 As Date = #7/4/2011#
Const H2 As Date = #12/23/2011#

lR = Range("B" & Rows.Count).End(xlUp).Row
Set R = Range("B1", "B" & lR)
vA = R.Value
For i = LBound(vA, 1) To UBound(vA, 1)
    If IsDate(vA(i, 1)) Then
        Select Case Weekday(vA(i, 1))
            Case 1, 7, Weekday(H1), Weekday(H2)
                If dRw Is Nothing Then
                    Set dRw = R.Rows(i)
                Else
                    Set dRw = Union(dRw, R.Rows(i))
                End If
        End Select
    End If
Next i
If Not dRw Is Nothing Then dRw.EntireRow.Delete
End Sub
 
Upvote 0
This worked perfectly! Thank you!


If you would like to try using VBA, this assumes your dates are in column B with either a header or date in B1:
Code:
Sub RemoveCertainDates()
Dim lR As Long, R As Range, dRw As Range, vA As Variant, i As Long
Const H1 As Date = #7/4/2011#
Const H2 As Date = #12/23/2011#

lR = Range("B" & Rows.Count).End(xlUp).Row
Set R = Range("B1", "B" & lR)
vA = R.Value
For i = LBound(vA, 1) To UBound(vA, 1)
    If IsDate(vA(i, 1)) Then
        Select Case Weekday(vA(i, 1))
            Case 1, 7, Weekday(H1), Weekday(H2)
                If dRw Is Nothing Then
                    Set dRw = R.Rows(i)
                Else
                    Set dRw = Union(dRw, R.Rows(i))
                End If
        End Select
    End If
Next i
If Not dRw Is Nothing Then dRw.EntireRow.Delete
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,609
Messages
6,131,723
Members
449,667
Latest member
PSAv

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