Hi Everyone,
I'm trying to figure out why this code runs when a button is placed on the active SalesData sheet but not when I move the button to an alternative dashboard sheet. I've been using previous code as reference while writing, and the last user used worksheet references with square brackets ie. [SalesData], could it be something with this syntax? The square brackets have been used to call other worksheets throughout the whole file and they work fine.
I wrote this to delete any rows older than 1 year old, which works, but only works when the macro button is placed on SalesData. Any ideas?
Thank you so much! I'm still new to VBA but have been really enjoying learning.
I'm trying to figure out why this code runs when a button is placed on the active SalesData sheet but not when I move the button to an alternative dashboard sheet. I've been using previous code as reference while writing, and the last user used worksheet references with square brackets ie. [SalesData], could it be something with this syntax? The square brackets have been used to call other worksheets throughout the whole file and they work fine.
I wrote this to delete any rows older than 1 year old, which works, but only works when the macro button is placed on SalesData. Any ideas?
VBA Code:
Sub DeleteOldRows()
Dim FilterRange As Range
Dim myDate As Date
Dim dte As Date
' Sets date to user input date and deletes anything older than 12 months from input date
dte = InputBox("Please Enter Date: (MM/DD/YYYY)", Default:=Format(Now, "mm/dd/yyyy"))
myDate = DateSerial(Year(dte), Month(dte) - 12, Day(dte))
' Set filter range and filter based on date
Set FilterRange = [SalesData].Range("A1:Q" & Cells(Rows.Count, 1).End(xlUp).Row)
FilterRange.AutoFilter Field:=16, Criteria1:="<=" & (myDate)
On Error Resume Next
' Delete filtered rows
With FilterRange
.Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
Err.Clear
[SalesData].AutoFilterMode = False
End Sub
Thank you so much! I'm still new to VBA but have been really enjoying learning.