Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Excel active rows
Message
From
24/05/2003 05:38:28
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
23/05/2003 20:48:28
General information
Forum:
Visual FoxPro
Category:
Object Oriented Programming
Miscellaneous
Thread ID:
00792018
Message ID:
00792518
Views:
18
Geoff,
You almost replied yourself :)
.Rows.Delete
If you look at it closely .Rows uses the range (the sheet itself in your code). Supply it the correct range. ie:
* Range object is quite flexible
* .range('15:25') refers to rows 15-25, .range('A:D') refers to columns A-D
* .range('A1') refers to single cell A1, .range('B5:D10') refers to a
* rectangular area
* .range('B5:D10, A1, 20:30, F:J') refers to multiple ranges - not safe
 

nStartRow = 15
nRowsToDelete = 10
lcRange = trans(nStartRow)+':'+trans(nStartRow+nRowsTodelete) && '15:25'

lcExportFile=GETFILE()
oExcel = CREATEOBJECT([Excel.Application])
WITH oExcel
    *!* Open the workbook
    .Workbooks.Open(lcExportFile)
    with .ActiveWorkBook.ActiveSheet && Not mandatory but I suggest
       .Range(lcRange).Rows.Delete()	   && Delete the rows
       .Range("A1").Activate
    endwith
ENDWITH
Cetin


>Thanks Cetin
>
>Always happy to stand corrected. I have struggled to understand how to use the various objects, etc in Excel (and other office products) so your code has really helped.
>
>In the case I was working on I am the creator of the extract file which is really just a transit file for importing into the users accounting system. So except in cases where the end user opened and modified the spreadsheet
>'.ActiveSheet.UsedRange.Rows.Count' would suffice. Still, better to be safe than sorry. It does however raise the issue of whether one should carry on after one has detected user modification. Ho hum!!!
>
>Do you know how to delete x number of rows starting at row y.
>
>
lcExportFile=GETFILE()
>oExcel = CREATEOBJECT([Excel.Application])
>WITH oExcel
>    *!* Open the workbook
>    .Workbooks.Open(lcExportFile)
>    .Rows.Delete()			&& Delete the rows
>    .Application.Goto(.Worksheets("Sheet1").Range("A1"),.T.)
>ENDWITH
>
>Works for my needs at the moment but it does seem a bit brutal.
>
>Thanks again
>Geoff Scott
>
>>>Sorry folks, I might have put you wrong there. It appears that
>>>
>>>
>>>oExcel = CREATEOBJECT([Excel.Application])
>>>WITH oExcel
>>>    .Workbooks.Open(GETFILE())
>>>    ?.Worksheets("Sheet1").UsedRange.Rows.Count
>>>* or
>>>    ?.ActiveSheet.UsedRange.Rows.Count
>>>    .Workbooks.Close()
>>>ENDWITH
>>>oExcel=.F.
>>>
>>>Returns the number of rows used in the sheet.
>>>
>>>Regards
>>>Geoff
>>Snip...
>>
>>Geoff,
>>At first look UsedRange.Rows.Count is the answer but it's NOT.
>>UsedRange returns info about 'Used' cells in a sheet. If your sheet has some columns and/or rows left empty at start then what you get is not what you should get to find first available unused row. Suppose someone has customer.xls generated from customer table but user moved the data to start at say M80 and saved. If customer.dbf had 92 rows and 10 columns, UsedRange.Rows.Count and USedRange.Columns.Count would still return 92 and 10 not 80+92, M+10. Hard to describe :) To see what I mean create a test customer.xls, open manually and move the data to start from another row/col instead of A1, save, quit and run (assuming saved in current folder) :
>>
>>#Define xlA1  1
>>#Define xlLastCell  11
>>
>>lcXLS = Sys(5)+Curdir()+'customer.xls'
>>oExcel = Createobject('Excel.application')
>>oExcel.Workbooks.Open(lcXLS)
>>With oExcel.Activeworkbook.ActiveSheet
>>  lcLastCell = .Range("A1").SpecialCells(xlLastCell).Address(.F.,.F.,xlA1)
>>  ? 'Last Cell',lcLastCell
>>  With .UsedRange
>>    ? 'Used Range Address',.Address(.F.,.F.,xlA1)
>>    ? 'Used Range Rows',.Rows.Count, 'Cols', .Columns.Count
>>  Endwith
>>  lcFirstUnusedRowCol = "A"+;
>>     Transform(Int(Val(Chrtran(;
>>       lcLastCell,Chrtran(lcLastCell,'0123456789',''),''))+1))
>>  ? 'First Column at First unused row',lcFirstUnusedRowCol
>>  .Range(lcFirstUnusedRowCol).Activate
>>Endwith
>>oExcel.Visible = .T.
>>
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Reply
Map
View

Click here to load this message in the networking platform