Results 1 to 3 of 3

Thread: XL speed problem

  1. #1

    Thread Starter
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    XL speed problem

    Are there any techniques, tricks or tips to accessing cell data from Excel t VB6.

    The basic way seems horrendously slow. I am using the .cells(x,y).value method to retrieve data.

  2. #2

    Thread Starter
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253
    Here is the function that (badly) needs optimisation

    VB Code:
    1. Private Function EnumerateCells(ByRef oSheet As Excel.Worksheet, RestrictColumnsTo As Long, ByVal TotalCellCount As Long, ByRef TotalComplete As Long, ByRef UserCancelled As Boolean) As EXECL_CELL()
    2.  
    3.     On Error GoTo ERR_EnumerateCells
    4.    
    5.     Dim lRow As Long
    6.     Dim lCol As Long
    7.     Dim aCells() As EXECL_CELL
    8.     Dim oRange As Excel.Range
    9.     Dim fCancel As Boolean
    10.     Dim lColumns As Long
    11.     Dim lRows As Long
    12.    
    13.     fCancel = False
    14.    
    15.     With oSheet
    16.    
    17.         Set oRange = .UsedRange
    18.        
    19.         lColumns = oRange.Columns.Count
    20.         lRows = oRange.Rows.Count
    21.        
    22.         If lColumns > RestrictColumnsTo Then
    23.             lColumns = RestrictColumnsTo
    24.         End If
    25.            
    26.         ReDim aCells(lColumns, oRange.Rows.Count)
    27.        
    28.         For lCol = 1 To lColumns
    29.        
    30.             For lRow = 1 To lRows
    31.            
    32.                 With .Cells(lRow, lCol)
    33.                     aCells(lCol, lRow).Colour = .Interior.Color
    34.                     aCells(lCol, lRow).Value = .Value
    35.                 End With
    36.                
    37.                 TotalComplete = TotalComplete + 1
    38.                 RaiseEvent ExcelScan((TotalComplete / TotalCellCount) * 1000, fCancel)
    39.                
    40.                 If fCancel Then
    41.                     Erase aCells
    42.                     UserCancelled = True
    43.                     Exit Function
    44.                 End If
    45.                
    46.                 DoEvents
    47.                
    48.             Next lRow
    49.            
    50.         Next lCol
    51.        
    52.     End With
    53.    
    54.     Exit Function
    55.    
    56. ERR_EnumerateCells:
    57.     Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext
    58. End Function

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    From the other thread on this issue:
    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)
    Although I can see that in your code you don't just work with the values - which means there is a high chance you couldn't optimise it, but fortunately in this case you set the same values so you could do:

    oSheet.Range ( < name of range > ).Colour = .Interior.Color
    or:
    oSheet.UsedRange.Colour = .Interior.Color

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