View Single Post
  #8   Report Post  
Posted to microsoft.public.excel.misc
rk0909 rk0909 is offline
external usenet poster
 
Posts: 50
Default Why does this code not work?

Thanks all for your help. Much appreciated.

RK.

"Jim Thomlinson" wrote:

In your code you do not declare wks so it is a variant. When you execute
wks = "sheet" & n
you make wks into a string with the value sheet1 in it.
You then try ot use the string as an object which will not work. You will
gete a 424 object required error...

Try this...
Sub test()
Dim wks As Worksheet
Dim l As Long

l = 1
For Each wks In Worksheets
If wks.CodeName = "Sheet" & l Then Exit For
Next wks
MsgBox wks.CodeName
End Sub

That being said I highly recommend that you not use this code as it is IMO
not a great idea. While the end user can not change the code names of the
sheets you can still run into a problem. Delete the sheet with code name
Sheet1. Save the file and close it. Open it and create a new sheet. The new
sheet will have code name Sheet1. You are best off to rename the code names
of the sheets to avoid this problem... If you Delete a sheet with code name
shtMySheet it will never get recreated with that same code name...
--
HTH...

Jim Thomlinson


"rk0909" wrote:

All,

I am trying to use the code name of sheets instead of the tab name, but it
wouldn't work for me. Any help will be appreciated.

Thanks much,

RK

Sub test()
Dim n As Integer

n = 1
wks = "sheet" & n

wks.Select
End Sub