(view source code of excelrd.vbs as plain text)
Option ExplicitDim arrSheet, intCount' Read and display columns A,B, rows 2..8 of "ReadExcelTest.xlsx"arrSheet = ReadExcel( "ReadExcelTest.xlsx", "Sheet1", "A1", "B8", True )
For intCount = 0 To UBound( arrSheet, 2 )
WScript.Echo arrSheet( 0, intCount ) & vbTab & arrSheet( 1, intCount )
NextWScript.Echo "==============="
' An alternative way to get the same resultsarrSheet = ReadExcel( "ReadExcelTest.xlsx", "Sheet1", "A2", "B8", False )
For intCount = 0 To UBound( arrSheet, 2 )
WScript.Echo arrSheet( 0, intCount ) & vbTab & arrSheet( 1, intCount )
NextFunction ReadExcel( myXlsFile, mySheet, my1stCell, myLastCell, blnHeader )
' Function : ReadExcel' Version : 3.00' This function reads data from an Excel sheet without using MS-Office'' Arguments:' myXlsFile [string] The path and file name of the Excel file' mySheet [string] The name of the worksheet used (e.g. "Sheet1")' my1stCell [string] The index of the first cell to be read (e.g. "A1")' myLastCell [string] The index of the last cell to be read (e.g. "D100")' blnHeader [boolean] True if the first row in the sheet is a header'' Returns:' The values read from the Excel sheet are returned in a two-dimensional' array; the first dimension holds the columns, the second dimension holds' the rows read from the Excel sheet.'' Written by Rob van der Woude' http://www.robvanderwoude.comDim arrData( ), i, j
Dim objExcel, objRS Dim strHeader, strRangeConst adOpenForwardOnly = 0
Const adOpenKeyset = 1
Const adOpenDynamic = 2
Const adOpenStatic = 3
' Define header parameter string for Excel objectIf blnHeader Then
strHeader = "HDR=YES;"
ElsestrHeader = "HDR=NO;"
End If
' Open the object for the Excel fileSet objExcel = CreateObject( "ADODB.Connection" )
' With IMEX=1 numbers won't be ignored; tip by Thomas Willig. ' Connection string updated by Marcel Niënkemper to open Excel 2007 (.xslx) files.objExcel.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" _
& myXlsFile & ";Extended Properties=""Excel 12.0;IMEX=1;" _
& strHeader & """"
' Open a recordset object for the sheet and rangeSet objRS = CreateObject( "ADODB.Recordset" )
strRange = mySheet & "$" & my1stCell & ":" & myLastCell
objRS.Open "Select * from [" & strRange & "]", objExcel, adOpenStatic
' Read the data from the Excel sheeti = 0
Do Until objRS.EOF
' Stop reading when an empty row is encountered in the Excel sheetIf IsNull( objRS.Fields(0).Value ) Or Trim( objRS.Fields(0).Value ) = "" Then Exit Do
' Add a new row to the output arrayReDim Preserve arrData( objRS.Fields.Count - 1, i )
' Copy the Excel sheet's row values to the array "row" ' IsNull test credits: Adriaan WestraFor j = 0 To objRS.Fields.Count - 1
If IsNull( objRS.Fields(j).Value ) Then
arrData( j, i ) = ""
ElsearrData( j, i ) = Trim( objRS.Fields(j).Value )
End If
Next ' Move to the next rowobjRS.MoveNext
' Increment the array "row" numberi = i + 1
Loop ' Close the file and release the objectsobjRS.Close
objExcel.Close
Set objRS = Nothing
Set objExcel = Nothing
' Return the results ReadExcel = arrDataEnd Function
page last modified: 2025-10-11; loaded in 0.0053 seconds