Remove data in a column if dated next month

Martin sherk

Board Regular
Joined
Sep 11, 2022
Messages
94
Office Version
  1. 365
  2. 2016
How to delete rows based on the date in the column (Due date), if it's dated next month, say if date in that column is dated Jan 23. i want it to be removed.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hi. Give the below a test.

VBA Code:
Sub deletenxtmo()

Dim ws As Worksheet: Set ws = ThisWorkbook.ActiveSheet
Dim x As Long

With ws

    'finds last row
    'Set the column (currently G) to column with due dates
    Dim lrow As Long: lrow = .Range("G" & .Rows.Count).End(xlUp).Row
    'Change 7 to the index of the column with due dates (i.e.; G = 7)
    Dim ddCol As Integer: ddCol = 7

    'will work from bottom up and delete rows where due date month or
    'year is greater than current month or year
    For x = lrow To 2 Step -1
        If Month(.Cells(x, ddCol)) > Month(Date) Or Year(.Cells(x, ddCol)) > Year(Date) Then
            .Rows(x).Delete
        End If
    Next x

End With

End Sub
 
Upvote 0
Hi. Give the below a test.

VBA Code:
Sub deletenxtmo()

Dim ws As Worksheet: Set ws = ThisWorkbook.ActiveSheet
Dim x As Long

With ws

    'finds last row
    'Set the column (currently G) to column with due dates
    Dim lrow As Long: lrow = .Range("G" & .Rows.Count).End(xlUp).Row
    'Change 7 to the index of the column with due dates (i.e.; G = 7)
    Dim ddCol As Integer: ddCol = 7

    'will work from bottom up and delete rows where due date month or
    'year is greater than current month or year
    For x = lrow To 2 Step -1
        If Month(.Cells(x, ddCol)) > Month(Date) Or Year(.Cells(x, ddCol)) > Year(Date) Then
            .Rows(x).Delete
        End If
    Next x

End With

End Sub
perfect solution but i wonder if it can be done in power query ?
 
Upvote 0
Look into the date column filter. There are things like this month, next month etc.
Then update the formula like not Date.IsInNextMonth([Custom])
1670756410136.png
 
Upvote 0
would be like
Power Query:
Table.SelectRows(PreviousStep, each ( Date.IsInPreviousMonth( [DateCol] ) or [DateCol] <= Date.From( DateTime.LocalNow() ) ) and not Date.IsInNextMonth( [DateCol] ) )
 
Upvote 0
Solution
would be like
Power Query:
Table.SelectRows(PreviousStep, each ( Date.IsInPreviousMonth( [DateCol] ) or [DateCol] <= Date.From( DateTime.LocalNow() ) ) and not Date.IsInNextMonth( [DateCol] ) )
I'm sorry, i am new to this. how to add this line to my query?

what comes before the equal sign, what should i type
 
Upvote 0
First activate the formula bar in the UI.
Then add a simple filter at that point in your Query where it is needed. Look in formula bar and update the formula like in my example.
else provide your code so we can help you more explicitely.
 
Upvote 0
First activate the formula bar in the UI.
Then add a simple filter at that point in your Query where it is needed. Look in formula bar and update the formula like in my example.
else provide your code so we can help you more explicitely.
i got it to work but it's removing data that is greater than today not greater than this month, it removes data that is in December, it removed data dated 16 Dec. and 21 Dec and above.
 
Upvote 0
Thank you! is there a way to keep everything from previous months till today and remove anything after this month?
You said till today, so that's why I updated the formula. Just leave out that argument and you are probably good to go, no?
 
Upvote 0

Forum statistics

Threads
1,223,400
Messages
6,171,891
Members
452,431
Latest member
TiffanyMcllwain

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