1

I am trying to activate a sheet from another sheet and display the cell value in a message box in excel but whenever i run it, there will be a subscript error, saying that subscript is out of range.

My code:

Sub Home()
Dim tbValue As String
tbValue = Worksheets("Home").TextBox1.Value

Worksheets(tbValue).Activate

MsgBox Cells(7,1).Value


End Sub

Anybody have any idea why subscript out of range ? Thanks

6
  • 1
    Does the sheet name you enter in Textbox1 actually exist as a sheet? If it does try wrapping your Textbox1 value with a TRIM to get rid of any extra spaces at the end: Trim(Worksheets("Home").TextBox1.Value) Commented Apr 18, 2017 at 9:19
  • Yeah it exists, I even tried hardcoding the sheet name instead of using tbValue but it is still the same error, run-time error 9, subscript out of range. Commented Apr 18, 2017 at 9:21
  • I don't think TextBox1 can't be accessed like that. It has to be the sheet object like Sheet1.TextBox1 or Worksheets("Home").Shapes("TextBox1")... Commented Apr 18, 2017 at 9:31
  • Which line does the error occur on? My previous comment would have it occur on the worksheet activate line. Does tbValue get a value correctly from TextBox1? Commented Apr 18, 2017 at 9:31
  • 1
    Without qualifying the worksheet in MsgBox Cells(7,1).Value it will be using the currently active sheet - you may be reading the cell value from the wrong sheet. @Vityata shows how to ensure it's looking at the right sheet with the With... End With block (or just putting the sheet name before Cells). Commented Apr 18, 2017 at 9:39

2 Answers 2

2

Subscript is out of range because worksheet name is not found. It may happen in both Worksheet(...) line codes.

  1. Worksheets("Home") may return subscript error because your active workbook may not be the one with your Home worksheet;

  2. Worksheets(tbValue) may fail by same first reason and because tbValue may not match exact sheet name.

First solution may be ensure correct book is active:

Sub Home()
   Dim tbValue As String
   Workbooks("your_workbook_name.xlsm").Activate
   tbValue = ThisWorkbook.Worksheets("Home").TextBox1.Value
   Worksheets(tbValue).Activate
   MsgBox Cells(7,1).Value
End Sub

Better solution is to avoid sheet and books activations and use full qualified objects. If your macro is in the same book as Home sheet:

Sub Home()
    Dim tbValue As String
    tbValue = ThisWorkbook.Worksheets("Home").TextBox1.Value
    MsgBox ThisWorkbook.Worksheets(tbValue).Cells(7,1)
End Sub

You can also replace Worksheets("Home") with VBA assigned name to worksheet, probably Sheet1 (you can check this name in IDE).

Sign up to request clarification or add additional context in comments.

4 Comments

I usually set my wb and ws hardcoded as this: Dim wb As Workbook Dim ws as Worksheet Set wb = Application.Workbooks("workbookname.xlsm") Set ws = wb.Worksheets("Sheetname") then you can just reference like: Msgbox ws.Cells(7,1)
But why create a new variable when you already have objects created? ThisWorkbook is previously defined, and doesn't depend on file name, which you may change!
To avoid complications when multiple workbooks are open.
How wb=Workbook("somename") is less complicated then ThisWorkbook?
0

The code looks workable, however, try the With/ End with and Option Explicit on top, it may work. Furthermore, it can be that .Cells(7,1) is an error or something. Anyhow, try again:

Option Explicit

Sub Home()
    Dim tbValue As String
    tbValue = trim(Worksheets("Home").TextBox1.text)

    with worksheets(tbValue)
        MsgBox .Cells(7,1)
    end with

End Sub

The idea for the Trim in the comments is also a good one. Just to go one step further, use TextBox1.Text. See more here - Distinction between using .text and .value in VBA Access

8 Comments

Now the msgbox appears, but it displays nothing, seems like it is unable to retrieve the value of the cells in the sheet
What is in there? This is A7.
How would Option Explict and With solve an subscript out of range?
@LS_dev - well, at least it solved something, considering the fact that the error does not show any more.
But you are aware that Option Explicit is a compiler and option and With is just grammatic suggar. Cells is much different from .Cells and this may be the explaination.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.