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.
Antonio KingIT ManagerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ShumsDistinguished Expert - 2017Commented:
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

0
Antonio KingIT ManagerAuthor Commented:
Hi Shums
That also generates the same RunTime error.
0
ShumsDistinguished Expert - 2017Commented:
Hi Alan,

Sample data would be helpful with your code.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

ProfessorJimJamMicrosoft Excel ExpertCommented:
you need to avoid using the direct object Select for this case.

try this
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
 Set CR = ThisWorkbook.Worksheets("Create Report")
    CR.Select
    CR.Range("A1").Select
End Sub

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Antonio KingIT ManagerAuthor Commented:
Thanks for this!
This works great.
0
ProfessorJimJamMicrosoft Excel ExpertCommented:
you are welcome. i am glad i was able to help.
0
Roy CoxGroup Finance ManagerCommented:
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

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.