VBA Squared Bracket Worksheet Reference

DKoontz

New Member
Joined
Mar 29, 2021
Messages
4
Office Version
  1. 365
Platform
  1. Windows
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?

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.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hi. Try to add the red part, as below.

Set FilterRange = [SalesData].Range("A1:Q" & [SalesData].Cells(Rows.Count, 1).End(xlUp).Row)
 
Upvote 0
Solution
Hi. Try to add the red part, as below.

Set FilterRange = [SalesData].Range("A1:Q" & [SalesData].Cells(Rows.Count, 1).End(xlUp).Row)
This worked! I thought I was covered with the first [SalesData] reference, didn't realize I needed another one for cells too. Thank you!
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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