I'm trying to initialize an Excel worksheet by setting properties of some OLE Objects in the worksheet with the code below placed in a standard module.
With ThisWorkbook.Worksheets("UserView")
.btnShowAllItems.Enabled = False
.btnShowAllItems.Visible = False
.btnAvailableOnly.Enabled = True
.btnAvailableOnly.Visible = True
.AutoFilter.ShowAllData
End With
The above code works as intended except when the sub is called while Excel window is minimized or the user is in a different worksheet, resulting in the error below.
Run-time error '1004':
Unable to set the Enabled property of the OLEObject class
What am I doing wrong? How can I set the properties while the Excel window is minimized or when the user is in a different worksheet?
CodePudding user response:
Try adding .Object
when calling Enabled
- that fixed it for me
With ThisWorkbook.Worksheets("UserView")
.btnShowAllItems.Object.Enabled = False
.btnShowAllItems.Visible = False
.btnAvailableOnly.Object.Enabled = True
.btnAvailableOnly.Visible = True
.AutoFilter.ShowAllData
End With