Hi, I have tried to write a function that returns the object returned by Cells. I.e. Instead of writing:
I'd like to write:
Here is the code that I wrote:
What appears to be happening, is that the return is of type Variant/Empty instead of Variant/Object/Range. Is it not possible to pass back the Range object?
NOTE: this is only an example. I'd be using this for other things.
I'm using VBA 6.5.1053 from Excel 2007 on WinXP SP 3
Thanks,
A
Code:
Range(Cells(ActiveCell.Column-1, ActiveCell.Row), Cells(ActiveCell.Column-1, ActiveCell.Row)).Select
Code:
Range(CellsOffset("R[-1]C"), CellsOffset("R[-1]C")).Select
Here is the code that I wrote:
Code:
Sub test()
Call Range(CellsOffset("R[-1]C"), CellsOffset("R[-1]C")).Select
End Sub
Function CellsOffset(offset)
localError = False
rIndex = InStr(offset, "R")
If (rIndex < 1) Then
localError = True
Else
cIndex = InStr(offset, "C")
If cIndex < 1 Then
localError = True
Else
Row = Mid(offset, rIndex + 1, cIndex - rIndex - 1)
Col = Mid(offset, cIndex + 1)
If Len(Row) = 0 Then
Row = ActiveCell.Row
ElseIf Left(Row, 1) = "[" Then
If Right(Row, 1) = "]" Then
Row = ActiveCell.Row + CInt(Mid(Row, 2, Len(Row) - 2))
Else
localError = True
End If
Else
Row = CInt(Row)
End If
If Len(Col) = 0 Then
Col = ActiveCell.Column
ElseIf Left(Col, 1) = "[" Then
If Right(Col, 1) = "]" Then
Col = ActiveCell.Column + CInt(Mid(Col, 2, Len(Col) - 2))
Else
localError = True
End If
Else
Col = CInt(Col)
End If
'MsgBox "rIndex = " & rIndex & " cIndex = " & cIndex & " row = " & Row & " col = " & Col
End If
End If
If localError Then
MsgBox CellOffset & "(""" & offset & """) is an invalid offset."
CellsOffset = Cells(ActiveCell.Row, ActiveCell.Column)
Else
CellsOffset = Cells(Row, Col)
End If
End Function
NOTE: this is only an example. I'd be using this for other things.
I'm using VBA 6.5.1053 from Excel 2007 on WinXP SP 3
Thanks,
A
Comment