Martin sherk
Board Regular
- Joined
- Sep 11, 2022
- Messages
- 94
- Office Version
- 365
- 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.
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 ?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
Thank you! is there a way to keep everything from previous months till today and remove anything after this month?Look into the date column filter. There are things like this month, next month etc.
Then update the formula likenot Date.IsInNextMonth([Custom])
View attachment 80633
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?would be like
Power Query:Table.SelectRows(PreviousStep, each ( Date.IsInPreviousMonth( [DateCol] ) or [DateCol] <= Date.From( DateTime.LocalNow() ) ) and not Date.IsInNextMonth( [DateCol] ) )
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.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.
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?Thank you! is there a way to keep everything from previous months till today and remove anything after this month?