Avatar of Antonio King
Antonio King
 asked on

Excel - VBA "Object doesn't support this property or method"

I have an Excel workbook that executes a macro when being opened. During the process the macro creates a number of worksheets and then saves itself and closes.

Prior to saving I would like a certain worksheet to be selected. So if I ever was to open the workbook  I don't have to change worksheets.

This code usually works well...
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    ThisWorkbook.Worksheets("Create Report").Select
    ThisWorkbook.Worksheets("Create Report").Range("A1").Select
End Sub

Sometimes there are 2 or more workbooks open at once.
If the workbook is not selected when the macro runs this error is generated...
"Object doesn't support this property or method" for this line ThisWorkbook.Worksheets("Create Report").Select

How can I work around this? I want the macro to set "Create Report" as the active/selected sheet in the workbook and then close it without the workbook itself having to be the focused/active window.
VBAMicrosoft ExcelMicrosoft Office

Avatar of undefined
Last Comment
Roy Cox

8/22/2022 - Mon
Shums Faruk

Hi,

Try below:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
ThisWorkbook.Worksheets("Create Report").Activate
    ThisWorkbook.Worksheets("Create Report").Range("A1").Select
End Sub

Open in new window

Antonio King

ASKER
Hi Shums
That also generates the same RunTime error.
Shums Faruk

Hi Alan,

Sample data would be helpful with your code.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER CERTIFIED SOLUTION
Professor J

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Antonio King

ASKER
Thanks for this!
This works great.
Professor J

you are welcome. i am glad i was able to help.
Roy Cox

In such cases I would use an alternative

Option Explicit

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Application.Goto ThisWorkbook.Worksheets("Create Report").Range("A1")
End Sub

Open in new window


Adding  scroll:=True will force the sheet to scroll to the range

Option Explicit

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Application.Goto ThisWorkbook.Worksheets("Create Report").Range("A1"), scroll:=True
End Sub

Open in new window

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.