codeModule.InsertLine is only working if the VBE is open?

ianohlander

New Member
Joined
Dec 5, 2002
Messages
26
Hello all. Back again this a seemingly undecipherable problem.

I have the following code:

Code:
              . . . 
    Set nwSheet = Sheets.Add
    nwSheet.name = name
              . . . 
    Set UFvbc = ActiveWorkbook.VBProject.VBComponents.Item(nwSheet.CodeName)
  
    Dim count As Integer
    count = UFvbc.CodeModule.CountOfLines + 1

            code = "" & "Private Sub Worksheet_Change(ByVal Target as Range)" & vbCr
            code = code & "myRow = CStr(Target.Row)" & vbCr
            code = code & "If Target.Column = 3 And Target.Row > 5 And Target.Row < 59 Then " & vbCr
            code = code & "If Target.Value <> """" Then " & vbCr
            code = code & "Range(""E"" & myRow).Value= payrollModule.getFed(Range(""L1"").Value, Range(""N1"").Value, Range(""C"" & myRow).Value) " & vbCr
            code = code & "Range(""G"" & myRow).Formula = ""=$g$5*e"" & myRow" & vbCr
            code = code & "Range(""I"" & myRow).Formula = ""=$i$5*e"" & myRow" & vbCr
            code = code & "Range(""K"" & myRow).Formula = ""=$k$5*e"" & myRow" & vbCr
            code = code & "Range(""M"" & myRow).Formula = ""=c"" & myRow & "" - e"" & myRow &"" - g"" & myRow & ""- i"" & myRow & ""- k"" & myRow" & vbCr
            code = code & "Else" & vbCr
            code = code & "Range(""E"" & myRow).ClearContents" & vbCr
            code = code & "Range(""G"" & myRow).ClearContents" & vbCr
            code = code & "Range(""I"" & myRow).ClearContents" & vbCr
            code = code & "Range(""K"" & myRow).ClearContents" & vbCr
            code = code & "Range(""M"" & myRow).ClearContents" & vbCr
            code = code & "End if" & vbCr
            code = code & "End if" & vbCr
            code = code & "end sub"
        UFvbc.CodeModule.insertLines count, code

The thing is, this part of the sub works perfectly if the VB Editor is open. When it is executed, the sheet it creates gets that code inside it's module. But if the VB Editor is closed (either on start up or if I close it manually) then this code doesn't work. It gives me a "Object not set or with block not set" error (or something like that). So the "code" string is not insterted into the worksheets module.

I have made sure that this project references MS VB Extensibility 5.3 (to access the VBProject and VBComponent objects.)

What's the deal? Why would a sub work only if the VB Editor where open? What is the VB Editor doing that makes this ok? And how can I make this run without requiring the user open the VBE (which seems incredibly stupid to require)?

This is really aggrivating and I am finding nothing by google-searching it.

IT WORKS, if the VB Editor is open. Just open, mind you. Not run from the Editor. The sub is called from a command button on a worksheet. IF the VB editor is open (but minimized), the sub works. IF the editor is not open, it doesn't work. That simple.

Grrrr!!

Thanks,
Ian
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hi Ian
I got this to work for me.

Basically some timing Issues that get resolved with DoEvents
and On Error resume next

Code:
Sub Test()
Dim nwSheet As Worksheet
Dim UFvbc As VBComponent
Dim name As String
Dim Code As String

name = "Test"
Set nwSheet = ActiveWorkbook.Sheets.Add
    nwSheet.name = name
    DoEvents
    On Error Resume Next
    Set UFvbc = ActiveWorkbook.VBProject.VBComponents(nwSheet.CodeName)
    
    Dim count As Integer
    count = UFvbc.CodeModule.CountOfLines + 1

            Code = "" & "Private Sub Worksheet_Change(ByVal Target as Range)" & vbCr
            Code = Code & "myRow = CStr(Target.Row)" & vbCr
            Code = Code & "If Target.Column = 3 And Target.Row > 5 And Target.Row < 59 Then " & vbCr
            Code = Code & "If Target.Value <> """" Then " & vbCr
            Code = Code & "Range(""E"" & myRow).Value= payrollModule.getFed(Range(""L1"").Value, Range(""N1"").Value, Range(""C"" & myRow).Value) " & vbCr
            Code = Code & "Range(""G"" & myRow).Formula = ""=$g$5*e"" & myRow" & vbCr
            Code = Code & "Range(""I"" & myRow).Formula = ""=$i$5*e"" & myRow" & vbCr
            Code = Code & "Range(""K"" & myRow).Formula = ""=$k$5*e"" & myRow" & vbCr
            Code = Code & "Range(""M"" & myRow).Formula = ""=c"" & myRow & "" - e"" & myRow &"" - g"" & myRow & ""- i"" & myRow & ""- k"" & myRow" & vbCr
            Code = Code & "Else" & vbCr
            Code = Code & "Range(""E"" & myRow).ClearContents" & vbCr
            Code = Code & "Range(""G"" & myRow).ClearContents" & vbCr
            Code = Code & "Range(""I"" & myRow).ClearContents" & vbCr
            Code = Code & "Range(""K"" & myRow).ClearContents" & vbCr
            Code = Code & "Range(""M"" & myRow).ClearContents" & vbCr
            Code = Code & "End if" & vbCr
            Code = Code & "End if" & vbCr
            Code = Code & "end sub"
        UFvbc.CodeModule.insertLines count, Code

End Sub
 
Upvote 0
Thank you guys. I will try that DoEvents call. From I saw, the only difference between my original and what works is the DoEvents call just before UFvbc is set to the right component.

Very interesting, that. I don't think I would have caught that.

Thank you very much for your help. PS: I checked that link and it had good information.

Ian
 
Upvote 0

Forum statistics

Threads
1,217,348
Messages
6,136,047
Members
449,984
Latest member
Ffprojectjkt

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top