View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Peter T Peter T is offline
external usenet poster
 
Posts: 5,600
Default How to uniquely identify worksheet

Hi Vint,

I don't know VC++ but am curious as to why attempting to return a sheet's
codename returns blank, or an empty string. In VBA this same problem can
occur trying to return codename of a newly inserted sheet until the workbook
has been saved or resaved (but there are workarounds). However I assume
that's not the scenario in your case.

If you have access to the workbook at an early stage how about inserting a
(hidden) name that refers to a range on your required sheet. Then later you
can return the named range's parent name (ie sheet name). In vba, something
like this:

Sub test()

ActiveWorkbook.Names.Add "MyName", _
Worksheets("Sheet1").Range("A1:z1000")
'ActiveWorkbook.Names("MyName").Visible = False

Worksheets("Sheet1").Name = "Sheet1 Old"

shtName = Range("MyName").Parent.Name
End Sub

If user deletes entire rows or cols of the named range, the last line in the
example will error. Not serious but would need to trap for that and parse
for the sheet name which will still exist. Look for the first "!" and
possibly embraced apostrophes in the string returned with:
s = ActiveWorkbook.Names("MyName")
and remove the initial "="

Regards,
Peter T


"Vinit" wrote in message
oups.com...
Thanks Patrick,

My problem is in my program I take a cell location from user(e.g.
Sheet1!A1). and inserts value in it using
Worksheets("Sheet1").Range("A1").Value=10.
I again want to insert value in it but now sheet1 is renamed to
Sheet1_old. Now How can I insert the value using the above macro. I
cant use Codename(Changing the sheet name doesn't change the code
name) in VC++