Excel Crashes when adding code to workbooks via VBA

realniceguy5000

Board Regular
Joined
Aug 19, 2008
Messages
148
Hi Everyone,

I have cross posted to get more ideas of why this is happening...

Code:
[URL]http://www.excelforum.com/excel-programming/679287-excel-crashes-when-adding-code-to-workbook-via-vba.html[/URL]

I thought I had this fixed late yesterday, But the same problem happened again this morning. This all generated from this post
Code:
[URL]http://www.excelforum.com/excel-programming/678982-insert-code-into-this-workbook-mod.html#post2073179[/URL]
The code below resides in my personal.XLS Module. When I step through the code everything works till it reaches the point in red then I get the pop up that says excel needs to close and asks to send or not to send.
I noticed that if I choose recover my work and restart excel and I look at the code in THISWORKBOOK of the recovered workbook that the code I wanted was placed in there.
Can anyone explain why this is happening? I have the macro's security set to low and a check mark in the trust access to vb projects.
Any Idea's how to fix this?
Thank You
Mike
Code:
Sub OpenALL() 'open workbooks in a folder
Dim lCount As Long
Dim wbResults As Workbook
Dim wbCodeBook As Workbook
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
On Error Resume Next
Set wbCodeBook = ThisWorkbook
With Application.FileSearch
        .NewSearch
 
        .LookIn = "G:\Hazleton Production\2009 Production\Production Links\2009 Varsity"
        .FileType = msoFileTypeExcelWorkbooks
 
    If .Execute > 0 Then 
          For lCount = 1 To .FoundFiles.Count 'Loop through all.
 
          Set wbResults = Workbooks.Open(Filename:=.FoundFiles(lCount), UpdateLinks:=0)
 
          ActiveSheet.unProtect "password"
 
          AddCodeToThisWorkbook ActiveWorkbook, "C:\Documents and Settings\MAlston\Desktop\Count Sheets\mycode.txt"
 
 
          wbResults.Close SaveChanges:=True
          Next lCount
     End If
End With
    On Error GoTo 0
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Application.EnableEvents = True
End Sub
Code:
Sub AddCodeToThisWorkbook(wkb As Workbook, sFile As String, _
                          Optional bReplace As Boolean = False)
    With wkb.VBProject.VBComponents(wkb.CodeName).CodeModule
        If bReplace Then .DeleteLines StartLine:=1, Count:=.CountOfLines
        [COLOR=red].AddFromFile sFile[/COLOR]  
    End With
End Sub
 

Some videos you may like

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.

Watch MrExcel Video

Forum statistics

Threads
1,123,078
Messages
5,599,632
Members
414,326
Latest member
kfg1287

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
Top