Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 750
NCC Avatar asked May 21 '12 20:05

NCC


People also ask

How will you extend the range of selection?

Press "F8" to enter Extend Selection mode, which allows you to extend the selection with just your arrow keys. Unlike the previous technique, you do not need to hold the "Shift" key.

How do I resize a selection in VBA?

Below is the syntax of the VBA RESIZE property. First, we need to supply from which cell we need to resize by using the Range object. Then use excel VBA Resize property, and in this property, we need to supply row size limit and column size limit. Based on the provided row numbers and column numbers, it will resize it.


3 Answers

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.

like image 144
g3t_ Avatar answered Oct 19 '22 03:10

g3t_


How to select and extend a range from anywhere on a sheet, to anywhere on a sheet.

This is my first post. I know I'm a little bit late to the party, and it's obvious to me that most of the people here are far far more experienced and skilled than I am. So I doubt my solutions include much of their "big picture" nuanced considerations, but I've verified they work for me and I hope they work for all of you too.

Okay, so back to the question. Here is how I do it.

  • Example One
    To do this for the exact scenario posed by your question, if you’re starting at E18 and you want to extend the range to D16:F20, use the code below. As long as you have room for the full range, your active cell can actually be anywhere, and that range will follow it.

    Range(ActiveCell.Offset(-2, -1), ActiveCell.Offset(2, 1)).Select

  • Example Two
    If you’ve already selected a range, and then you want to expand it further (let’s say and additional 2 rows down and 1 column to the right), then do this:

    Range(Selection, Selection.Offset(2, 1)).Select

  • Example Three
    If you want to select a range of all the contiguous cells containing data, starting from the active cell and continuing down until it reaches a blank cell, and then also add the cells from 1 column to the left, then do this:

    Range(ActiveCell, Selection.End(xlDown).Offset(0, -1)).Select

like image 32
Greg Helm Avatar answered Oct 19 '22 02:10

Greg Helm


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.

like image 35
iDevlop Avatar answered Oct 19 '22 03:10

iDevlop