#35440 - 2003-01-21 04:54 PM
[Excel] Finding last row & Adding sheet
|
Y3PP3R
Fresh Scripter
Registered: 2003-01-21
Posts: 6
|
Hey all, I'm making a script that reads data from an acces DB and insert it into an excel Workbook.
Now I need to find the last row with data, so I can add new data under existing data, and I want to make a sheet for each different item in a row, called 'objects'. So all records with object 'car' need to be inserted in sheet 'car' and with object 'bike' in sheet 'bike' and so on...
I know how to read and manipulate a database and open an existing file, else create a new one...
Who wants to make me happy? [ 21. January 2003, 17:00: Message edited by: Jasper Timmer ]
|
Top
|
|
|
|
#35441 - 2003-01-21 05:33 PM
Re: [Excel] Finding last row & Adding sheet
|
Shawn
Administrator
Registered: 1999-08-13
Posts: 8611
|
Hi Jasper, welcome to korg.
I'm trying to paint a mental picture of what your asking. So a sample of your input would be something like this (using csv just for convience):
"car","ford","blue" "bike","harley","grey" "car","honda","silver" "bike","honda","black"
then you want two xlsheets produced (name in brackets), like this:
[car]
ford,blue honda,silver
[bike] harley,grey honda,black
Plus, these rows will be additive in that you will be appending new rows every time you run the script ?
|
Top
|
|
|
|
#35442 - 2003-01-21 05:41 PM
Re: [Excel] Finding last row & Adding sheet
|
Y3PP3R
Fresh Scripter
Registered: 2003-01-21
Posts: 6
|
You did understand the problem fine!
Additional info: all records that are added to my workbook are deleted, so all data what will be inserted will be new and needs to be under all exitsting rows. The sheets need to have the name of the object...
|
Top
|
|
|
|
#35443 - 2003-01-21 05:44 PM
Re: [Excel] Finding last row & Adding sheet
|
Y3PP3R
Fresh Scripter
Registered: 2003-01-21
Posts: 6
|
I forgot one thing: I can read all the different objects from an other table...
|
Top
|
|
|
|
#35445 - 2003-01-21 07:48 PM
Re: [Excel] Finding last row & Adding sheet
|
Y3PP3R
Fresh Scripter
Registered: 2003-01-21
Posts: 6
|
The code (link) you refer to, does nothing with me, only output is a1 at the console
How comes?
Greetz, Y3PP3R
|
Top
|
|
|
|
#35447 - 2003-01-22 06:37 PM
Re: [Excel] Finding last row & Adding sheet
|
Y3PP3R
Fresh Scripter
Registered: 2003-01-21
Posts: 6
|
I'm using Kix 4.12 and I'd manage to get it working, but the last value it returns is a1.
New question 1: How can I start adding rows under the last row found? Which variable from your example returns the row on which I should start?
New question 2: How can I check if a sheet already exists and add multiple sheets?
I've added my code, so you can see what I've got and what goes wrong.
code:
Global $oXl,$xlRow,$SearcStartRow,$xlHeader
$filename = "c:\werktijdreg\registraties.xls" $dsn='DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=c:\werktijdreg\werkinfo.mdb' $sql="Select * FROM Objecten" $recordset = DBCommand($dsn,$sql)
$oXL = CreateObject("EXCEL.application")
IF EXIST ($filename) $RC = $oXl.Workbooks.Open($filename) For $object=0 to ubound($recordset,0) If $oxl.sheets.name($recordset[$object,0]) = false $objNewSheet = $objWorkBook.Worksheets.Add $objNewSheet.Name = $recordset[$object,0] $array = "Datum", "Naam", "Plaats", "Taken", "Afstand", "Duur" $oXL.Range("A1:F1").Value = $array Endif next
else $rc = $oXL.Workbooks.Add For $object=0 to ubound($recordset,1) $objNewSheet = $objWorkBook.Worksheets.Add $objNewSheet.Name = $recordset[$object,0] $array = "Datum", "Naam", "Plaats", "Taken", "Afstand", "Duur" $oXL.Range("A1:F1").Value = $array next ENDIF
$sql="Select * FROM Registraties" $recordset = DBCommand($dsn,$sql)
if @error = 0 $oXL.Visible = 1
For $row=0 To ubound($recordset,1) for $column=0 to ubound($recordset,2) $oXL.Cells(($row+2),$column).Value = $recordset[$row,$column] Next Next
$oXL.Range("A1:G1").Font.Bold = 1 $rc = $oXL.Range("A1:G1").EntireColumn.AutoFit
;##loop door colom A om een waarde te vinden##
$objWorksheet = $objWorkbook.ActiveSheet $objCell = $objWorksheet.Range("a2")
While $objCell.Offset(0,1).Value <> "" $Value = $objCell.Value ? $Value $objCell = $objCell.Offset(1,0) Loop
If Exist($FileName) $RC = $oXl.ActiveWorkbook.Save Else $RC = $oXl.ActiveWorkbook.SaveAs($FileName,-4143,"","",0,0,,,0) ; xlWorkbookNormal = -4143 EndIf
$oXL.UserControl = 1 else ? @error + " / " @serror endif exit
;############################################################### function DBCommand($ConnDSN,$sql) Dim $objConn, $adStateOpen dim $Conntimeout, $CmdTimeout dim $cmdCommand, $rsRecordset dim $Records, $FinalRecords dim $adCmdText, $adLockReadOnly, $adOpenStatic dim $row, $rows, $column, $columns
$ConnDSN=trim($ConnDSN) if not $ConnDSN exit 87 endif $sql=trim($sql) if not $sql exit 87 endif
$adStateOpen=1 $ConnTimeout=15 $CmdTimeout=30 $adCmdText = 1 $adOpenStatic = 3 $adLockReadOnly = 1
; open the database connection $objConn = CreateObject("ADODB.Connection") if @ERROR exit @ERROR endif $objConn.ConnectionTimeout = $ConnTimeout if @ERROR exit @ERROR endif $objConn.CommandTimeout = $CmdTimeout if @ERROR exit @ERROR endif $objConn.Open($ConnDSN) if @ERROR exit @ERROR endif if not $objConn.State=$adStateOpen $objConn='' $DBCommand='' exit @ERROR endif
; create the database command object $cmdCommand = CreateObject('ADODB.Command') if @ERROR exit @ERROR endif $cmdCommand.ActiveConnection = $objConn if @ERROR exit @ERROR endif $cmdCommand.CommandType = $adCmdText if @ERROR exit @ERROR endif $cmdCommand.CommandText = $sql if @ERROR $DBCommand=@ERROR exit @ERROR endif
if instr($sql,'SELECT')=1 ; return a recordset
; create the recordset object $rsRecordSet = CreateObject('ADODB.Recordset') if @ERROR exit @ERROR endif $rsRecordset.CursorType = $adOpenStatic if @ERROR exit @ERROR endif $rsRecordset.LockType = $adLockReadOnly if @ERROR exit @ERROR endif $rsRecordset.Open($cmdCommand) if @ERROR exit @ERROR endif
if $rsRecordset.EOF and $rsRecordSet.BOF ; recordset is empty $FinalRecords='' else if @ERROR exit @ERROR endif
; retrieve all records at once and transpose into tabular format $Records = $rsRecordset.GetRows() $columns=ubound($records,1) $rows=ubound($records,2) redim $FinalRecords[$rows,$columns] for $row=0 to $rows for $column=0 to $columns $FinalRecords[$row,$column]=$records[$column,$row] next next endif
; close recordset if $rsRecordset.state=$adStateOpen $rsRecordset.Close() if @ERROR exit @ERROR endif endif
$rsRecordset='' $cmdCommand=''
$DBCommand=$FinalRecords else $rsRecordset=$cmdCommand.Execute() $cmdCommand='' $rsRecordset='' if @ERROR exit @ERROR endif
$DBCommand=0 endif
; close the database connection If $objConn.State = $adStateOpen $objConn.Close() if @ERROR exit @ERROR endif EndIf $objConn=''
exit 0 endfunction
[ 22. January 2003, 18:47: Message edited by: Jasper Timmer ]
|
Top
|
|
|
|
#35449 - 2003-01-23 04:28 PM
Re: [Excel] Finding last row & Adding sheet
|
Y3PP3R
Fresh Scripter
Registered: 2003-01-21
Posts: 6
|
It's for a company that uses an excelsystem, which works fine and they don't want a new system. Now they have to type in all the data from notes, but I made a pocketpc program which collects data from all of the workers, but it needs to be exported to excelsheets, sorted by object(sheets). That's the way they did it at that's the way they want it [ 23. January 2003, 16:31: Message edited by: Jasper Timmer ]
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 255 anonymous users online.
|
|
|