Home » excel » Excel VBA: How to Extend a Range Given a Current Selection

Excel VBA: How to Extend a Range Given a Current Selection

Posted by: admin March 7, 2020 Leave a comment

Questions:

I want to do something like:

E18-(1,1) &":" &E18+(1,1)

My intent is to keep the selection of range E18 (value = B) and extend the selection to D16:F20.

Cell_Image_Excel_Highlighted_B

If I have a cell’s range of E18 and I want to extend the range to D16:F20, how can I do this?

How to&Answers:
Range(Cells(WorksheetFunction.Max(1, Selection.Row - 1), _
      WorksheetFunction.Max(1, Selection.Column - 1)), _
      Cells(WorksheetFunction.Min(Selection.Worksheet.Rows.Count, _
      Selection.Row + 1), _
      WorksheetFunction.Min(Selection.Worksheet.Columns.Count, _
      Selection.Column + 1))).Select

upd: thanks Siddharth Rout for formating my msg

Answer:

You mean like this?

SYNTAX

ExpandRange [Range], [Number of Col on left], [Number of Rows on Top], [Number of Col on right], [Number of Rows down]

Sub Sample()
    Debug.Print ExpandRange(Range("B5"), 1, 1, 1, 1)            '<~~ $A$4:$C$6
    Debug.Print ExpandRange(Range("A1"), 1, 1, 1, 1)            '<~~ Error
    Debug.Print ExpandRange(Range("XFD4"), 1, 1, 1, 1)          '<~~ Error
    Debug.Print ExpandRange(Range("XFD1048576"), 1, 1, 1, 1)    '<~~ Error
    Debug.Print ExpandRange(Range("E5"), 1, 1, 1, 1)            '<~~ $D$4:$F$6
End Sub

Function ExpandRange(rng As Range, lft As Long, tp As Long, _
rt As Long, dwn As Long) As String
    If rng.Column - lft < 1 Or _
       rng.Row - tp < 1 Or _
       rng.Column + rt > ActiveSheet.Columns.Count Or _
       rng.Row + dwn > ActiveSheet.Rows.Count Then
        ExpandRange = "Error"
        Exit Function
    End If

    ExpandRange = Range(rng.Offset(-1 * tp, -1 * lft).Address & ":" & _
                        rng.Offset(dwn, rt).Address).Address
End Function

Answer:

Here is the simple code that I use to resize an existing selection.

Selection.Resize(Selection.Rows.Count + 5, Selection.Columns.Count + 50).Select

This will add 5 to the row count and 50 to the column count. Adapt to suit your needs.

Answer:

You can use Application.WorksheetFunction.Offset() which is richer than VBA’s Offset and does everything required by the question.
I think it does what Siddharth Rout ExpandRange does, without the need of a UDF.

Answer:

Instead of returning an absolute address, I modifying the syntax above to return a range. Credit goes to Siddharth Rout = )

Function ExpandRG(rng As Variant, lft As Long, tp As Long, rt As Long, dwn As Long) _
 As Range
 Set ws = rng.Parent
If rng.Column - lft < 1 Or _
   rng.Row - tp < 1 Or _
   rng.Column + rt > ActiveSheet.Columns.Count Or _
   rng.Row + dwn > ActiveSheet.Rows.Count Then
        MsgBox "Out of range"
        Exit Function
End If

 Set rng = ws.Range(rng.Offset(-1 * tp, -1 * lft).Address & ":" & _
                    rng.Offset(dwn, rt).Address)                        
End Function

Sub aa()
Dim ori_add, O_add, New_add As Range
Set ori_add = Range("B2")
Set O_add = ori_add

Call ExpandRG(ori_add, 1, 1, 1, 1)
Set New_add = ori_add

MsgBox "Original address " & O_add.Address & ", new address is" & New_add.Address
End Sub