2012年8月15日水曜日

あるセルにとって、最終行、列を返す

'rgStart 単一セル
'horv 縦か横か、デフォルト縦
'戻り値
'  縦の場合 0→エラー
'  横の場合 ""→エラー
Public Function getBounce(ByRef rgStart As Range, Optional ByVal horv As String = "V") As String
Dim blnVertical As Boolean
Dim rgBounce As Range
    blnVertical = IIf(UCase(horv) = "H", False, True)
    If blnVertical Then
        Set rgBounce = rgStart.Worksheet.Range(ConvertToLetter(rgStart.Column) & rgStart.Worksheet.Rows.Count).End(xlUp)
        If rgStart.Cells.Count > 1 Then
            getBounce = IIf(blnVertical, 0, "")
            Exit Function
        End If
        If rgStart.Value = "" And rgBounce.Row <= rgStart.Row Then
            getBounce = 0
        Else
            getBounce = rgBounce.Row
        End If
        Exit Function
    Else
        Set rgBounce = rgStart.Worksheet.Range(ConvertToLetter(rgStart.Worksheet.Columns.Count) & rgStart.Row).End(xlToLeft)
        If rgStart.Value = "" And rgBounce.Column <= rgStart.Column Then
            getBounce = ""
        Else
            getBounce = ConvertToLetter(rgBounce.Column)
        End If
        Exit Function
    End If
    
End Function


Function ConvertToLetter(iCol As Integer) As String
    ConvertToLetter = Split(Cells(1, iCol).Address, "$")(1)
End Function

0 件のコメント:

コメントを投稿