Hello!
I currently have a button that exports a query, customized by the user on the form, to excel. The data that is exported is compared to older values (from a history table) and depending on the changes, the cellcolor is changed, so I need to format each cell individually. I have it working, but with many columns and more than 2500 records it can take like 15 minutes to export. Here's what I do: - 'fill in data in excel cells
-
intI = 2
-
Do While Not rsCurLL.EOF()
-
intCellCnt = 1
-
For Each fd In rsCurLL.Fields
-
'get the IDLine of the current record
-
If intCurIDLine <> rsCurLL.Fields("IDLine").Value Then
-
intCurIDLine = rsCurLL.Fields("IDLine").Value
-
'move the rsOldLL pointer to the matching record
-
rsOldLL.FindFirst ("[IDLine] = " & intCurIDLine & "")
-
If rsOldLL.NoMatch Then
-
blnIDLineNotFound = True
-
Else
-
blnIDLineNotFound = False
-
End If
-
End If
-
'if the "Deleted" field is not in teh view query it will not be exported
-
If fd.Name = "Deleted" Then
-
If varDeleted > 0 Then
-
xlSheet.Cells(intI, intCellCnt).Value = rsCurLL.Fields(fd.Name).Value
-
xlSheet.Cells(intI, intCellCnt).HorizontalAlignment = xlLeft
-
xlSheet.Cells(intI, intCellCnt).BorderAround xlContinuous
-
'if the current line is not present in the linelist_issued table, the line is new, so empty the old value
-
If blnIDLineNotFound = False Then
-
strFValueOld = Nz(rsOldLL.Fields(fd.Name).Value)
-
Else
-
strFValueOld = ""
-
End If
-
strFValueNew = Nz(rsCurLL.Fields(fd.Name).Value)
-
If strFValueNew <> strFValueOld Then
-
xlSheet.Cells(intI, intCellCnt).Interior.Color = RGB(192, 192, 192)
-
End If
-
If rsCurLL.Fields("Deleted").Value = True Then
-
xlSheet.Cells(intI, intCellCnt).Interior.Color = RGB(153, 204, 255)
-
End If
-
intCellCnt = intCellCnt + 1
-
End If
-
'if the "IDLine" field is not in the view query it will not be exported
-
ElseIf fd.Name = "IDLine" Then
-
If varIDLine > 0 Then
-
xlSheet.Cells(intI, intCellCnt).Value = rsCurLL.Fields(fd.Name).Value
-
xlSheet.Cells(intI, intCellCnt).HorizontalAlignment = xlLeft
-
xlSheet.Cells(intI, intCellCnt).BorderAround xlContinuous
-
If blnIDLineNotFound = False Then
-
strFValueOld = Nz(rsOldLL.Fields(fd.Name).Value)
-
Else
-
strFValueOld = ""
-
End If
-
strFValueNew = Nz(rsCurLL.Fields(fd.Name).Value)
-
If strFValueNew <> strFValueOld Then
-
xlSheet.Cells(intI, intCellCnt).Interior.Color = RGB(192, 192, 192)
-
End If
-
If rsCurLL.Fields("Deleted").Value = True Then
-
xlSheet.Cells(intI, intCellCnt).Interior.Color = RGB(153, 204, 255)
-
End If
-
intCellCnt = intCellCnt + 1
-
End If
-
ElseIf fd.Name = "revision" Then
-
'do nothing
-
Else
-
xlSheet.Cells(intI, intCellCnt).Value = rsCurLL.Fields(fd.Name).Value
-
xlSheet.Cells(intI, intCellCnt).HorizontalAlignment = xlLeft
-
xlSheet.Cells(intI, intCellCnt).BorderAround xlContinuous
-
If blnIDLineNotFound = False Then
-
strFValueOld = Nz(rsOldLL.Fields(fd.Name).Value)
-
Else
-
strFValueOld = ""
-
End If
-
strFValueNew = Nz(rsCurLL.Fields(fd.Name).Value)
-
If strFValueNew <> strFValueOld Then
-
xlSheet.Cells(intI, intCellCnt).Interior.Color = RGB(192, 192, 192)
-
End If
-
If rsCurLL.Fields("Deleted").Value = True Then
-
xlSheet.Cells(intI, intCellCnt).Interior.Color = RGB(153, 204, 255)
-
End If
-
intCellCnt = intCellCnt + 1
-
End If
-
Next
-
'display the current record
-
intCurRecord = intCurRecord + 1
-
lblCountCur.Caption = intCurRecord
-
Forms("MainMenuNew").Repaint
-
'move to next record
-
rsCurLL.MoveNext
-
intI = intI + 1
-
Loop
Is there a faster/more efficient way to do this? Any other tips are welcome since this is the first time I'm trying something like this.
Thanks!
5 8934
Hi. The fastest way to export data to Excel will not work in your case, because you have so much cell formatting to do. Looking at your code I doubt there is much more you can do to it to speed it up unless you lose the formatting, as it is the formatting itself which is slowing things down.
The fastest way to transfer data to Excel is to use a variant array to store the data, an array whose dimensions are reset to match the current number of rows and columns you need to transfer. Instead of accessing Excel directly, loop processing transfers data to the array, and then as a final step the array is assigned to Excel in one operation. This is much faster (around 10 times as fast) than using the Cells method to write data to Excel cell-by-cell.
How I resolve it in cases such as yours is to open a pre-formatted Excel worksheet - a template sheet, although not an XLT file as such - and copy the data to the pre-formatted cells using the array method. I size the template to the maximum which the data will ever need, then delete any unused rows/columns within the processing of the worksheet after transferring the data from the array.
This hybrid method works well for me, and may be something you wish to consider at a later date.
-Stewart
Code extract from one transfer routine, with use of dynamic array picked out - Dim RowData() As Variant '<< DYNAMIC ARRAY
-
Dim RowRef As Long, ColRef As Long
-
Dim CellRange As Excel.Range
-
' ...
-
FirstPass = True
-
RowCounter = 0
-
Set RS = CurrentDb.OpenRecordset(ProgQueryName)
-
Do Until RS.EOF
-
...
-
If PrevDept <> DeptRef Then
-
If Not FirstPass Then
-
...
-
End If
-
FirstPass = False
-
ReDim RowData(MaxProgs, MaxCols) << SET UP ACTUAL DIMENSIONS
-
...
-
End If
-
RowData(RowCounter, 0) = CourseName ' << WRITE DATA TO THE ARRAY
-
RowData(RowCounter, 1) = Block
-
RowData(RowCounter, 2) = FEHE
-
RowData(RowCounter, 4 + SessionOffset) = Enr
-
...
-
PrevDept = DeptRef
-
RS.MoveNext
-
Loop
-
RS.Close
-
If Not FirstPass Then
-
With xlRep.CurrentWorkSheet
-
Set CellRange = .Range(.Cells(StartRow, 2), .Cells(StartRow + RowCounter - 1, MaxCols + 1)) '<< SET UP THE CELL RANGE FOR THE ARRAY TRANSFER
-
End With
-
CellRange.Value = RowData '<< TRANSFER DATA FROM ARRAY TO EXCEL
-
xlRep.DeleteRows RowCounter + StartRow, LastRow
-
...
-
End If
Hi. The fastest way to export data to Excel will not work in your case, because you have so much cell formatting to do. Looking at your code I doubt there is much more you can do to it to speed it up unless you lose the formatting, as it is the formatting itself which is slowing things down.
The fastest way to transfer data to Excel is to use a variant array to store the data, an array whose dimensions are reset to match the current number of rows and columns you need to transfer. Instead of accessing Excel directly, loop processing transfers data to the array, and then as a final step the array is assigned to Excel in one operation. This is much faster (around 10 times as fast) than using the Cells method to write data to Excel cell-by-cell.
How I resolve it in cases such as yours is to open a pre-formatted Excel worksheet - a template sheet, although not an XLT file as such - and copy the data to the pre-formatted cells using the array method. I size the template to the maximum which the data will ever need, then delete any unused rows/columns within the processing of the worksheet after transferring the data from the array.
This hybrid method works well for me, and may be something you wish to consider at a later date.
-Stewart
Thanks a lot for your reply Stewart. Too bad I can't use your method in my case, since I would lose the formatting. It is good to know there is such a fast way to export data to excel though. I should be able to use it in the future, thanks!
NeoPa 32,556
Expert Mod 16PB
Would a Copy/Paste not do the trick pretty efficiently?
I'm planning that shortly in a current project.
Hi NeoPa. Copy/Paste does work - but it is tabular in format (so no ability to define where the data goes if you need a particular cell layout that is not just rectangular), is actually slower than other methods (the copy to the clipboard has to be complete before the data can be pasted at all), and the default paste overwrites any formatting. Although Paste Text can be used to resolve the formatting problem, it in turn can cause problems with fields formatted in certain ways (numerics formatted with leading zeros for example).
In copying recordsets to Excel the Excel copy from recordset method is far faster than copy-paste. The use of a dynamic variant array is similarly fast, particularly for data that is not strictly tabular throughout.
What really slows Excel automation down is any form of range or worksheet selection prior to copying a value, and using bespoke formatting a cell at a time. To take an instance, to form a border round a single cell means making at least four automation calls between Access and Excel with all the communication overheads that are entailed along with it. Range formatting is more efficient, because the range can be formatted in more or less the same number of communication calls as a single cell.
-Stewart
NeoPa 32,556
Expert Mod 16PB
Thanks for all that Stewart.
I always try to do my formatting by range where possible, and I have recently come across some of the problems associated with transferring data between cells in Excel. I tend to use the .Value = .Value approach where possible, but that tends to require formatting beforehand and afterwards.
I will see what I can come up with in my project - then I may get back to you for some further clarification on any outstanding matters. Obviously in another thread, as I don't want to hijack this one any further than I have already :D
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Matt |
last post by:
I have an ASP page that calls ASP routines that I created that execute
a database query and return the results to a recordset. I then iterate
through the recordset and display the data in a table....
|
by: Maria L. |
last post by:
Hi,
I need to export the content of a DataGrid (in Windows application in C#), into
an Excel spreadsheet.
Anyone knows how to do this? Any code snippets would help!
thanks a lot,
Maria
|
by: Tomek |
last post by:
Hi,
How could I export data from table or query to excel file in VB.NET?
Thanks in advance,
Tomek
|
by: Agnes |
last post by:
I can export the data to an excel(quit slow , for 5k records, It need
20mins)
Now, my problem is how can I join another table from another database ?
"select I.invno,I.company,C.telno,C.faxno from...
|
by: Jiro Hidaka |
last post by:
Hello,
I would like to know of a fast way to export data source data into an Excel
sheet.
I found a way from C# Corner(Query Tool to Excel using C# and .NET) which is
a neat little way of...
|
by: Hemant Sipahimalani |
last post by:
The following piece of code is being used to export HTML to excel.
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"...
|
by: Simon |
last post by:
Dear reader,
With the export command you can export a query to Excel.
By activate this command a form pop's up with the following text:
|
by: smaczylo |
last post by:
Hello, I've recently been asked to work with Microsoft Access, and
while I feel quite comfortable with Excel, I'm at a complete loss with
databases. If someone could help me with this issue I'm...
|
by: hemantbasva |
last post by:
Note We need to have a template on server for generating report in multiple sheet
as we do not had msoffice on server
moreover this require a batch job to delete excel file created by the...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |