Results 1 to 8 of 8

Thread: excel automation speed ?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904

    excel automation speed ?

    I'm using VB to stuff data into an existing excel spreadsheet, and I can hardly believe how SLOW it is. On a 750Mhz PIII, I get 180 cells per second. Does anyone have any idea why this is so slow?

    My code is below if you're interested.

    VB Code:
    1. '
    2. ' excel data stuff --- shows that on this machine (750Mhz PIII) you can
    3. ' stuff about 180 cells per second, which is STUNNINGLY slow based on
    4. ' the processor speed; SO ... it must be that there is a connection that
    5. ' gets established for every cell stuff & if that connection could be
    6. ' kept open, it might take WAY less time to stuff all the data
    7. '
    8. ' the doevents does not make any difference in the amount of time the
    9. ' whole thing takes, but without the doevents, the timer counts never
    10. ' happen
    11. '
    12. Option Explicit
    13. Public xlApp As Excel.Application
    14. Public xlBook As Excel.Workbook
    15. Public xlSheet As Excel.Worksheet
    16. Public xlRows As Integer
    17. Public seconds As Integer
    18. '
    19. '
    20. '
    21. Private Sub cmdDoIt_Click()
    22.     Dim ix As Integer, iy As Integer
    23.     Dim row As Integer, col As Integer
    24.    
    25.  
    26.     Set xlApp = New Excel.Application
    27.     Set xlBook = xlApp.Workbooks.Open(App.Path & "\Book1.xls", , True)
    28.     Set xlSheet = xlBook.Worksheets("Data")
    29.    
    30.     seconds = 0
    31.     For ix = 0 To 600
    32.         row = 2 + ix
    33.         For iy = 0 To 36
    34.             col = 2 + iy
    35. '            xlSheet.Cells(row, col).Value = CStr(iy + ix * 10) ' as strings
    36.             xlSheet.Cells(row, col).Value = iy + ix * 10    ' as integers
    37.         Next iy
    38.     Next ix
    39.  
    40.     xlApp.Workbooks.Close
    41.     xlApp.Quit
    42.     Set xlApp = Nothing
    43. End Sub

  2. #2
    Member
    Join Date
    Aug 2002
    Location
    Indianapolis In, lookin for a Good Woman !~
    Posts
    48

    faster inserts

    hi
    i really dont see anything wrong with teh code...
    i wrote one for work, but did it a slight bit differnet.

    from looking at the code, i am assuming that when you are inserting data, you an see it doing the inserts... (the sheet is visible)

    when i did mine, i had it so that everytihng happened in teh background and after everything was completed i made eh excel application visible. i thnik it will speed up your process a bit.

    if you need me to i can log into work and look that code up.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904
    I've done it foreground and background and didn't see any difference in speed. any help you can give would be appreciated.

    have you done any speed check on yours? I mean, if you get a result 10% or 30% different than mine, then who cares, but if you get FACTORS of difference, then it would point to something in my technique being more time-consuming that it needs to be, which I certainly hope it is since the current speed is horrible.

    thanks for your time.

  4. #4
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253
    Any solution to this one - I am currently trying to read 89000 cells from a spreadsheet, and it runs really slow (3 mins or more)

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    One major comment based on the code shown above - it is MUCH faster (about 10*) to read/write arrays of cells rather than a single cell at a time.

    I think the property is FormulaArray (on a range), eg:

    tmp_Array = Array (0, 1, 3.3, 2, 4.2) (or whatever code you want to fill the array!)

    xlSheet.Range ("A1:E1").FormulaArray = tmp_Array

    and:

    tmp_Array = xlSheet.Range ("A1:E1").FormulaArray


    (nb: I think you can omit FormulaArray, but the results are slightly different)

  6. #6
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    The problem is marchalling. Excel and your app run in seperate memory spaces, so every call to excel needs to be marchalled across processes.
    Try to reduce the number of times this is needed. Pass as much data in one call as you can.
    When you need to read from excel, you could read an entire sheet into an array (variant) in one call.
    VB Code:
    1. Dim varValues as Variant
    2. Dim xlApp As Object
    3. Dim xlBook As Object
    4.  
    5.     Set xlApp = CreateObject("Excel.Application")
    6.     Set xlBook = xlApp.Workbooks.Open(strPath, False, True)
    7.     varValues = xlBook.Sheets(1).UsedRange.Value
    8.     xlBook.Close False
    9.     Set xlBook = Nothing
    10.     xlApp.Quit
    11.     Set xlApp = Nothing
    varValues will contain a two dimensional array, containing the complete sheet.

    If you need to write data to excel, you can use a similar technique.
    You can either use excel's CopyFromRecordset method, to copy a recordset to a range, or you can set a range to a two dimensional array.
    Frans

  7. #7
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253
    How can you read multiple properties from a cell such as it's value and it's colour without resorting to marshalling.

    Forumla Array will return it's values -but how can I get an array of colours?

  8. #8
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    I don't think you can do this for properties other then value.
    Frans

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width