PDA

View Full Version : adding code in runtime



robje
07-07-2008, 04:18 AM
Hello,

When I try to add code in runtime after clicking a button, excel says it has come acros an error and it has to shut down.
I'm using excel 2003 sp2
and this is the code I'm using:


With Workbooks(BestandsNaam).VBProject.VBComponents("Blad2").CodeModule
.InsertLines .CountOfLines + 1, "private Sub " + "bLeverantieToevoegen" + typenVerzameling(typenVuller) + "_click()"
.InsertLines .CountOfLines + 1, " Sheets(""Algemeen"").Cells(1, 1).Value = ""Woning"" "
.InsertLines .CountOfLines + 1, " Sheets(""Algemeen"").Cells(1, 2).Value = ""Type Woning leverantie"" "
.InsertLines .CountOfLines + 1, " Sheets(""Algemeen"").Cells(1, 3).Value = ""Type Woning Uren"" "
.InsertLines .CountOfLines + 1, " Sheets(""Algemeen"").Cells(1, 4).Value = " + """" + "Leverantie Details(Groep: " + groepNaam + ", Type: " + typeNaam + "(" + nummer + "))" + """"
.InsertLines .CountOfLines + 1, " Sheets(""Algemeen"").Cells(1, 5).Value = " + """" + "Uren Details(Groep: " + groepNaam + ", Type: " + typeNaam + "(" + nummer + "))" + """"
.InsertLines .CountOfLines + 1, " Load Selecteren"
.InsertLines .CountOfLines + 1, " Selecteren.Show"
.InsertLines .CountOfLines + 1, "End Sub" & Chr(13)
End With


Can anyone see what I'm doing wrong here?

With Kind regards,
Rob

Edited by Aussiebear: To include code within the VBA Tags

Bob Phillips
07-07-2008, 04:56 AM
It is difficult to envisgae what the final code is meant to look like, what is variables, what is text, but is this any better?



With Workbooks(BestandsNaam).VBProject.VBComponents("Blad2").CodeModule
.InsertLines .CountOfLines + 1, _
"private Sub " & "bLeverantieToevoegen" & typenVerzameling(typenVuller) & "_click()" & vbNewLine & _
" Sheets(""Algemeen"").Cells(1, 1).Value = ""Woning"" " & vbNewLine & _
" Sheets(""Algemeen"").Cells(1, 2).Value = ""Type Woning leverantie"" " & vbNewLine & _
" Sheets(""Algemeen"").Cells(1, 3).Value = ""Type Woning Uren"" " & vbNewLine & _
" Sheets(""Algemeen"").Cells(1, 4).Value = " & """" & _
"Leverantie Details(Groep: " & groepNaam & ", Type: " & _
typeNaam & "(" & nummer & "))" & """" & vbNewLine & _
" Sheets(""Algemeen"").Cells(1, 5).Value = " & """" & _
"Uren Details(Groep: " & groepNaam & ", Type: " & _
typeNaam & "(" & nummer & "))" & """" & vbNewLine & _
" Load Selecteren" & vbNewLine & _
" Selecteren.Show" & vbNewLine & _
"End Sub" & vbCrLf
End With