Share via

script out of range error

Anonymous
2020-01-19T20:59:52+00:00

Starting a little project but have hit my first wall. When the Workbook opens I would like the code below to run but when it runs, I get a script out of range error and it highlights If Worksheets("Sheet1").Range("O2") = "" Then

Any thoughts on what the issue might be?

Private Sub Workbook_Open()

If Worksheets("Sheet1").Range("O2") = "" Then

MsgBox "hello"

End If

End Sub

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2020-01-19T21:48:31+00:00

Sheet1 is the code name. Its sheet name is June 19. So use either

Private Sub Workbook_Open()

    If Worksheets("June 19").Range("O2") = "" Then

        MsgBox "hello"

    End If

End Sub

or

Private Sub Workbook_Open()

    If Sheet1.Range("O2") = "" Then

        MsgBox "hello"

    End If

End Sub

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

7 additional answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. Anonymous
    2020-01-19T21:40:20+00:00

    I do have a sheet1 but am still getting the same error.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2020-01-19T21:25:38+00:00

    To Ordnance1,

    Thanks for reaching out.This is Gems88 an Independent Advisor. I'd be more than happy to help you.

    I tried to run your code in my excel and I did not encounter any error

    Could you please create a new workbook, then go to sheet1,right click,click view code and then right click in the project explorer,then choose insert then choose module,then paste this code

    Private Sub Workbooktest()

    If Worksheets("Sheet1").Range("O2") = "" Then

    MsgBox "hello"

    End If

    End Sub

    If there is anything else I can help you with, please let me know.

    Regards,

    Gems88

    Was this answer helpful?

    0 comments No comments
  4. HansV 462.6K Reputation points
    2020-01-19T21:09:21+00:00

    Are you sure that you have a sheet named Sheet1?

    If so: sometimes the workbook isn't "ready" yet during Workbook_Open. You might try a small delay:

    1. Insert a new module and copy the following code into it.

    Sub MyTest()

        If Worksheets("Sheet1").Range("O2") = "" Then

            MsgBox "hello"

        End If

    End Sub

    1. Change the Workbook_Open event procedure to

    Private Sub Workbook_Open()

        Application.OnTime Now + TimeSerial(0, 0, 2), "MyTest"

    End Sub

    This will run MyTest 2 seconds after the workbook has been opened. If that works, you can try changing 2 to 1 in the code.

    Was this answer helpful?

    0 comments No comments