PDA

View Full Version : Solved: If statement based on active worksheet Index Name



bananatang
04-12-2011, 04:58 AM
Hi,

Need abit of help please.

I am trying to write and if statement that will trigger a particular macro based on the active worksheets Index Name, as i need ensure the macro will work regardless if the user changes the worksheet name.

Any assistance will be greatly appreciated. I have tried loads of variables but still stuck on this.

Thanks

BT

nepotist
04-12-2011, 06:45 AM
Use the code name to reference a particular sheet. The code name for a sheet is unique, irrespective of its index and name. You can see the code name in the VBA environment on the left side. Example in the attached image, The workbook has three sheets The codename for the sheet name "Hello" is Sheet1.

This cannot be used if you are referencing worksheets from a different workbook.

Example


Sub Test()
Dim A As String
A = ActiveSheet.CodeName
If (A = "Sheet1") Then
MsgBox A
Else
MsgBox "Not the same"
End If
End Sub


if you change the name of the sheet form Hello to Fellow the result will still be the same

bananatang
04-12-2011, 07:32 AM
Hi Nepotist,

Thank you for your help. The bit of the code i did not know was "CodeName".

I should be able to take it from here.

Thank u kindly.
BT