Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default EXCEL, VSTO: Fastest way to access multiple cells

Hi Alan,

Not sure what you are using for code in VSTO but whatever it is changing
cell formats individually is slow. One way to speed up is to colour the
entire range to suit the condition you expect most of the cells to meet.
Then ignore changing these individually in the loop. In VBA, and similar in
VB6 with additional ref to Excel, something like this:

Sub Test()
Dim i As Long, j As Long
Dim rng As Range
Dim wksheet As Worksheet
Dim vals
Dim XXX

Set wksheet = ActiveSheet
Set rng = wksheet.Range("A1:Z1000")

rng.Rows(3).Value = 1
rng.Rows(4).Value = -1

vals = rng.Value

XXX = 0

rng.Interior.ColorIndex = 2

For i = 1 To UBound(vals)
For j = 1 To UBound(vals, 2)
If vals(i, j) XXX Then
rng(i, j).Interior.ColorIndex = 1
ElseIf vals(i, j) < XXX Then
rng(i, j).Interior.ColorIndex = 3
End If
Next
Next

End Sub

iso of colorindex = 2 (white), maybe use xlAutomatic (-4142)

Regards,
Peter T

"Alan" wrote in message
...
Hi all,

Can anybody help clarifying my issues?

In one part of my Excel/VSTO application, I have to compare values of

cells
and assign a background color according to the result of the comparison.
The code looks something like this:

...
rng = wkSheet.Range("A1:Z1000")
vals = Array(1000 * 26 ) ' same size as 'rng'

Do ( for the whole range )
If vals(i, j) XXX Then
rng(i, j).Interior.ColorIndex = 1
Else If vals(i, j) = XXX Then
rng(i, j).Interior.ColorIndex = 2
Else ' vals(i, j) < XXX Then
rng(i, j).Interior.ColorIndex = 3
End If
Loop

It takes 10 seconds on my machine for excel to complete the loop, i.e.
excel will stop responding for 10 secs:
this is not acceptable for my app.

I tried:
- ThisApplication.ScreenUpdating = False
- ThisApplication.Calculation = Excel.XlCalculation.xlCalculationManual
they don't help much.


*** I don't want to use "Conditional formatting" for security reason.


To my understanding, for every change/call to a member/method of a Range
object, there is one out-of-process communication
to the Excel COM Server, correct? (sorry if I used wrong technical terms
about COM; not a COM fan )

And this out-of-process communication is slow; I have 1000*26 calls in my

do
loop, so it's (slow*26000) = damn slow :-)

My idea is to have a local copy (variable) of a Range object, then make

some
changes and later notify the Excel COM Server of the change just once,
something like:

...
rngNotAsACOMObj =

(ISomeInterfaceThatDoesTheTrick)wkSheet.Range("A1: Z1000")

rngNotAsACOMObj.NoConectionToCOMServer()
rngNotAsACOMObj.Interior.Color = 10 <=== Excel doesn't see the change yet.
rngNotAsACOMObj.Interior.ColorIndex = 4 <=== Still not.
rngNotAsACOMObj.NotifyChange() <=== Excel sheet gets updated.

Is this possible at all?
Any better solution exist?


Cheers -- Alan



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
EXCEL, VSTO: Fastest way to access multiple cells Alan Excel Discussion (Misc queries) 1 June 1st 05 02:45 PM
Get VSTO code to run on every launch of Excel Dave Excel Programming 1 December 8th 04 08:56 PM
Fastest way of getting data into excel jnc[_2_] Excel Programming 4 May 26th 04 02:06 PM
VSTO Excel Addin JK Excel Programming 0 April 14th 04 09:26 PM
fastest hardware for excel N Lennox Excel Programming 2 April 12th 04 07:30 PM


All times are GMT +1. The time now is 01:50 AM.

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

About Us

"It's about Microsoft Excel"