More efficient code using "with class" and .Cells, .Range, etc.? Excel, VBA

Posted on 2011-03-11
Last Modified: 2012-05-11
I'm trying to find more efficient methods for iterating through multiple row values in multiple workbooks. I think this is best illustrated through some code:

with data
  If .Cells(.Range("custom_name").Row + i, .Range("custom_name").Column) > Date then
    do something here
  end if
end with

Open in new window

Basically...I'm looking for a way to be able to start my "with" statement with data.Range and then back track into .Cells

...or find a different way to call a specific cell without the above .Cells(.Range1.Row, .Range2.Row) syntax, which is currently making my code extremely dense.
Question by:keats88
LVL 81

Accepted Solution

zorvek (Kevin Jones) earned 500 total points
ID: 35112305
Assuming data is a variable defined as a range object and containing a range object then you are good to go.

Here are some additional notes on referencing ranges (and cells) you may find useful:

There are many ways to specify a range in Visual Basic. Below are some examples.

   Range(Cells(Row1, Column1), Cells(Row2, Column2))
   Range(RangeVariable, RangeVariable)
   Cells(Row, Column)
   Cells(Row, "ColumnLetter") (Important: see note at the end of this article about a bug in Excel 2003 and earlier)
   RangeVariable.EntireRow.Cells(1, "Z") (Important: see note at the end of this article about a bug in Excel 2003 and earlier)
   RangeVariable.EntireColumn.Cells(10, 1)
   Columns("A") (Important: see note at the end of this article about a bug in Excel 2003 and earlier)
   Columns("A:B") (Important: see note at the end of this article about a bug in Excel 2003 and earlier)

When using a "ColumnLetter" parameter with the Cells or Columns property, the letter is translated into an absolute column number.

All of the above referencing methods except for the bracketed method ([A1]) can be used on any previously defined range object. When used in this manner, the row and column are treated as offsets into the range. They are also not limited to the parent range and thus can reference cells beyond the range's boundary. Below are some examples.

   [C1:E1].Cells(1, "D") equals [C1:E1].Cells(1, 4) or [F1]
   [C5].Columns("B") equals [C5].Columns(2) or [D5]
   [C5].Rows(4).Columns(2) equals [D8]

Note that the Cells method must be used when indexing into a range derived from the Rows or Columns collections, the EntireRow and EntireColumn objects, or when indexing into a range specified with square brackets:

   Columns("B")(23) will fail so use Columns("B").Cells(23)
   Rows(2)(1, 3) will fail so use Rows(2).Cells(1, 3)
   [B2].EntireRow(1, "C") will fail so use [B2].EntireRow.Cells(1, "C")
   [B10:B20](23) will fail so use [B10:B20].Cells(5)

If starting with a range that does not start in row 1 or column A and an absolute row or column is desired, use the EntireColumn or EntireRow method:

   [C5].EntireRow.Cells(1, "D") equals [D5]
   [C5].EntireRow.Columns("D") equals [D5]
   [C5].EntireColumn.Cells(10, 1) equals [C10]
   [C5].EntireColumn.Rows(10) equals [C10]

When passing range variables to the Range method the top left cell of the first parameter and the bottom right cell of the second parameter are used and the result is the smallest rectangular range encompasing both range parameters:

   Range([B2:C3], [D4:E5)] equates to B2:E5

Use commas to create unions:

   [RangeName1, RangeName2]

Note that the text passed to Range and in square brackets cannot be longer than 255 characters.

Use spaces to create intersections:

   [1:2 A:B] equates to A1:B2
To offset from a particular cell:

   [B2].Cells(2, 3) equates to [C4] and is the same as [B2].Offset(1, 2)

To double offset from a particular cell:

   [B2].Cells(2, 3)(4, 1) translates to [D6] and is the same as [B2].Offset(1, 2).Offset(3, 0)

To index into a range of cells starting from the top left cell and moving across each row from left to right and then down each row from top to bottom:

   [B2:D4].Cells(1) equates to [B2]
   [B2:D4].Cells(5) equates to [C3]

This technique works for any shape of range. Note that indexing continues down past the end of the specified range:

   [B2:D4].Cells(10) equates to [B5]

Top left cell of a range:

   Target(1, 1)

Bottom right cell of a range:

   Target(Target.Rows.Count, Target.Columns.Count)

Nth row or column of a range:


Working with all rows or columns in a range:

   For Each Row In Target.Rows
      Row(1, 1)
      Row(1, 2)
   Next Row

   For Each Column In Target.Columns
      Column(1, 1)
      Column(2, 1)
   Next Column

Important: Note that the Columns and Cells collections fail when passed a column letter and the active sheet is a chart or there is no visible workbook. This bug was resolved in Excel 2007. The workaround is to use the Range method. The problem occurs even if a specific parent expression is used which fully qualifies a worksheet. The following examples will fail when a chart sheet is active or there is no visible workbook:

   Set MyRange = ThisWorkbook.Worksheets(1).Columns("A")
   Set MyRange = ThisWorkbook.Worksheets(1).Cells(1, "A")
   Set MyRange = ThisWorkbook.Worksheets(1).Range("A1:C10").Columns("A")
   Set MyRange = ThisWorkbook.Worksheets(1).Range("A1:C10").Cells(1, "A")

These, however, will work:

   Set MyRange = ThisWorkbook.Worksheets(1).[A1:IV65536].Columns("A")
   Set MyRange = ThisWorkbook.Worksheets(1).[A1:IV65536].Cells(1, "A")
   Set MyRange = ThisWorkbook.Worksheets(1).[A1:IV65536].Range("A1:C10").Columns("A")
   Set MyRange = ThisWorkbook.Worksheets(1).[A1:IV65536].Range("A1:C10").Cells(1, "A")

As will these:

   Set MyRange = ThisWorkbook.Worksheets(1).Columns(1)
   Set MyRange = ThisWorkbook.Worksheets(1).Cells(1, 1)
   Set MyRange = ThisWorkbook.Worksheets(1).Range("A1:C10").Columns(1)
   Set MyRange = ThisWorkbook.Worksheets(1).Range("A1:C10").Cells(1, 1)

LVL 29

Expert Comment

ID: 35117352
One way of iterating thru a range without having to go thru heavy repettitive syntax is to do something like this

For each Cell in Data.Cells(Data.Range("custom_name").Row + i, Data.Range("custom_name").Column)

if Cell > Date then
 do something
Next Cell


Author Closing Comment

ID: 35158954
Great information, thank you!

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now