romeerome368
Programmer
Hello,
I have created code to help me write large amounts of data to an Excel 2010 file. The code works great, but it's working slower than what I would like. It ran for 2 hours and only posted 40,000 records which is way to slow for my fast paced environment.
I have a recordset that has 618,000 rows of data, but it's slow going with writing the data to the spreadsheet.
Below is what my code looks like:
Is there a way to speed this code up? The commented letters represent the column letters in my spreadsheet template.
Anyone's help is greatly appreciated.
I have created code to help me write large amounts of data to an Excel 2010 file. The code works great, but it's working slower than what I would like. It ran for 2 hours and only posted 40,000 records which is way to slow for my fast paced environment.
I have a recordset that has 618,000 rows of data, but it's slow going with writing the data to the spreadsheet.
Below is what my code looks like:
Code:
Dim appExcel As Object
Dim i As Integer
Dim lngCount, lngCurrentRow, lngRows, n As Long
Dim rng, rngData, rngStart As Excel.Range
Dim strDataRange, strSheetName, strStartAddress As String
Dim strTemplate, strTemplateFile, strTitle, strSaveName As String
Dim strFilePath, strPrompt As String
Dim wkb As Excel.Workbook
Dim wks As Excel.Worksheet
strFilePath = "C:\Documents and Settings\. . .\"
strTemplate = "Rolling13PdOutput.xltx"
strTemplateFile = strFilePath & strTemplate
Set appExcel = CreateObject("Excel.Application")
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("qryRolling13PdOutput")
Set wkb = appExcel.Workbooks.Add(strTemplateFile)
Set wks = wkb.Sheets(1)
wks.Activate
appExcel.Visible = True
Set rngStart = wks.Range("A2")
Set rng = wks.Range("A2")
rst.MoveLast
rst.MoveFirst
lngCount = rst.RecordCount
For n = 1 To lngCount
rng.Value = Nz(rst![PC]) 'A
Set rng = rng.Offset(Columnoffset:=1)
rng.Value = Nz(rst![Regions]) 'B
Set rng = rng.Offset(Columnoffset:=1)
rng.Value = Nz(rst![Areas]) 'C
Set rng = rng.Offset(Columnoffset:=1)
rng.Value = Nz(rst![Branch]) 'D
Set rng = rng.Offset(Columnoffset:=1)
rng.Value = Nz(rst![Segment]) 'E
Set rng = rng.Offset(Columnoffset:=1)
rng.Value = Nz(rst![Class]) 'F
Set rng = rng.Offset(Columnoffset:=1)
rng.Value = Nz(rst![Total Dollars]) 'G
Set rng = rng.Offset(Columnoffset:=1)
rng.Value = Nz(rst![YEAR]) 'H
Set rng = rng.Offset(Columnoffset:=1)
rng.Value = Nz(rst![PERIOD]) 'I
Set rng = rng.Offset(Columnoffset:=1)
rng.Value = Nz(rst![Total Volume]) 'J
Set rng = rng.Offset(Columnoffset:=1)
rng.Value = Nz(rst![# of Services]) 'K
Set rng = rng.Offset(Columnoffset:=1)
rng.Value = Nz(rst![Chain Code]) 'L
Set rng = rng.Offset(Columnoffset:=1)
rng.Value = Nz(rst![Material]) 'M
Set rng = rng.Offset(Columnoffset:=1)
rng.Value = Nz(rst![Customer ID]) 'N
Set rng = rng.Offset(Columnoffset:=1)
rng.Value = Nz(rst![Customer Name]) 'O
Set rng = rng.Offset(Columnoffset:=1)
rng.Value = Nz(rst![Street1]) 'P
Set rng = rng.Offset(Columnoffset:=1)
rng.Value = Nz(rst![City]) 'Q
Set rng = rng.Offset(Columnoffset:=1)
rng.Value = Nz(rst![State]) 'R
Set rng = rng.Offset(Columnoffset:=1)
rng.Value = Nz(rst![Zip Code]) 'S
Set rng = rng.Offset(Columnoffset:=1)
rng.Value = Nz(rst![Phone]) 'T
rst.MoveNext
Set rng = rngStart.Offset(RowOffset:=n)
Next n
lngRows = wks.UsedRange.Rows.Count
strRange = "A2:AD" & CStr(lngRows)
Set rngData = wks.Range(strRange)
strSheetName = "All customer info - " & Format(Date, "yymmdd")
strSaveName = strFilePath & strSheetName & ".xlsx"
wkb.SaveAs FileName:=strSaveName, FileFormat:=xlWorkbookDefault
wkb.Close
rst.Close
strTitle = "Workbook Created"
strPrompt = strSheetName & vbCrLf & "was created in " _
& strFilePath
MsgBox strPrompt, vbOKOnly + vbInformation, strTitle
End Sub
Is there a way to speed this code up? The commented letters represent the column letters in my spreadsheet template.
Anyone's help is greatly appreciated.