Help Understanding and Fixing Out of Memory & Automation error Catastrophic failure Messages

TXRChav

New Member
Joined
Nov 5, 2015
Messages
10
I have a macro that is the most advanced I have built. However, it often crashes when saving if I have previously edited or ran it in my workbook. I typically get an "Out of Memory" error message and have recently received a "Automation error Catastrophic failure" error message.

Bellow is the entirety of my code. This is definitely already pushing the limits of my vba knowledge so can someone help identify the cause and give an explanation to understand the theory behind it to help me take that next step. I know this will take some time so thanks in advanced to anyone who takes the time.


Code:
Sub Related_BA()


Dim wb As Workbook
Dim ws As Worksheet
Dim filename As Variant
Dim returnVAlue As Variant
Dim BAwb As Workbook
Dim BAws As Worksheet
Dim BArng As Range
Dim LastRow As Integer
Dim i As Integer
Dim rng As Range


filename = Application.GetOpenFilename(filefilter:="Excel Files (*xls), *xls", Title:="Please select BA refernce file")
If filename = False Then Exit Sub


With Application
    .EnableEvents = False
    .ScreenUpdating = False
End With
    
Set wb = ActiveWorkbook
Set ws = wb.Worksheets("Super User Report")


LastRow = ws.Range("A1").CurrentRegion.Rows.Count
Set rng = ws.Range("A2:J" & LastRow)


With rng
 .HorizontalAlignment = xlCenter
End With


rng.Borders(xlDiagonalDown).LineStyle = xlNone
rng.Borders(xlDiagonalUp).LineStyle = xlNone
With rng.Borders(xlEdgeLeft)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
End With
With rng.Borders(xlEdgeTop)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
End With
With rng.Borders(xlEdgeBottom)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
End With
With rng.Borders(xlEdgeRight)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
End With
With rng.Borders(xlInsideVertical)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
End With
With rng.Borders(xlInsideHorizontal)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
End With
    
ws.Range("A:B").EntireColumn.Insert


Set BAwb = Application.Workbooks.Open(filename)
Set BAws = BAwb.Worksheets("Sheet1")
Set BArng = BAws.ListObjects("DepartmentBA").DataBodyRange




On Error Resume Next
For i = 2 To LastRow
    ws.Cells(i, 1) = Application.WorksheetFunction.VLookup(ws.Cells(i, 6), BArng, 2, 0)
Next i


On Error Resume Next
For i = 2 To LastRow
    ws.Cells(i, 2) = Application.WorksheetFunction.VLookup(ws.Cells(i, 6), BArng, 3, 0)
Next i


BAwb.Close False


ws.Columns("A:B").EntireColumn.AutoFit


ws.Range("B2").CurrentRegion.Sort key1:=ws.Range("B2"), order1:=xlAscending, _
    key2:=ws.Range("C2"), order2:=xlAscending, Header:=xlYes
    
Call SendEmail


ws.Range("A:B").EntireColumn.Delete


rng.Borders(xlDiagonalDown).LineStyle = xlNone
rng.Borders(xlDiagonalUp).LineStyle = xlNone
rng.Borders(xlEdgeLeft).LineStyle = xlNone
rng.Borders(xlEdgeTop).LineStyle = xlNone
rng.Borders(xlEdgeBottom).LineStyle = xlNone
rng.Borders(xlEdgeRight).LineStyle = xlNone
rng.Borders(xlInsideVertical).LineStyle = xlNone
rng.Borders(xlInsideHorizontal).LineStyle = xlNone
    
With Application
    .EnableEvents = True
    .ScreenUpdating = True
End With


End Sub


Sub SendEmail()


Dim cBA As Collection
Dim rng As Range
Dim rng2 As Range
Dim cell As Range
Dim wb As Workbook
Dim ws As Worksheet
Dim vNum As Variant
Dim lRow As Integer


Set wb = ActiveWorkbook
Set ws = wb.Worksheets("Super User Report")
lRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
Set rng = ws.Range("A2:A" & lRow)
Set rng2 = ws.Range("A1:A" & lRow)
Set cBA = New Collection


On Error Resume Next
    For Each cell In rng.Cells
        cBA.Add cell.Value, CStr(cell.Value)
    Next cell
On Error GoTo 0


On Error Resume Next
cBA.Remove ("None")
'Not removing #N/A to have email containging orphans display


Worksheets("Super User Report").AutoFilterMode = False


For Each vNum In cBA
    rng2.AutoFilter Field:=1, Criteria1:=vNum
    Call Email(vNum)
    'rng2.AutoFilter Field:=1
Next vNum
    
Worksheets("Super User Report").AutoFilterMode = False


End Sub


Sub Email(BA As Variant)
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim lRow As Integer
    Dim StrBody As String
    Dim rng As Range
    Dim OutApp As Object
    Dim OutMail As Object
    Dim Mnth As Variant
    Dim Yr As Variant
    
    Set wb = ActiveWorkbook
    Set ws = wb.Worksheets("Super User Report")
    lRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    Set rng = Nothing
    On Error Resume Next
    'Only the visible cells in the selection
    Set rng = ws.Range("C1:L" & lRow).SpecialCells(xlCellTypeVisible)
    On Error GoTo 0


    If rng Is Nothing Then
'        MsgBox "The selection is not a range or the sheet is protected" & _
'               vbNewLine & "please correct and try again.", vbOKOnly
        Exit Sub
    End If




    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
 
'    StrBody = wb.Sheets("Controls").Range("A6").Value & "


"
    
    StrBody = "Hello," & "

" & _
            "Multiple email lines containing sensitive information deleted in example"
             
Mnth = Format(Date, "mmmm yyyy")


    On Error Resume Next
    With OutMail
        .To = BA
        .CC = ""
        .BCC = ""
        .Subject = "Monthly Super User Training Report " & Mnth
        .HTMLBody = StrBody & RangetoHTML(rng)
        '.Send   'or use
        .Display
    End With
    On Error GoTo 0
    




    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub




Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2016
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook


    TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"


    'Copy the range and create a new workbook to past the data in
    rng.Copy
    Set TempWB = Workbooks.Add(1)
    With TempWB.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial xlPasteValues, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, False
        .Cells(1).Select
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0
    End With


    'Publish the sheet to a htm file
    With TempWB.PublishObjects.Add( _
         SourceType:=xlSourceRange, _
         filename:=TempFile, _
         Sheet:=TempWB.Sheets(1).Name, _
         Source:=TempWB.Sheets(1).UsedRange.Address, _
         HtmlType:=xlHtmlStatic)
        .Publish (True)
    End With


    'Read all data from the htm file into RangetoHTML
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    RangetoHTML = ts.readall
    ts.Close
    RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                          "align=left x:publishsource=")


    'Close TempWB
    TempWB.Close savechanges:=False


    'Delete the htm file we used in this function
    Kill TempFile


    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
What bit- Excel and OS as well as installed RAM?

Can returnVALue be more constrained on its DIMension? Variant will call for a larger memory allocation.
 
Upvote 0
also, How many New Emails are you creating?
You might need to use .Save instead of .Display
That will give you saved drafts instead x# of open New Emails displayed.
 
Upvote 0
Excel 2013 32-bit on a Windows 7 64 bit machine. 8GB installed ram.

I am only creating 8 emails.

Also the crash never happens while running the macro. It always happens when saving the spreadsheet after running the macro. It seems I am leaving something in the memory, but I do not know what or how to clear. Also the temp files created are always left in the folder where the file is stored when the crash occurs.

After reviewing my macro it seems I have deleted or never used returnVAlue as it is not present other than at the declaration. I guess I can delete that dim statement.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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