worksheets(1).Name gives an error

macroNubee

New Member
Joined
Apr 9, 2002
Messages
5
hi,
does anyone know why the statment

Worksheets(1).Name

give an error even when that sheet exists.
It works fine on most of my workbooks, but
in some workbooks, I can run

MsgBox Worksheets.Count

and know there are worksheets in the workbook, but I get an error when I try to access any of their names.

Thanx
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi
you probably have a space in the name.
this function (alone) can't handle that.

regards Tommy
 
Upvote 0
Works OK for me, what error are you getting? Remember that Worksheets(1) refers to the first worksheet in your workbook, regardless of when it was added. To refer to a sheet explicitly, try using Sheet1 (the actual name will be in the VBA explorer) or Worksheets("Worksheet_Name"), but remember that you will get an error if you try and rename a sheet to the name of a sheet that already exists.
 
Upvote 0
On 2002-04-19 10:38, Tommy Bak wrote:
Hi
you probably have a space in the name.
this function (alone) can't handle that.

regards Tommy

Sorry Tommy, it doesn't matter whether it has a space in there or not.
 
Upvote 0
I've noticed this behaviour as well. So I use Sheets for WorkSheets. It works both ways in the attached code. You could also be missing a object reference statement like With or Set. VBA statements may be written in a multitude of ways and still work, but miss one little letter and NADA!!

Also:
What did your Error statement tell you.
What was the Error Number and What was the text?

Option Explicit

Sub WorksheetNm()
Dim sheetnm As String
sheetnm = Worksheets(1).Name
MsgBox Sheets.Count
MsgBox Worksheets.Count
MsgBox (sheetnm)
End Sub


Yours in EXCELent Frustration
KniteMare
 
Upvote 0
thanks for your responses, but the
Worksheets(1).Name
method call does work for workbooks that have sheets named with spaces in them, it just doesn't seem to work for a specific file even though it does have two sheets in it. The following is the actual code:

For Each ws In Worksheets
If Trim(ws.Name) = "mySheet" Then
found = 1
End If
Next ws

the error says:

"Application defined or object defined error"
 
Upvote 0
Mudface > sorry, you are off caurse right. Space doesn't matter in this case.
It's just that I recently had a problem regarding names, but know I come to think of that, it was i the indirect function.



regards Tommy
 
Upvote 0
On 2002-04-19 10:50, macroNubee wrote:
thanks for your responses, but the
Worksheets(1).Name
method call does work for workbooks that have sheets named with spaces in them, it just doesn't seem to work for a specific file even though it does have two sheets in it. The following is the actual code:

For Each ws In Worksheets
If Trim(ws.Name) = "mySheet" Then
found = 1
End If
Next ws

the error says:

"Application defined or object defined error"

How did you dimension the variables? The following worked for me.

------------------
Sub test()
Dim ws As Worksheet, found As Byte

For Each ws In Worksheets
If Trim(ws.Name) = "mySheet" Then
found = 1
End If
Next ws
MsgBox found
End Sub
-------------------
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,383
Members
448,956
Latest member
JPav

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