how to use codenames with an addin?

G

Guest

I´m writing my first addin and I´m having the following problem:

I have a workbook which retrieves data from a database. With the addin I
create a new menu that has the update functions. One update function should
be renaming 6 sheets in the workbook. Of course, once renamed, the sheets
have a different name the next time I use the update, so I want to use the
codename of the sheets as the reference. But I read somewhere that I can´t
use codenames to reference to sheets in a different workbook, hence it
wouldn´t work from the addin either.

Is there a solution or workaround to this problem?
Thanks.
 
P

Peter T

Dim wb As Workbook
Dim ws As Worksheet
Dim sCodename As String
Dim n As Long

Set wb = Application.Workbooks("myFile.xls")

sCodename = "myCodename" ' case sensitive

For Each ws In wb.Worksheets
n = n + 1
If ws.CodeName = sCodename Then
Exit For
End If
Next

If n > wb.Worksheets.Count Then
MsgBox "not found"
Else
MsgBox ws.Name, , "ws no. " & n
End If

Bear in mind there are problems returning the codename of a new sheet that's
been inserted with the VBE closed and the file not saved.

Regards,
Peter T
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top