borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (List Box Scripting Problems)Next Topic (VBA DTPicker) New Topic New Poll Post Reply
AndreaVB Forum : VBA (Access, Excel, Word, ...) : Reading data from an Excel File
Poster Message
richardrfarthing
Level: Guest


icon Reading data from an Excel File

Hi Everyone

I have a script that reads data from a excel spreadsheet using ole.

I need to search a excel file on a certian field (Coloum), then once found read all data from that row into varables.

I am using VB and have a script that can do the row part.

Here it is:


BREAK ON CLS
;
IF REDIRECTOUTPUT("") = 0 ENDIF
;
$cFileName = "C:kixtest.xls"
READEXCEL($cFileName)
FUNCTION READEXCEL($cfilename)

$objexcel = Createobject("Excel.Application")
IF $cfilename = ""
     ? "Excel file not found"
     GOTO Cleanup
ENDIF

; -- Open the Excel Work book
$_=$objexcel.workbooks.open ($cfilename)

$nRow = 1 ; -- Assume Row one is column Headings

$x = $objexcel.cells($nRow, 1).value

WHILE $objexcel.cells($nRow, 1).value <> ""

$nRow = $nRow + 1

IF $objexcel.cells($nRow, 1).value = ""
      GOTO Cleanup
ENDIF

$value1 = $objexcel.cell($nRow,1).value
$value1 ?

$value2 = $objexcel.cells($nRow,2).value
$value2 ?

$value3 = $objexcel.cells($nRow,3).value
$value3 ?

LOOP

;
:cleanup
?"Script is Complete" ?
$objexcel.application.quit
$objexcel = ""
ENDFUNCTION  




Thanks

Any Ideas or good web sites that good ole excel reference

19-05-2003 at 07:42 PM
| Quote Reply
~Bean~
Level: VB Guru


Registered: 07-04-2003
Posts: 488
icon Re: Reading data from an Excel File

Is it known how many columns you have? or are you checking for a blank column header to determine the end of worksheet? and I'm not sure if I know what language script you're using...but try this...


; -- Open the Excel Work book
$_=$objexcel.workbooks.open ($cfilename)

$myVal = WhatImLookingFor
$Col = 1
$x = $objexcel.cells(1, $Col).value

WHILE $x <> ""
    ;Check for End of File
    IF $objexcel.cells($nRow, 1).value = ""
        GOTO Cleanup
    ENDIF

;Check for Column Header Value that we are Looking For...
IF x$ = $myVal Then
    $Row = 2
    $x = $objexcel.cells($Row, $Col).value
    WHILE $x <> ""
        ;now do something with Row Values ($x)
        ? $x
        $Row = $Row + 1
        $x = $objexcel.cells($Row, $Col).value
    LOOP
    GOTO Cleanup
END IF

$Col = $Col + 1
$x = $objexcel.cells(1, $Col).value
LOOP


____________________________
Eggheads unite! You have nothing to lose but your yolks.

20-05-2003 at 02:06 PM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
AndreaVB Forum : VBA (Access, Excel, Word, ...) : Reading data from an Excel File
Previous Topic (List Box Scripting Problems)Next Topic (VBA DTPicker) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder