Very nice – just one small observation I would make:
When assigning a range to a variable, it is best to specify the sheet (and ideally, the workbook) that the range belongs to rather than relying on the default activesheet. That way you can be sure of exactly what you are dealing with.
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
Calculating Range References (Excel (All))
Home » Forums » AskWoody support » Productivity software by function » MS Excel and spreadsheet help » Calculating Range References (Excel (All))
- This topic has 6 replies, 3 voices, and was last updated 17 years, 4 months ago.
AuthorTopicWSrory
AskWoody LoungerDecember 19, 2007 at 5:01 pm #447131Viewing 1 reply threadAuthorReplies-
WSRudi
AskWoody Lounger -
WSRudi
AskWoody LoungerDecember 24, 2007 at 1:08 am #1089039(Edited by Rudi on 24-Dec-07 02:08. Added some more threads with additional code to select ranges.)
Calculating Range References
One of the most valuable techniques in Excel programming is the ability to calculate a range reference, store it in a declared variable and then use that reference to complete tasks in the worksheet. Think of setting a range reference as the “hard work” done at the beginning of the macro, and once set, you can do anything with that reference. If the set range reference is calculated in such a way to be volatile, then it really does make a flexible and dynamic macro, irrespective of what it is programmed to do on the worksheet.
In this post I have compiled a variety of ways to accurately calculate the required range reference using range properties or functions to define the reference. The details below are to serve as examples that you can use or modify to suite your requirements.
For ALL examples below you need to set up a declared variable(s) by “DIM’ing” it as a range object. After that you SET the variable(s) with the appropriately calculated reference. The most basic example I can provide is as follows:
Dim myRange as Range
Set myRange = Range(“A1”)The above example creates a reference to Range A1 and populates the variable called myRange, (declared as a range type variable), with that reference. “myRange” can now be used anywhere within code to refer to Range A1. But this is just the beginning. The range object can be used to calculate a myriad of useful references to specific or volatile areas on your worksheet using properties or functions.
Below follow more examples for your reference. Once you have the range reference set, you can select the range using the statement : myRange.Select in each case below
Using CurrentRegion
Set myRange = Range(“A1”).CurrentRegion
CurrentRegion is a property of the range object that extents the selection (from the initial reference) into all directions to create a solid selection encompassing a list of information block of various sizes.Using Resize
Set myRange = Range(“A1”).CurrentRegion.Resize(1)
Resize resizes the selection to become only one row in height. This is perfect for selecting all the column headings of a list of any number of columns in width.Set myRange = Range(“A1”).CurrentRegion.Resize( ,1)
Resizes the selection to become only one column in width. This is perfect for selecting all the column headings of a list of any number of columns in width.Set myRange = Range(“A1”).CurrentRegion.Resize(Range(“A1”).CurrentRegion.Rows.Count+1 , Range(“A1”).CurrentRegion.Columns.Count+1)
Selects the entire list including an extra row and column below and to the right.Using Offset
Set myRange = Range(“A1”).CurrentRegion.Offset(1,1)
Unlike Resize, which actually resizes the selection to a new dimension, Offset moves the selection up, down, left or right. The example above selects the entire list and offsets the selection one row down and one column to the right. It theirfore excludes the first row and first column.Using Offset and Resize
Set myRange = Range(“A1”).CurrentRegion.Offset(1,1).Resize(Range(“A1”).CurrentRegion.Rows.Count-1 , Range(“A1”).CurrentRegion.Columns.Count-1)
The example above selects the entire list and offsets the selection one row down and one column to the right. Then Resizes the selection to exclude the extra blank row and column resulting from the offset.Set myRange = Range(“A1”).CurrentRegion.Offset(Range(“A1”).CurrentRegion.Rows.Count,0).Resize(1)
The example above selects only the row directly below the list. A perfect position to insert totals if required.Using the Union Function
Set myTitles = Range(“A1”).CurrentRegion.Resize(1)
Set myTotals = Range(“A1”).CurrentRegion.Offset(Range(“A1”).CurrentRegion.Rows.Count,0).Resize(1)
Set myRange = Union(myTitles,myTotals)
You can use the Union Function to select multiple ranges in one go. Union has 30 arguments allowing you to select up to 30 selections using one variable. The above example selects the Titles and the Totals rows.Using the Intersect Function
Set myRng1 = Columns(“D”).EntireColumn
Set myRng2 = Rows(10).EntireRow
Set myRange = Intersect(myRng1,myRng2)
The Intersect Function selects the cells that represent the rectangular intersection of two or more ranges. The function has up to 30 arguments that (if used) will select only cells that have been intersected by a row and column.Set myRng1 = Range(“A1”).CurrentRegion.Offset(1, 0)
Set myRng2 = Range(“A1”).CurrentRegion.Offset(0, 1)
Set myRange = Intersect(myRng1, myRng2)
This example selects the body info of a data block. In other words, it selects the entire data block excluding the first row and first column. Another version of an example above.Conditional Selection
I tracked down a question I posted some while back that shows how to invert a selection. The example is rather specific to duplicate entries, but can be modified to suite requirements you may have. See post 485,599 for details.Inverting the Selection within a range
Hans has graciously provided some handy code to invert the selection based on current selected cells. This takes range calculation to the extreme (in my opinion). For this very useful code, see post 684,556 for details.Using the Find Command
Set myRange = Cells.Find(What:=”Type Search Value Here”, After:=Cells(1), _
LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
myRange.Select
Using the Find command can help find a specific entry on the worksheet. Once that unique entry is located, a reference to that cell is created and then you can use the above techniques to extend the selection with CurrentRegion, Offset, Resize, etc…If you find these techniques to select ranges useful, I can recommend ASAP Utilities. As quotes from the site : ASAP Utilities is a powerful Excel add-in that fills the gaps in Excel and automates frequently used tasks. Since 1999 it has grown to become probably one of the world’s most popular add-ins for MS Excel. This add-in provides all the above options for selecting ranges, and MORE, all compiled into a tool that neatly integrates into Excel in a custom menu. Selecting specific areas in the worksheet is now just a menu command away. See the site and the downloads here.
-
WSHansV
AskWoody LoungerDecember 19, 2007 at 5:39 pm #1089056Thanks Rudi, this will be useful to many readers.
Two other remarks:
1) An instruction such as
Set myRange = Range(“A1”).CurrentRegion.Offset(1,1).Resize(Range(“A1”).CurrentRegion.Rows.Count-1 , Range(“A1”).CurrentRegion.Columns.Count-1)
is rather long. You can split it into two instructions:
Set myRange = Range(“A1”).CurrentRegion
Set myRange = myRange.Offset(1,1).Resize(myRange.Rows.Count-1 , myRange.Columns.Count-1)to improve readability.
2) (This is for others reading this, I know that you know it)
Your post uses “smart quotes”, for example in Range(“A1”). VBA doesn’t understand those, you should always use straight quotes: Range(“A1”).
-
Viewing 1 reply thread -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
A CVE-MITRE-CISA-CNA Extravaganza
by
Nibbled To Death By Ducks
1 hour, 34 minutes ago -
Sometimes I wonder about these bots
by
Susan Bradley
8 hours, 27 minutes ago -
Does windows update component store “self heal”?
by
Mike Cross
14 hours, 31 minutes ago -
Windows 11 Insider Preview build 27858 released to Canary
by
joep517
15 hours, 31 minutes ago -
Pwn2Own Berlin 2025: Day One Results
by
Alex5723
14 hours, 56 minutes ago -
Windows 10 might repeatedly display the BitLocker recovery screen at startup
by
Susan Bradley
11 hours, 26 minutes ago -
Windows 11 Insider Preview Build 22631.5409 (23H2) released to Release Preview
by
joep517
18 hours, 13 minutes ago -
Windows 10 Build 19045.5912 (22H2) to Release Preview Channel
by
joep517
18 hours, 14 minutes ago -
Kevin Beaumont on Microsoft Recall
by
Susan Bradley
6 hours, 48 minutes ago -
The Surface Laptop Studio 2 is no longer being manufactured
by
Alex5723
1 day, 2 hours ago -
0Patch, where to begin
by
cassel23
20 hours, 24 minutes ago -
CFPB Quietly Kills Rule to Shield Americans From Data Brokers
by
Alex5723
1 day, 16 hours ago -
89 million Steam account details just got leaked,
by
Alex5723
1 day, 3 hours ago -
KB5058405: Linux – Windows dual boot SBAT bug, resolved with May 2025 update
by
Alex5723
2 days ago -
A Validation (were one needed) of Prudent Patching
by
Nibbled To Death By Ducks
1 day, 15 hours ago -
Master Patch Listing for May 13, 2025
by
Susan Bradley
1 day, 2 hours ago -
Installer program can’t read my registry
by
Peobody
9 hours, 19 minutes ago -
How to keep Outlook (new) in off position for Windows 11
by
EspressoWillie
1 day, 13 hours ago -
Intel : CVE-2024-45332, CVE-2024-43420, CVE-2025-20623
by
Alex5723
1 day, 20 hours ago -
False error message from eMClient
by
WSSebastian42
2 days, 11 hours ago -
Awoke to a rebooted Mac (crashed?)
by
rebop2020
2 days, 20 hours ago -
Office 2021 Perpetual for Mac
by
rebop2020
2 days, 21 hours ago -
AutoSave is for Microsoft, not for you
by
Will Fastie
32 minutes ago -
Difface : Reconstruction of 3D Human Facial Images from DNA Sequence
by
Alex5723
3 days, 1 hour ago -
Seven things we learned from WhatsApp vs. NSO Group spyware lawsuit
by
Alex5723
8 hours, 55 minutes ago -
Outdated Laptop
by
jdamkeene
3 days, 6 hours ago -
Updating Keepass2Android
by
CBFPD-Chief115
3 days, 12 hours ago -
Another big Microsoft layoff
by
Charlie
3 days, 12 hours ago -
PowerShell to detect NPU – Testers Needed
by
RetiredGeek
14 hours, 8 minutes ago -
May 2025 updates are out
by
Susan Bradley
15 hours, 50 minutes ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.