Problem is that it is INCREDIBLY slow...like turtle slow. Like a turtle in molasses slow. Did I mention it was slow?
For example, it took 8 minutes to write 3,340 records, 42 minutes to write 21,432 records, and 1 hour 46 minutes to write 40,864 records.
I've already made Visible = false and ScreenUpdating = false.
Surely there must be a faster method.
Any suggestions?
Code I use is below:
Code: Select all
Sub OutputVPReport(strVPIn As String, blnSendEmail As Boolean)
'Open the template
Set objxl = CreateObject("excel.application")
objxl.Workbooks.Open FileName:="MyFile Template-BLANK.xls"
objxl.Visible = False 'True
objxl.ScreenUpdating = False
objxl.Sheets("VP Detail Data Review").Select
objxl.Range("A3").Select
'Populate it
Dim db As DAO.Database
Set db = CurrentDb
Dim rst As DAO.Recordset
Set rst = db.OpenRecordset("SELECT * FROM [qryFullData2-v2] WHERE [VP] = '" & strVPIn & "'", dbOpenForwardOnly)
While Not rst.EOF
objxl.ActiveCell.Value = rst!CID
objxl.ActiveCell.offset(0, 1).Value = rst!ELID
objxl.ActiveCell.offset(0, 2).Value = rst![Last Name]
objxl.ActiveCell.offset(0, 3).Value = rst![First Name]
objxl.ActiveCell.offset(0, 4).Value = rst!Title
objxl.ActiveCell.offset(0, 5).Value = rst!Company
objxl.ActiveCell.offset(0, 6).Value = rst![WorkerClassification]
objxl.ActiveCell.offset(0, 7).Value = rst![DecisionTree]
objxl.ActiveCell.offset(0, 8).Value = rst![Comments]
objxl.ActiveCell.offset(0, 9).Value = rst![Business Unit]
objxl.ActiveCell.offset(0, 10).Value = rst![Cost Center]
objxl.ActiveCell.offset(0, 11).Value = rst![Manager CID]
objxl.ActiveCell.offset(0, 12).Value = rst![Manager Last Name]
objxl.ActiveCell.offset(0, 13).Value = rst![Manager First Name]
objxl.ActiveCell.offset(0, 14).Value = rst![Sponsor First Name] & " " & rst![Sponsor Last Name]
objxl.ActiveCell.offset(0, 15).Value = rst![Org Name]
objxl.ActiveCell.offset(0, 16).Value = GetName(rst![VP])
If IsNull(rst![Director]) Or rst![Director] = "" Then
'Do nothing
Else
objxl.ActiveCell.offset(0, 17).Value = GetName(rst![Director]) 'StrConv(Replace(Left(rst![Director], InStr(rst![Director], "@") - 1), ".", " "), vbProperCase)
End If
objxl.ActiveCell.offset(0, 18).Value = rst![Start Date]
objxl.ActiveCell.offset(0, 19).Value = rst![End Date]
objxl.ActiveCell.offset(0, 20).Value = Int(rst![Days])
objxl.ActiveCell.offset(0, 21).Value = Int(rst![Years])
objxl.ActiveCell.offset(0, 22).Value = rst![Months]
objxl.ActiveCell.offset(0, 23).Value = rst![Aging2]
objxl.ActiveCell.offset(0, 24).Value = GetJobTitle(rst![VP])
objxl.ActiveCell.offset(0, 25).Value = rst![VP]
objxl.ActiveCell.offset(0, 26).Value = rst![Director]
'objxl.ActiveCell.offset(0, 27).Value = rst![HRBP]
objxl.ActiveCell.offset(1, 0).Select
rst.MoveNext
Wend
rst.Close
'Save it
'Save the Spreadsheet
objxl.ActiveWorkbook.SaveAs FileName:="MyFile" & Format(Now, "YYYYMMDD") & "\" & Format(Now, "YYYYMMDD") & "-" & strVPIn & " 3501 Report.xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
objxl.ScreenUpdating = True
objxl.Visible = True
objxl.ActiveWorkbook.Close SaveChanges:=False
'Close the Spreadsheet
objxl.Workbooks.Close
objxl.Quit
Set objxl = Nothing
Set rst = Nothing
Set db = Nothing
'Send it
If blnSendEmail = True Then
'Email the report
End If
End Sub