Sometimes in a macro it is helpful to select cells relative to whichever cell is currently selected. For instance, let's say you want to select the first three cells of the current row. You can do that by using the following VBA code:
Range(Cells(Selection.Row, 1), Cells(Selection.Row, 3)).Select
The Cells property returns an object that represents a specific row and column (individual cell) of a worksheet. In this usage, Cells is used twice to determine a specific range of cells. The first instance returns the first cell of the current row, while the second returns the third cell of the current row. Thus, the range becomes the first through third cells of the current row.
Instead of using the Cells property to specify a location, you can use the Offset property to accomplish much of the same task. Consider the following code:
Range(ActiveCell.Offset(-3, 5), ActiveCell.Offset(0, 10)).Select
This uses the Offset property of the ActiveCell object to specify a range relative to the currently selected cell. The Offset property takes an argument that represents the row and column of the offset. A negative value represents up (for the row) and left (for the column). A positive value is down (for the row) and right (for the column). You can also use a value of 0, which represents the current row or column.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11402) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. You can find a version of this tip for the older menu interface of Excel here: Selecting a Range of Cells Relative to the Current Cell.
Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!
Want to use a worksheet function (such as SUM) from within a macro? Here's how easy it is to accomplish the task.
Discover MoreThe undo list can be a lifesaver when working in a macro. Unfortunately, the undo list is not preserved when you run a ...
Discover MoreOne common type of workbook used in offices is one that contains a single worksheet for each month of the year. If you ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2016-12-15 11:11:50
eric
In the meantime I succeeded to put together a solution. I suppose it's very clumsy, but it works perfectly :)
Range(ActiveCell, Cells.Find(What:="abc", After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)).Select
Selection.Delete Shift:=xlUp
ActiveCell.Offset(1, 0).Range("A1").Select
2016-12-12 16:37:55
eric
Excel 2016. I have to select a range relatively to the current cell. From the current cell up to the first cell containing the string "abc" (by columns). Then delete selection and moving cells up.
I am not so skilled with VBA, so I will appreciate a lot some guidance.
Thank you very much
2016-09-14 07:52:14
Willy Vanhaelen
@ jens jensen
Range(Cells(1, 1), Cells(32, 8)).Select
or
Range(Range("A1"), Range("H32")).Select
Both will do the job.
2016-09-13 19:58:28
jens jensen
I was making a print button for a sheet and it should always select 8 columns and 32 rows from the top left cell and print only that area.
I can't make it work
I'm sure it's easy but then I see baltthamossa2b saying that if my macro needt to select cells I'm doing something wrong.
...
2015-10-05 04:47:00
As a general rule, if your macro needs to select cells it means you are doing something wrong. Or you took a macro straight from the macro recorder and didn't optimize it.
That being said, Range.Offset is my preferred way of navigating around.
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2021 Sharon Parq Associates, Inc.
Comments