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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I Read from an Excel Spreadsheet in VBScript?

Status
Not open for further replies.

JohnnyBGoode

Programmer
May 1, 2002
76
CA
How can i read from a excel spreadsheet (ie. workbook.xls) in VBScript?
 
Not sure how much this might help you (I'm not that good with VBS), but you could probably figure out how to reverse this following code to read values from a spreadsheet instead of add them. At the very least this will show you how to access Excel:
Code:
Set fso = CreateObject("Scripting.FileSystemObject")
FileLoc = NetPath & FormFiles & "spreadsheet.xls"

    ' Checks to see if Excel is running - creates object
    If fso.FileExists(FileLoc) Then
        On Error Resume Next
        Set oExcel = GetObject(, "Excel.Application")
        If Err.Number <> 0 Then
            Err.Clear
            Set oExcel = CreateObject(&quot;Excel.Application&quot;)
        End If
    Else
        strErr = FileLoc & &quot; could not be found.&quot;
        MsgBox (strErr), vbCritical, &quot;Error: File Not Found&quot;
        Exit Function
    End If

' Opens desired spreadsheet
Set oWrkBook = oExcel.Workbooks.Open (FileLoc)

' Search each row for either blank(new) row or matching value
varRow = 1
Do While oExcel.Worksheets(&quot;Current Year&quot;).Cells(varRow, 1) > &quot;&quot;
    If oExcel.Worksheets(&quot;Current Year&quot;).Cells(varRow, 1) = varVariable Then
        Exit Do
    End If
    varRow = varRow + 1
Loop

' Add values to spreadsheet.
oExcel.Worksheets(&quot;Current Year&quot;).Cells(varRow, 1).Value = txtVar1
oExcel.Worksheets(&quot;Current Year&quot;).Cells(varRow, 2).Value = varVar2
oExcel.Worksheets(&quot;Current Year&quot;).Cells(varRow, 3).Value = txtVar3
oExcel.Worksheets(&quot;Current Year&quot;).Cells(varRow, 4).Value = txtVar4
oExcel.Worksheets(&quot;Current Year&quot;).Cells(varRow, 5).Value = txtVar5
oExcel.Worksheets(&quot;Current Year&quot;).Cells(varRow, 6).Value = txtVar6
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top