I have a very large Excel file with 14k+ lines of code and 320+ sheets in it.
The file's VBA imports data from other Excel spreadsheets, does some calculations and "exports" the results.
The "export" involves opening a new workbook and copying across values, formats, comments and the colours of those sheets, then saving that new file.
As part of the code, I have written something that checks the data has been pasted in. Just a very simple:
If Range("A2").Value <> True Then
'error has occured with export
MsgBox "error", vbCritical, "AURORA Model: Error #MU-ID: Unable to Export Datafile"
Exit Sub
End If
As I say, this error is intermittent and I can't seem to work out what causes it. I assume it's a memory thing. Any advice?
Below is the interesting part of the code:
Sub ExportData(filename As String, FileLocation As String, export_reason As String)
'
' this macro copys over the values and comments from the model
' this is run by clicking the "butExport" button on the userform
'
Dim sheets_all As Integer
Dim sheets_cycle As Integer
Dim sheet_name As String
Dim sheet_colour As Long
Dim wBook1 As String
Dim wBook2 As String
'wBook1 is AuRORA
wBook1 = ThisWorkbook.Name
Application.SheetsInNewWorkbook = 1
Workbooks.Add
'wBook2 is what will be the new datafile
wBook2 = ActiveWorkbook.Name
'activate the Aurora Model
Windows(wBook1).Activate
'count the number of sheet in the model
sheets_all = Sheets.Count
Sheets("l_ERROR_IPT").ListObjects("tbl_ERROR_IPT").Range.AutoFilter Field:=3
Sheets("l_MRUN").ListObjects("tbl_MRUN").Range.AutoFilter Field:=2, _
Criteria1:=""
On Error Resume Next
For sheets_cycle = 1 To sheets_all
sheet_name = Sheets(sheets_cycle).Name
Application.StatusBar = _
"Running Macro. Exporting data " & sheets_cycle _
& " (" & sheet_name & ") of " & sheets_all & " (" & Round(sheets_cycle / sheets_all * 100, 1) & "%)"
'if the cycling sheet name begins with s, i, n, l, u, c, o, p or d then...
If Mid(sheet_name, 1, 2) = "i_" Or Mid(sheet_name, 1, 2) = "u_" _
Or Mid(sheet_name, 1, 2) = "c_" Or Mid(sheet_name, 1, 2) = "o_" _
Or Mid(sheet_name, 1, 2) = "p_" Or Mid(sheet_name, 1, 2) = "d_" Or _
Mid(sheet_name, 1, 2) = "l_" Or Mid(sheet_name, 1, 2) = "s_" Or _
Mid(sheet_name, 1, 2) = "n_" Then
Sheets(sheets_cycle).Select
'if it's not a "p_" sheet then show all data
If Mid(sheet_name, 1, 2) <> "p_" Then ActiveSheet.ShowAllData
'copy all cells
Range("A1:AZ2000").Copy
sheet_colour = Sheets(sheets_cycle).Tab.Color
'activate datafile
Windows(wBook2).Activate
'paste values and comments
With Range("A1")
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteComments
.PasteSpecial xlPasteFormats
End With
'rename sheet with newly pasted data in datafile
ActiveSheet.Name = sheet_name
ActiveSheet.Tab.Color = sheet_colour
'check that cell "A2" = "TRUE" otherwise there has been an error with the export
If Range("A2").Value <> True Then
'error has occured with export
MsgBox "error", vbCritical, "AURORA Model: Error #MU-ID: Unable to Export Datafile"
Windows(wBook1).Activate
Exit Sub
End If
'add another sheet
Sheets.Add After:=Sheets(Sheets.Count)
Windows(wBook1).Activate
End If
Next sheets_cycle
'Delete the last sheet and select the front sheet (d_file_info_sheet)
Windows(wBook2).Activate
Sheets(Sheets.Count).Delete
Sheets(1).Select
'save datafile with a random password
ActiveWorkbook.SaveAs filename:=FileLocation & filename, FileFormat:=50, WriteResPassword:= _
funcRandNumbersLarge, ReadOnlyRecommended:=True
ActiveWorkbook.Close
Sheets(1).Select
MsgBox "Datafile exported and saved as """ & ReconciliationModel.labExportFile.Caption _
& """. Please check that this has been saved correctly before closing the exported datafile (which has been left open).", vbOKOnly, "AURORA Model: Datafile exported"
I'm starting to tear my hair out over this issue so any advice or suggestions would be very welcome!
Thanks
Tom