> Your code works fine for me. All I did is
>
[quoted text clipped - 47 lines]
> > >>ActiveSheet.PivotTables(""pivottable1"")"
> > >> .InsertLines .CountOfLines + 1, "
I skipped not important code.
All workbooks, workseets are created correctly, sModuleName is not
empty, null, etc.
if I replace RegionSheet.CodeName by "ThisWorkbook" it works.
if I insert just "Sub a end sub" it works.
if I insert event procedure it fails.
-------------------
Dim wkBook As Workbook
Set wkBook = AddWorkbook(...)
Dim RegionSheet As Worksheet
Set RegionSheet = AddWorksheet(wkBook, ...)
AddCode wkBook, RegionSheet.CodeName
-----------------
Sub AddCode(wkb As Workbook, sModuleName As String)
Dim VBCodeMod As CodeModule
Set VBCodeMod = wkb.VBProject.VBComponents(sModuleName).CodeModule
With VBCodeMod
.InsertLines .CountOfLines + 1, "Private Sub ComboBox1_Change()"
.InsertLines .CountOfLines + 1, " Dim TargetPivotTable As
PivotTable"
.InsertLines .CountOfLines + 1, " On Error GoTo ErrHandler"
.InsertLines .CountOfLines + 1, " Set TargetPivotTable =
ActiveSheet.PivotTables(""pivottable1"")"
.InsertLines .CountOfLines + 1, " Worksheet_PivotTableUpdate
TargetPivotTable"
.InsertLines .CountOfLines + 1, "ErrHandler:"
.InsertLines .CountOfLines + 1, "End Sub"
end with
end sub
>>On which point - how/where did you define "Sheet1"
>
[quoted text clipped - 82 lines]
>>>>>
>>>>>What am I doing wrong?
Peter T - 25 Jan 2006 16:57 GMT
As I said, the code you posted works fine for me, inserting an event proc
for ComboBox1 into a WorkSheet module.
The problem might be in code that you have not posted, eg inserting the
ActiveX combobox, which certainly could cause problems if inserting into
same project that's running the code.
Regards,
Peter T
> I skipped not important code.
> All workbooks, workseets are created correctly, sModuleName is not
[quoted text clipped - 123 lines]
> >>>>>
> >>>>>What am I doing wrong?
Kris - 25 Jan 2006 17:17 GMT
It is inserted into new workbook, so it's not the case.
I don't know why it doesn't work.
Thanks anyway.
> As I said, the code you posted works fine for me, inserting an event proc
> for ComboBox1 into a WorkSheet module.
[quoted text clipped - 133 lines]
>>>>>>>
>>>>>>>What am I doing wrong?
Kris - 25 Jan 2006 17:28 GMT
I changed the order.
I add code before I insert combobox.
Now it works.
in previous code I inserted combobox and code was the last element
added to worksheet.
> As I said, the code you posted works fine for me, inserting an event proc
> for ComboBox1 into a WorkSheet module.
[quoted text clipped - 133 lines]
>>>>>>>
>>>>>>>What am I doing wrong?