Hello all,
I wrote a macro in a "Summary" workbook, which loops through design documents (where the name of the design document is written in a column of the summary workbook, and each document is denoted by
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 1).Value
in my code) and fetch some data from each workbook and inserts the value in a cell. Each of the design documents have the same format.
My problem is that when the macro tries to open the 6th document, excel crashes. I've tried doing this on a seperate machine and the same happens (i think on the other machine it crashed after the 8th document). I then tried adding a button which replaces the loop. Instead of looping, each time the button is pressed it adds the information of the next document, but to no avail it still crashed.
I tried looking into methods of clearing memory since I suspect that this is the problem.
My code to follow. The code for the button is the same, less the for loop. I created a class which fetches the relevant data from each document, please let me know if you would like this included.
Option Explicit
Public workbookname As String
Sub update_Click()
workbookname = ThisWorkbook.name
Dim sh1 As Worksheet, sh2 As Worksheet
Dim myworkbook As Range
Dim i As Integer
Set sh2 = Workbooks("AI HVL AHU Summary v1").Sheets("Sheet1")
Application.ScreenUpdating = False
Dim curRoom As sumRoom
Set curRoom = New sumRoom
For i = 5 To 7 'sh2.UsedRange.Rows.Count
If Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 1).Value <> Empty Then
Set myworkbook = Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 1)
Workbooks.Open ThisWorkbook.Path & "\" & Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 1).Value
'Set sh1 = Workbooks(myworkbook.Value).Sheets("Detail RDS")
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 3).Value = curRoom.Area(i, myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 4).Value = curRoom.Volume(i, myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 5).Value = curRoom.Temp(myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 6).Value = curRoom.RH(myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 7).Value = curRoom.SA(myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 8).Value = curRoom.RA(myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 9).Value = curRoom.FA(myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 10).Value = curRoom.EA(myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 11).Value = curRoom.Cooling(myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 12).Value = curRoom.TCL(myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 13).Value = curRoom.BP(myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 14).Value = curRoom.PreHeating(myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 15).Value = curRoom.CW(myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 16).Value = curRoom.HW(myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 17).Value = curRoom.AHUType(myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 18).Value = curRoom.AHUFilters(myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 19).Value = curRoom.AHUPA(myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 20).Value = curRoom.AHUkW(myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 21).Value = curRoom.AHUlenght(myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 22).Value = curRoom.AHUwidth(myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 23).Value = curRoom.AHUheight(myworkbook.Value)
Workbooks(myworkbook.Value).Close SaveChanges:=False
'Workbooks(workbookname).Activate
End If
Next i
End Sub
Display More
Below is the code for the button which includes an attempt to clear the memory:
Sub insertData(i As Integer)
Application.ScreenUpdating = False
workbookname = ThisWorkbook.name
Dim sh1 As Worksheet, sh2 As Worksheet
Dim myworkbook As Range
Set sh2 = Workbooks("AI HVL AHU Summary v2").Sheets("Sheet1")
Dim curRoom As sumRoom
Set curRoom = New sumRoom
If Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 1).Value <> Empty Then
Set myworkbook = Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 1)
' On Error GoTo ending
Workbooks.Open ThisWorkbook.Path & "\" & Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 1).Value
'Set sh1 = Workbooks(myworkbook.Value).Sheets("Detail RDS")
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 3).Value = curRoom.Area(i, myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 4).Value = curRoom.Volume(i, myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 5).Value = curRoom.Temp(myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 6).Value = curRoom.RH(myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 7).Value = curRoom.SA(myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 8).Value = curRoom.RA(myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 9).Value = curRoom.FA(myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 10).Value = curRoom.EA(myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 11).Value = curRoom.Cooling(myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 12).Value = curRoom.TCL(myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 13).Value = curRoom.BP(myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 14).Value = curRoom.PreHeating(myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 15).Value = curRoom.CW(myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 16).Value = curRoom.HW(myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 17).Value = curRoom.AHUType(myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 18).Value = curRoom.AHUFilters(myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 19).Value = curRoom.AHUPA(myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 20).Value = curRoom.AHUkW(myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 21).Value = curRoom.AHUlenght(myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 22).Value = curRoom.AHUwidth(myworkbook.Value)
Workbooks(workbookname).Worksheets("Sheet1").Cells(i, 23).Value = curRoom.AHUheight(myworkbook.Value)
Workbooks(myworkbook.Value).Close SaveChanges:=False
'Workbooks(workbookname).Activate
End If
Application.ScreenUpdating = True
Set sh1 = Nothing
Set sh2 = Nothing
Set myworkbook = Nothing
' Exit Sub
'ending:
' MsgBox ("Error opening; Make sure the source file is in the same folder as the summary sheet")
End Sub
Display More