Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The function below reads data from a closed Excel file and returns the result in an array. It loses formatting, formulas etc. You might want to call the isArrayEmpty function (at the bottom) in your main code to test that the function returned something.</p> <pre><code>Public Function getDataFromClosedExcelFile(parExcelFileName As String, parSheetName As String) As Variant 'see http://www.ozgrid.com/forum/showthread.php?t=19559 'returns an array (1 to nRows, 1 to nCols) which should be tested with isArrayEmpty in the calling function Dim locConnection As New ADODB.Connection Dim locRst As New ADODB.Recordset Dim locConnectionString As String Dim locQuery As String Dim locCols As Variant Dim locResult As Variant Dim i As Long Dim j As Long On Error GoTo error_handler locConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" _ &amp; "Data Source=" &amp; parExcelFileName &amp; ";" _ &amp; "Extended Properties=""Excel 8.0;HDR=YES"";" locQuery = "SELECT * FROM [" &amp; parSheetName &amp; "$]" locConnection.Open ConnectionString:=locConnectionString locRst.Open Source:=locQuery, ActiveConnection:=locConnection If locRst.EOF Then 'Empty sheet or only one row ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '''''' FIX: an empty sheet returns "F1" '''''' http://support.microsoft.com/kb/318373 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' If locRst.Fields.Count = 1 And locRst.Fields(0).Name = "F1" Then Exit Function 'Empty sheet ReDim locResult(1 To 1, 1 To locRst.Fields.Count) As Variant For i = 1 To locRst.Fields.Count locResult(1, i) = locRst.Fields(i - 1).Name Next i Else locCols = locRst.GetRows ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '''''' FIX: an empty sheet returns "F1" '''''' http://support.microsoft.com/kb/318373 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' If locRst.Fields.Count = 1 And locRst.Fields(0).Name = "F1" And UBound(locCols, 2) = 0 And locCols(0, 0) = "" Then Exit Function 'Empty sheet ReDim locResult(1 To UBound(locCols, 2) + 2, 1 To UBound(locCols, 1) + 1) As Variant If locRst.Fields.Count &lt;&gt; UBound(locCols, 1) + 1 Then Exit Function 'Not supposed to happen For j = 1 To UBound(locResult, 2) locResult(1, j) = locRst.Fields(j - 1).Name Next j For i = 2 To UBound(locResult, 1) For j = 1 To UBound(locResult, 2) locResult(i, j) = locCols(j - 1, i - 2) Next j Next i End If locRst.Close locConnection.Close Set locRst = Nothing Set locConnection = Nothing getDataFromClosedExcelFile = locResult Exit Function error_handler: 'Wrong file name, sheet name, or other errors... 'Errors (#N/A, etc) on the sheet should be replaced by Null but should not raise an error If locRst.State = ADODB.adStateOpen Then locRst.Close If locConnection.State = ADODB.adStateOpen Then locConnection.Close Set locRst = Nothing Set locConnection = Nothing End Function Public Function isArrayEmpty(parArray As Variant) As Boolean 'Returns false if not an array or dynamic array that has not been initialised (ReDim) or has been erased (Erase) If IsArray(parArray) = False Then isArrayEmpty = True On Error Resume Next If UBound(parArray) &lt; LBound(parArray) Then isArrayEmpty = True: Exit Function Else: isArrayEmpty = False End Function </code></pre> <p>Sample use:</p> <pre><code>Sub test() Dim data As Variant data = getDataFromClosedExcelFile("myFile.xls", "Sheet1") If Not isArrayEmpty(data) Then 'Copies content on active sheet ActiveSheet.Cells(1,1).Resize(UBound(data,1), UBound(data,2)) = data End If End Sub </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload