2010年9月28日 星期二

替代 Range ("A65536").End (xlUp).Row

REM ***** BASIC *****
' Source
' http://www.h3.dion.ne.jp/~sakatsu/OOo_vs_VBA.htm
' http://blog.livedoor.jp/addinbox/archives/51212979.html
' http://openoffice3.web.fc2.com/OOoBasic_General.html

Sub Main

Dim oSheet As Object
Dim oCell As Object

oSheet = ThisComponent.Sheets(0)

'oCell = GetLastCell(oSheet.Rows(1),True)
'oCell = GetLastCell(oSheet.getCellRangeByName("A2:AMJ2"),True)
'oCell = GetLastCell(oSheet.Columns(0),True)

oCell = GetLastCell(oSheet.getCellRangeByName("A1:A65536"),True)

'MsgBox oCell.AbsoluteName
MsgBox oCell.CellAddress.Row
MsgBox oCell.CellAddress.Column
End Sub

'=======[ GetLastCell ]==============
' [argRowCol]
' It appoints one Row range or one Column range.
' In the case of one Row range, it is searched the Right-End cell.
' In the case of one Column range, it is searched the Bottom cell.
' [argNextCell]
' In the case of True or omission, next cell (Empty Cell) of the last cell returns.
' In the case of False, the last cell returns.
' In the case of an empty cell all, the Top cell (or Left-End cell) returns.
' When the last cell is not Empty-Cell, Nothing returns in argNextCell=True,
' and the last cell returns in argNextCell=False.

Global Function GetLastCell(ByRef argRowCol As Object, _
Optional ByVal argNextCell As Boolean) As Object
Dim oRanges As Object
Dim oRowCol As Object
Dim oEmptyRanges As Object
Dim intEmptyRangesCount As Integer
Dim oLastRange As Object
Dim oCell As Object
Dim blnNextCell As Boolean
Dim blnGetRow As Boolean
Dim lngRow As Long
Dim lngCol As Long

If IsMissing(argNextCell) Then
blnNextCell = True
Else
blnNextCell = argNextCell
End If

Select Case argRowCol.ImplementationName
Case "ScCellObj"
GetLastCell = Nothing
Exit Function
Case "ScCellRangeObj", "ScTableColumnObj", "ScTableRowObj"
oRowCol = argRowCol
Case "ScCellRangesObj", "ScTableColumnsObj", "ScTableRowsObj"
oRanges = argRowCol
If (oRanges.Count = 1) Then
oRowCol = oRanges(0)
Select Case oRowCol.ImplementationName
Case "ScCellObj"
GetLastCell = Nothing
Exit Function
Case "ScCellRangeObj", "ScTableColumnObj", "ScTableRowObj"
'OK
Case "ScCellRangesObj", "ScTableColumnsObj", "ScTableRowsObj"
GetLastCell = Nothing
Exit Function
Case Else
GetLastCell = Nothing
Exit Function
End Select
Else
GetLastCell = Nothing
Exit Function
End If
Case Else
GetLastCell = Nothing
Exit Function
End Select

If (oRowCol.Rows.Count = 1) Then
blnGetRow = False '1 Row is [get Right_End_Cell]
ElseIf (oRowCol.Columns.Count = 1) Then
blnGetRow = True '1 column is [get Bottom_Cell]
Else
GetLastCell = Nothing
Exit Function
End If

'Last cell is Empty?
With oRowCol.RangeAddress
oCell = oRowCol.SpreadSheet.getCellByPosition(.EndColumn, .EndRow)
If (oCell.Type <> com.sun.star.table.CellContentType.EMPTY) Then
If blnNextCell Then
GetLastCell = Nothing
Else
GetLastCell = oCell
End If
Exit Function
End If
End With

oEmptyRanges = oRowCol.queryEmptyCells()
intEmptyRangesCount = oEmptyRanges.Count
If (intEmptyRangesCount = 0) then
GetLastCell = oRowCol.getCellByPosition(0,0)
Else
oLastRange = oEmptyRanges.getByIndex(intEmptyRangesCount -1)
oCell = oLastRange.getCellByPosition(0,0)
If blnNextCell Then
GetLastCell = oCell
Else
With oCell.CellAddress
If blnGetRow Then
lngCol = .Column
lngRow = .Row - 1
Else
lngCol = .Column - 1
lngRow = .Row
End If
End With
GetLastCell = oCell.SpreadSheet.getCellByPosition(lngCol, lngRow)
End If
End If
End Function

沒有留言: