I am using the following code to insert an Event Handler into a specific sheet. I am getting inconsistent results in what sheet actually get the code inserted.
The sheet I am placing the event handler onto is added to the sheet by the code. The new sheets name is Always Sheet1 and the code name is always Sheet9. Sometime the index returned is 1 and sometimes it is 9. Why am I getting these different results and is there a better way to have it detect the index to use?
Code to add the event handler(Code is the var that contains the event handler code):
The sheet is added using the following:
The CTB Summary sheet is the sheet that the code will be placed on in error.
When I look in the VBA editor I see the sheets listed as Sheet1(CTB Summary) and Sheet9(Sheet1).
Any help would be greatly appreciated. I am using Excel 2007.
The sheet I am placing the event handler onto is added to the sheet by the code. The new sheets name is Always Sheet1 and the code name is always Sheet9. Sometime the index returned is 1 and sometimes it is 9. Why am I getting these different results and is there a better way to have it detect the index to use?
Code to add the event handler(Code is the var that contains the event handler code):
Code:
With ActiveWorkbook.VBProject.VBComponents("Sheet" & ActiveSheet.Index).CodeModule
.insertlines .CountOfLines + 1, Code
End With
The sheet is added using the following:
Code:
Sheets("CTB Summary").Select 'This sheet is the first sheet in the workbook.
Sheets.Add
The CTB Summary sheet is the sheet that the code will be placed on in error.
When I look in the VBA editor I see the sheets listed as Sheet1(CTB Summary) and Sheet9(Sheet1).
Any help would be greatly appreciated. I am using Excel 2007.