Click here to Skip to main content
15,915,603 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to work on VBA script for a project and re-pick up some old staffs used a long time ago. For such simple lines of code:
VB
Sheets("Sheet1").Select

or
VB
Sheets("Sheet2").Cells(2, 5).Value = 100

I got Runtime eoor '9' Script out of range. What's wrong in the code? Thanks.
Posted

Ensure a sheet by the name Sheet2 exists.
 
Share this answer
 
Comments
[no name] 2-Apr-14 12:04pm    
It does exist. See Solution 1. Thanks.
Maciej Los 2-Apr-14 12:22pm    
+5!
Please, see my answer ;)
Abhinav S 2-Apr-14 13:16pm    
Thank you.
Problem fixed by revising the code like that below,
VB
Sub One()
    Dim shTrklon  As Worksheet
    Set shTrklon = Worksheets(2)
    shTrklon.Select
    shTrklon.Cells(2, 5).Value = 100
End Sub
 
Share this answer
 
Comments
CHill60 2-Apr-14 11:56am    
Is that because the worksheets were not called "Sheet1" and "Sheet2"?
[no name] 2-Apr-14 11:59am    
No. See Solution 1. Thanks.
Maciej Los 2-Apr-14 12:57pm    
No, no, no... ;(
Please, see my answer. There you'll find many usefull information.
[no name] 2-Apr-14 13:01pm    
Los: Using the code in Solution 1 works well. I believe yours in Solution 3 works also. Thanks.
Quote:
(...)This error has the following causes and solutions:

  • You referenced a nonexistent array element.
  • You declared an array but didn't specify the number of elements.
  • You referenced a nonexistent collection member.
  • You used a shorthand form of subscript that implicitly specified an invalid element.


More details about Error 9 you'll find here: Error 9: subscript out of range[^]


Maybe Solution1 solves the issue, but... i need to comment it.

  1. There are known differences between using Sheet vs. Worksheet object.
    Excel's Worksheets and Sheets Collection - What's the Difference?[^]
  2. Secondly, the code has no context!
    Let say, there are 2 opened workbooks. Activate the first one and run below code:
    VB
    Worksheets(2).Range("A1") = "Stupid code"

    Now, activate another one and run the code again.
    What's happend? Workbook1 and Workbook2 have been changed. To avoid this behaviour, define the context:
    VB
    ThisWorkbook.Worksheets(2).Range("A1") = "Excellent code!"

    No matter of which workbook is actually active, only one workbook is modified by code.
  3. If you're beginner programmer, it's strongly recommended to call element from collection by its name.
    VB
    Dim wsh = ThisWorkbook.Worksheets("Sheet1")

    Why? Change the order of woksheets! Worksheets(2) can refer to Sheet1, Sheet2, Sheet3, etc.
    In some special cases, CodeName[^] is used.
  4. Do not use Select method, unless it's necessary!
    It's bad practice of beginner programmers. It does not help! When code is executed, Excel window is going blinking! If some other actions are binded with Sheet_Change event, Excel is going crazy jumping from one procedure to another! It wasting the time and resources!
  5. Learn from masters!
    Visual Basic Coding Conventions[^]
    Excel VBA Performance Coding Best Practices[^]

 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900