Forum Discussion

DKoontz's avatar
DKoontz
Iron Contributor
Mar 29, 2021
Solved

VBA Squared Bracket Worksheet Reference

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 pre...
  • HansVogelaar's avatar
    Mar 29, 2021

    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.

Resources