 | OO-Snippets: erase-create_range_nameCommons| Keywords | macro, named range, delete/create named range, last used row, cell containing last balance, erase named range, create named range |
|---|
| Language | OOBasic |
|---|
| Application | Calc |
|---|
| Authors | Ennio-Sr (initial)
Andrew Douglas Pitonyak Marc Santhoff Sasa Kelesevic
|
|---|
| Supported Versions | 1.1.4 1.9.82 beta |
|---|
| Supported OS | Linux |
|---|
| Question | Reassign named range to a cell in last used row
How to determine last row used and set a named range for a cell in that row
GregChi <geardoc36@snet.net>; asked a similar question on users@openoffice.org
and received workarounds more than a direct answer. I had a similar problem and after
reading Andrew Pitonyak's "Useful Macro Information for OOo" and asking a few questions
on dev@api.openoffice.org, wrote a macro which does the job. It considers col. F as the one
containing a formula to determine current account balance (i.e. previous balance , plus credit,
minus debit). Col. headings (A to F) are: Date, Value_date, Description, Debit, Credit, Balance).
Once you have copy pasted the code in your Calc file, you can well assign the macro
(_0_create_last_bal) to the Event/Opening of document so that it is run when the file
is opened. A second macro (create_last_bal) mentioned in the dialog box, does the same
as it just calls the previous one.
|
|---|
| Answer | |
|---|
sub _0_create_last_bal
On Error goto Label
Dim oDoc As Object
Dim oSheet As Object
Dim oCell As Object
Dim oRange
Dim oRanges
Dim sName$
msg$ = "I' m ready to accept new records: for each new one copy formula " +_
"in last balance cell (or run macro 'create_new_rec')."
oDoc=ThisComponent
ocell=ThisComponent.CurrentSelection
oRanges = ThisComponent.NamedRanges
sName$="last_bal"
If oRanges.hasByName(sName$) Then
oRanges.getByName(sName$)
oRanges.removeByName(sName$)
End If
odoc = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
Dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "ToPoint"
args1(0).Value = "$F$6"
dispatcher.executeDispatch(oDoc, ".uno:GoToCell", "", 0, args1())
dispatcher.executeDispatch(oDoc, ".uno:GoDownToEndOfData", "", 0, args1())
ocell=ThisComponent.CurrentSelection
vlc = oCell.getValue()
if vlc = 0 then
dispatcher.executeDispatch(oDoc, ".uno:Cut", "", 0, args1())
dispatcher.executeDispatch(oDoc, ".uno:GoUp", "", 0, args1())
r$ = oCell.CellAddress.row+1
xcl$ = "Sheet1.$F$"+r$
else
r$ = oCell.CellAddress.row+2
xcl$ = "Sheet1.$F$"+r$
End if
Dim oCellAddress As new com.sun.star.table.CellAddress
oCellAddress.Sheet = 0
oRanges.addNewByName(sName$,xcl$,oCellAddress,0)
dispatcher.executeDispatch(oDoc, ".uno:Copy", "", 0, Args1())
Dim args2(1) as new com.sun.star.beans.PropertyValue
args2(0).Name = "By"
args2(0).Value = 1
args2(1).Name = "Sel"
args2(1).Value = false
dispatcher.executeDispatch(oDoc, ".uno:GoDown", "", 0, args2())
dispatcher.executeDispatch(oDoc, ".uno:Paste", "", 0, Args2())
dispatcher.executeDispatch(oDoc, ".uno:GoToStartOfRow", "", 0, args2())
Print msg$
Exit sub
Label:
print "Error!"
Exit Sub
End Sub
Sub create_new_rec
sub _0_create_last_bal
End Sub
|
Changelog| Date | User | Modification |
|---|
| 2005-06-24 | ennio_sr | Version 2.0
I discovered some flaws in the initial version (on some circumstances "last_bal" was created on the last but one row). So it was necessary to re-write the code. The file structure is very simple (as said above). You can put this formula in cell F6:
=IF(ISBLANK(B6);0;F5-D6+E6).
|
|