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 8994
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,579
Recognized Expert Moderator MVP
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,579
Recognized Expert Moderator MVP
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. Before I iterate
through the recordset I instruct the browser that the content type is
Excel using the following line:
(Response.ContentType = "application/vnd.ms-excel")
This works fine with Excel 2003 but with older versions (I tested Excel...
|
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 invoice I,company C where
I.company = C.company"
Thanks a lot
Dim Excel As New Excel.Application
Dim intColumn, intRow, intColumnValue As Integer
Dim strExcelFile As String
Dim strFileName As String
|
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 exporting dataset data to an excel using the Excel COM
object.
This works fine but the problem is its pretty darn slow when exporting large
| |
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"
HttpContext.Current.Response.AddHeader("content-disposition",
"attachment;filename=ABC.xls")
HttpContext.Current.Response.Write(strHTML)
HttpContext.Current.Response.End()
However when the user tries to save it the Default File Type is Web
Page(*.htm; *.html)
|
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 having I'd be
most appreciative. The database is already constructed, I'm just
wanting to export the data to an excel file.
In short, I'm hoping to export two Tables (or queries...not sure which
to use - they both seem to have the same data) in...
|
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 method....
it creates 6 sheets
# region Namespaces
using System;
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |