Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Randy
 
Posts: n/a
Default Use Sheet CodeNames to Select Sheet in Different Workbook

I'm using an .xla to make modifications to another workbook. In my
procedure, I'm need to test the name of each worksheet to determine how
to proceed. For some reason, I am unable to correctly point to capture
the sheet name in the other workbook. I have tried a few different
methods, none with any success.

Here is the abbreviated code:

Sub UnprotectSheets(strUpdateWb As String)
'where strUpdateWb is the name of an open workbook

Windows(strUpdateWb).Activate
Dim wb As Workbook
Set wb = ActiveWorkbook

'Attempt #1
Dim temp As String
temp = wb.Sheet1.Name
MsgBox (temp) 'yields a run-time error. code stops.

'Attempt #2
temp = Sheet1.Name
MsgBox (temp) 'gives me the sheet name from the xla. I need the
sheet name from the xls workbook

'Attempt #3
temp = strUpdateWb.Sheet1.Name
MsgBox (temp) 'run time error again

End Sub

Can anybody point me in the right direction?

Thanks,
Randy Eastland

  #2   Report Post  
George Nicholson
 
Posts: n/a
Default

As you are using it Sheet1 is going to refer to the CodeName of a sheet. In
VBE's Project explorer a sheet appears as CodeName(Name on Tab). The
CodeName can be changed by changing the (Name) property of a sheet in VBE.
When a sheet is first created these names are the same but from that point
on changes to one are not reflected in the other. i.e., renaming the tab of
Sheet30 to Sheet1 won't change it's CodeName, which remains Sheet30.
Likewise, you can change the codename of a sheet in the VBE and always be
able to refer to it as such without worrying about what names the user may
have applied to the tabs (which is why the CodeName property exists).

#1 wb.Sheet1.Name - - I assume you got an error because the ActiveWorkbook
did not have a sheet with the CodeName "Sheet1". Deleted or changed
somewhere along the way.

#2 Sheet1.Name - - I guess that CodeName must use ThisWorkbook (the
Add-In) as the default (I'm a little surprised by this one). It evidently
has a Sheet1.

#3 strUpdateWb.Sheet1.Name - - strUpdateWb is a string, not a workbook, so
it failed on the workbook reference
(#3b) Workbooks(strUpdateWb).Sheet1.Name - - would have addressed the string
issue, but with results identical to #1

Maybe you can try something like:

for i = 1 to wb.Sheets.Count
temp = wb.Sheets(i).Name & " " & wb.Sheets(i).CodeName
msgBox temp
Next i

BTW, when working with Add-Ins, ActiveWorkbook defaults to the book that is
calling the add-in. You might not need to pass any values or activate any
workbooks.


HTH,
--
George Nicholson

Remove 'Junk' from return address.


"Randy" wrote in message
ups.com...
I'm using an .xla to make modifications to another workbook. In my
procedure, I'm need to test the name of each worksheet to determine how
to proceed. For some reason, I am unable to correctly point to capture
the sheet name in the other workbook. I have tried a few different
methods, none with any success.

Here is the abbreviated code:

Sub UnprotectSheets(strUpdateWb As String)
'where strUpdateWb is the name of an open workbook

Windows(strUpdateWb).Activate
Dim wb As Workbook
Set wb = ActiveWorkbook

'Attempt #1
Dim temp As String
temp = wb.Sheet1.Name
MsgBox (temp) 'yields a run-time error. code stops.

'Attempt #2
temp = Sheet1.Name
MsgBox (temp) 'gives me the sheet name from the xla. I need the
sheet name from the xls workbook

'Attempt #3
temp = strUpdateWb.Sheet1.Name
MsgBox (temp) 'run time error again

End Sub

Can anybody point me in the right direction?

Thanks,
Randy Eastland



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Subset of one sheet on another sheet bxb7668 Excel Discussion (Misc queries) 3 April 25th 05 03:55 PM
Subset of one sheet on another sheet bxb7668 Excel Worksheet Functions 3 April 25th 05 03:55 PM
Impoting data from Sheet 1 to Sheet 2 a-leano Excel Discussion (Misc queries) 1 April 20th 05 01:05 AM
how can i select all the cells with same color on a sheet if there are multipale colors by vba code uobt Charts and Charting in Excel 1 December 15th 04 05:27 PM
Naming & renaming a sheet tab Cgbilliar Excel Worksheet Functions 1 November 7th 04 05:57 PM


All times are GMT +1. The time now is 01:10 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"