0

I'm using form buttons with macros assigned to them on an instructions page, and according to which button is selected a range of rows is hidden.

I am getting a Method of range class failed error for each of my macros, but if I debug the code and run it when I am on the AM Sheet, the code works.

How can I modify this so that it works from the instructions page? Did I forget to specify something?

Sub Daily()

Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("AM")

ws.Rows("22:80").Select

'Hide everything below daily
Selection.EntireRow.Hidden = True

ws.PageSetup.PrintArea = "$A$1:$I$21"

'Unselect everything
Range("A1").Select

Application.CutCopyMode = False

End Sub

eenz
  • 143
  • 10
  • Which line has the problem? – Dominique Oct 14 '20 at 12:12
  • 7
    Fairly sure you can only run a `.select` for a specific sheet if you are in the actual sheet. Hide without selecting should work. `ws.Rows("22:80").EntireRow.Hidden = True` – Christofer Weber Oct 14 '20 at 12:15
  • 1
    You might benefit from reading [How to avoid using Select in Excel VBA](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba). This will solve your issue. – Pᴇʜ Oct 14 '20 at 12:21
  • The problems in the line 'ws.Rows("22:80").Select' – eenz Oct 14 '20 at 12:22
  • Where is this procedure (the code) located? Where is the button located? In which worksheet would you like to select `Range("A1")`? – VBasic2008 Oct 14 '20 at 12:23
  • Range("A1").Select. This will work on the ActiveSheet unless you qualify it. If the active sheet doesn't have an "A1" you could run into problems. This is why it probably works when you are on the "AM" sheet, and doesn't if you are not. Try ws.Range("A1").Select instead. And as the comment above says, you often don't need to use Select() at all ... – DS_London Oct 14 '20 at 12:27

0 Answers0