Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy an Excel Range to an Array fast?

Status
Not open for further replies.

timtaylor

Programmer
Nov 1, 2002
1
DE
Hi guys,

is there a way to copy a range of excel cells to an array without looping through each cell.

the problem is that my software has to perform 10000 operations like this and the cellwise approach takes up to 30 minutes.
There should be a faster way.

Can you help me?

Thank you
 
You may want to open the Excel worksheet as a standard ADO recordset: see the sample below... a snippet from a large class module we often use here...

Giorgio Vidali
Director of Software Development
Automated Business Designs, Inc.

Code:
Public Function RecordsetGet(FromExcelFileName As String, FromExcelDataRange As String) As ADODB.Recordset

    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim exString As String
    Dim exRange As String
    Dim sql As String
    
    On Error GoTo L_LocalErrorHandler

    exString = "Data Source=" & FromExcelFileName & "; Extended Properties=Excel 8.0;"
    
    exRange = FromExcelDataRange
    If Right$(exRange, 1) <> &quot;$&quot; Then exRange = exRange & &quot;$&quot;

    Set cn = CreateObject(&quot;ADODB.Connection&quot;)
    Set rs = CreateObject(&quot;ADODB.Recordset&quot;)

    With cn
        .Provider = &quot;Microsoft.Jet.OLEDB.4.0&quot;
        .ConnectionString = exString
        .CursorLocation = adUseClient
        .Open
    End With
        
    sql = &quot;SELECT * FROM [&quot; & exRange & &quot;]&quot;

    With rs
        .ActiveConnection = cn
        .CursorLocation = adUseClient
        .CursorType = m_RecordsetCursorType
        .LockType = m_RecordsetLockType
        .Open sql
    End With

    Set RecordsetGet = rs

    Exit Function

L_LocalErrorHandler:

    Screen.MousePointer = vbDefault
    Err.Raise Err.Number, &quot;cExcelRecordset:RecordsetGet&quot;, Err.Description

End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top