1

I have a worksheet with 600 rows each representing a student. I have a User Form which displays one student at a time. The Next/Previous buttons on the form trigger a GetStudent Sub which takes one argument, the current row index. I don't want to work with all 600 rows at a time so I create a range with A1:Z10 syntax like this in the GetStudent Sub. This is done programmatically, but for clarity I've put a literal value in the code sample:

Dim ds As Range, w As Worksheet, currentRow As Range
Set w = ActiveWorkbook.Worksheets("students_sheet")
Set ds = w.Range("A4:Z5")

If I do

Debug.Print ds.Rows.Count

I get the expected 2, but when I pass an index to ds.Rows()

Set currentRow = ds.Rows(rowIndex)

I get the next row in the worksheet all the way through the 600 rows in the worksheet not only the 2 rows in the specified range. The range is not limited to the 2 rows indicated by the Rows.Count property. Can anyone help me figure out what I'm doing wrong?

1 Answer 1

2

The Rows property is relative to the range you call it on, and not bounded by the range itself.

So, when ds is set to A4:Z5 and you call ds.Rows(rowIndex) then you will get

rowIndex     Range
1            A4:Z4
10           A13:Z13
0            A3:Z3
-2           A1:Z1
-3           error
6
  • Thanks for your insight on that Chris but I think you'd agree we needed someone with over 20k reputation to figure out something so counter-intuitive. Can you point to something in the perfunctory documentation that would have made that clear to me? How did you know that?
    – DavidHyogo
    Jul 12, 2014 at 8:12
  • @DavidHyogo Maybe I have a warped mind, but to me it seems quite natural. Anyway, from Excel VBA help Rows Returns a Range object that represents the rows in the specified range. From MSDN, definition of Range Property Jul 12, 2014 at 8:23
  • 1
    There's nothing in the help pages you quoted that makes this obvious to me. There are 2 rows in the range I specified so it makes no sense that I can pass an index of 3 to get a 3rd row which shouldn't exist. If you change all this to arrays, you'll see where I'm coming from. Arrays have upper bounds and various languages tend to throw errors and exceptions if you even try to access a non-existent member above the upper bound. How would I go about getting a range in Excel that is bounded by the range itself? Convert it to an array?
    – DavidHyogo
    Jul 12, 2014 at 8:41
  • Moving the range data to a Variant array will produce an array of the size of the range, so this May suit your needs (and it's a useful thing to do for lots of other reasons too) eg Dim dsArr As Variant dsArr = ds.Value Jul 12, 2014 at 8:46
  • 1
    What I do is when I load the info from the sheet, I store the row number in one of the Tag properties of a control. So, if you display the student ID on the userform, say, in a label, then use the label.Tag property to store the row number.
    – Tinbendr
    Jul 12, 2014 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.