SOLVED

VBA Squared Bracket Worksheet Reference

%3CLINGO-SUB%20id%3D%22lingo-sub-2243202%22%20slang%3D%22en-US%22%3EVBA%20Squared%20Bracket%20Worksheet%20Reference%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2243202%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3EHi%20Everyone%2C%3C%2FSPAN%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%3EI'm%20trying%20to%20figure%20out%20why%20this%20code%20runs%20when%20a%20button%20is%20placed%20on%20the%20active%20SalesData%20sheet%20but%20not%20when%20I%20move%20the%20button%20to%20an%20alternative%20dashboard%20sheet.%20I've%20been%20using%20previous%20code%20as%20reference%20while%20writing%2C%20and%20the%20last%20user%20used%20worksheet%20references%20with%20square%20brackets%20ie.%20%5BSalesData%5D%2C%20could%20it%20be%20something%20with%20this%20syntax%3F%20The%20square%20brackets%20have%20been%20used%20to%20call%20other%20worksheets%20throughout%20the%20whole%20file%20and%20they%20work%20fine.%3C%2FSPAN%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%3EI%20wrote%20this%20to%20delete%20any%20rows%20older%20than%201%20year%20old%2C%20which%20works%2C%20but%20only%20works%20when%20the%20macro%20button%20is%20placed%20on%20SalesData.%20Any%20ideas%3F%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-visual%22%3E%3CCODE%3ESub%20DeleteOldRows()%0A%0ADim%20FilterRange%20As%20Range%0ADim%20myDate%20As%20Date%0ADim%20dte%20As%20Date%0A%0A'%20Sets%20date%20to%20user%20input%20date%20and%20deletes%20anything%20older%20than%2012%20months%20from%20input%20date%0Adte%20%3D%20InputBox(%22Please%20Enter%20Date%3A%20(MM%2FDD%2FYYYY)%22%2C%20Default%3A%3DFormat(Now%2C%20%22mm%2Fdd%2Fyyyy%22))%0AmyDate%20%3D%20DateSerial(Year(dte)%2C%20Month(dte)%20-%2012%2C%20Day(dte))%0A%0A'%20Set%20filter%20range%20and%20filter%20based%20on%20date%0ASet%20FilterRange%20%3D%20%5BSalesData%5D.Range(%22A1%3AQ%22%20%26amp%3B%20Cells(Rows.Count%2C%201).End(xlUp).Row)%0AFilterRange.AutoFilter%20Field%3A%3D16%2C%20Criteria1%3A%3D%22%26lt%3B%3D%22%20%26amp%3B%20(myDate)%0A%0AOn%20Error%20Resume%20Next%0A%0A'%20Delete%20filtered%20rows%0AWith%20FilterRange%0A%20%20%20%20.Offset(1).Resize(.Rows.Count%20-%201).SpecialCells(xlCellTypeVisible).EntireRow.Delete%0AEnd%20With%0A%0AErr.Clear%0A%0A%5BSalesData%5D.AutoFilterMode%20%3D%20False%0A%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you!%20I'm%20loving%20my%20time%20spent%20with%20VBA%20so%20far.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2243202%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Frequent Contributor

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?

 

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! I'm loving my time spent with VBA so far.

 

2 Replies
best response confirmed by DKoontz (Frequent Contributor)
Solution

@DKoontz 

In the line

 

Set FilterRange = [SalesData].Range("A1:Q" & Cells(Rows.Count, 1).End(xlUp).Row)

 

Cells and Rows refer to the active sheet, so if that is a different sheet than SalesData, Cells(Rows.Count, 1).End(xlUp).Row will not return the correct value. Does it work if you change the above line to

 

With [SalesData]
    Set FilterRange = .Range("A1:Q" & .Cells(.Rows.Count, 1).End(xlUp).Row)
End With

 

Note the . before Range, Cells and Rows.

This works perfectly! I didn't know that was how it should be typed when referring to another sheet, I thought because I set FilterRange with reference to [SalesData] it would know that I was referring to that specific sheet. Should be an easy fix to change my other subs. Thank you so much!