Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Optimizing Excel Reading

Hi there,
I am trying to write a simple program to do excel files data
comparison. I use Excel DOM to read through the cells and determine
whether the data on both files differs. However, the performance is not
to my expectations. For a file of 504 rows and 256 columns, I find
myself processing row 60 within 2 mins. Below is the snippet of my code
on the logic. Please do comment if I have missed something or advise in
order to improve the performance of the program.

Dim l_src_rowcount, l_tgt_rowcount As Long
Dim l_src_cellcount, l_tgt_cellcount As Long
Dim ls_val_src, ls_val_tgt As String
Dim l_row, l_cell As Long

'io_src_xcel,io_tgt_xcel are variables for Excel Workbooks
On Error Resume Next

l_src_rowcount = io_src_xcel.Worksheets(1).UsedRange.Rows.Count
l_tgt_rowcount = io_tgt_xcel.Worksheets(1).UsedRange.Rows.Count
l_src_cellcount =
io_src_xcel.Worksheets(1).UsedRange.Columns.Count
l_tgt_cellcount =
io_tgt_xcel.Worksheets(1).UsedRange.Columns.Count

If (l_src_rowcount < l_tgt_rowcount) Or (l_src_cellcount <
l_tgt_cellcount) Then
Return 0
End If

'This portion is the one taking alot of time
For l_row = 1 To l_src_rowcount
For l_cell = 1 To l_src_cellcount
ls_val_src = io_src_xcel.Worksheets(1).Cells(l_row,
l_cell).value
ls_val_tgt = io_tgt_xcel.Worksheets(1).Cells(l_row,
l_cell).Value

If ls_val_src < ls_val_tgt Then
'Mark as different
End If
Next
Next

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Optimizing Excel Reading

Assigning range values to arrays and comparing the arrays is generally
significantly faster. Obviously both arrays should be same size.

Sub test2()
Dim rng1 As Range, rng2 As Range
Dim vArr1, vArr2
Const SEP As String = " < "
sAddr = "a1:IV504"
Worksheets(1).Range(sAddr).Value = 1
Worksheets(2).Range(sAddr).Value = 1
Worksheets(2).Range("c1:c504").Value = 2

Set rng1 = Worksheets(1).Range(sAddr)
rng1.Interior.ColorIndex = xlNone

Stop ' look at first sheet
vArr1 = rng1
vArr2 = Worksheets(2).Range(sAddr)

For r = 1 To UBound(vArr1)
For c = 1 To UBound(vArr1, 2)
If vArr1(r, c) < vArr2(r, c) Then
vArr1(r, c) = vArr1(r, c) & SEP & vArr2(r, c)
rng1(r, c).Interior.ColorIndex = 6
End If
Next
Next
Worksheets(1).Range(sAddr).Value = vArr1
End Sub

Regards,
Peter T

"Kwan Thean Keong" wrote in message
oups.com...
Hi there,
I am trying to write a simple program to do excel files data
comparison. I use Excel DOM to read through the cells and determine
whether the data on both files differs. However, the performance is not
to my expectations. For a file of 504 rows and 256 columns, I find
myself processing row 60 within 2 mins. Below is the snippet of my code
on the logic. Please do comment if I have missed something or advise in
order to improve the performance of the program.

Dim l_src_rowcount, l_tgt_rowcount As Long
Dim l_src_cellcount, l_tgt_cellcount As Long
Dim ls_val_src, ls_val_tgt As String
Dim l_row, l_cell As Long

'io_src_xcel,io_tgt_xcel are variables for Excel Workbooks
On Error Resume Next

l_src_rowcount = io_src_xcel.Worksheets(1).UsedRange.Rows.Count
l_tgt_rowcount = io_tgt_xcel.Worksheets(1).UsedRange.Rows.Count
l_src_cellcount =
io_src_xcel.Worksheets(1).UsedRange.Columns.Count
l_tgt_cellcount =
io_tgt_xcel.Worksheets(1).UsedRange.Columns.Count

If (l_src_rowcount < l_tgt_rowcount) Or (l_src_cellcount <
l_tgt_cellcount) Then
Return 0
End If

'This portion is the one taking alot of time
For l_row = 1 To l_src_rowcount
For l_cell = 1 To l_src_cellcount
ls_val_src = io_src_xcel.Worksheets(1).Cells(l_row,
l_cell).value
ls_val_tgt = io_tgt_xcel.Worksheets(1).Cells(l_row,
l_cell).Value

If ls_val_src < ls_val_tgt Then
'Mark as different
End If
Next
Next



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
optimizing excel work sheet Jay Excel Programming 5 October 2nd 06 03:21 PM
MapPoint Optimizing mak Excel Discussion (Misc queries) 0 August 17th 05 09:30 PM
Optimizing Code Jim Thomlinson[_3_] Excel Programming 5 March 2nd 05 10:07 PM
optimizing a macro The Grinch[_11_] Excel Programming 2 August 4th 04 09:55 AM
Optimizing in VB Phil Excel Programming 1 August 8th 03 05:18 PM


All times are GMT +1. The time now is 11:48 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"