Setting worksheet name to variable doesn't work

mark hansen

Well-known Member
Joined
Mar 6, 2006
Messages
534
Office Version
  1. 2016
Platform
  1. Windows
I have the following code that seemed to stop working...

Dim ws, bf As Worksheet

'On Error GoTo ErrHandler
Set ws = Sheets(8)
Set bf = Sheets(9)

When I step through it (F8) I get a Error 9 Subscript out of range. The code is on sheet 8 and I want to set WS as sheet 8 and BF as sheet 9.

What could cause this to stop working. It was working when I wrote and stepped through it before?

Thanks,
Mark
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Mark

How many worksheets do you have in the workbook and what are their names?
 
Upvote 0
I have four work sheets 10, 5, 8, 9

In order above Configuration, DashBoard, New Data, Blue form.

I find it interesting before I removed the older worksheets, the code worked as expected. I removed the older data sheets because they were large and out of date.
 
Upvote 0
If you have named your worksheets with a numeric value only then you will need to coerce it in your code to a string value otherwise, code treats it as an index value which may explain why you get the error message.

Try

Rich (BB code):
Set ws = Sheets(CStr(8))

and see if this solves your problem

Dave
 
Last edited:
Upvote 0
Mark

What are the worksheet names?

Is it 10, 5, 8, 9 or 'Configuration', 'DashBoard', 'New Data', 'Blue form'?
 
Upvote 0
Sheet 10 is "Configuration"
sheet 5 is "Dashboard"
Sheet 8 is "New Data"
Sheet 9 is "Blue Form"

The Cstr does work, but I don't want to have numbers as the worksheet name. Wouldn't it bring be right to messing with the code if the sheet name changes? That's why I like to go with the Sheet number when I can.

This is a strange one to me, I don't know why deleting other worksheets would break something that seems so basic. In this case I started with an existing workbook from someone... I think I'll start from scratch.
 
Upvote 0
Sheet 10 is "Configuration"
sheet 5 is "Dashboard"
Sheet 8 is "New Data"
Sheet 9 is "Blue Form"

The Cstr does work, but I don't want to have numbers as the worksheet name. Wouldn't it bring be right to messing with the code if the sheet name changes? That's why I like to go with the Sheet number when I can.

This is a strange one to me, I don't know why deleting other worksheets would break something that seems so basic. In this case I started with an existing workbook from someone... I think I'll start from scratch.

When you place a number between the parentheses of the Sheet object, that is an index to the displayed sheets, it is not sheet name. When you say Sheet(8), you are telling it to count over 8 sheets from the left and use that worksheet. Since you only have 4 worksheets, Excel cannot count over that many sheets, so it issued the subscript out of range. Your worksheet is (apparently) named "New Data", so that, quoted, is what should go between the parentheses... Sheet("New Data").
 
Last edited:
Upvote 0
just use the Sheet8 or Sheet9 these names never change no matter what order they are or what "Name" you give the sheet
 
Upvote 0
just use the Sheet8 or Sheet9 these names never change no matter what order they are or what "Name" you give the sheet
Unfortunately, the user can change those (code names) if he/she wants with very little trouble.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,648
Messages
6,120,725
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top