View Single Post
  #12   Report Post  
Posted to microsoft.public.excel.programming
Peter T Peter T is offline
external usenet poster
 
Posts: 5,600
Default Why does my code randomly ignore variable initialisation?

So has it 'happened' since, what did y debug to just beforehand.

Regards,
Peter T

"Graham Y" wrote in message
...
Thanks I'll try it, but it does happen seemingly randomly!
.[H3].CurrentRegion.Rows.count
gives me the last row number which is what I want. Rows 1 and 2 are

headings.

"Peter T" wrote:

Add a new line between 4 & 5
Debug.Print y

What's y just before it fails on the next line. If y=0 that would be

more
than strange!

In passing, do you need to populate from the entire region, if so I

assume
you need to loop like this
For y = 3 To .[H3].CurrentRegion.Rows.count + 3 - 1

FWIW, the square brackets to return the cell are fine for doing quick &
dirty stuff, but in real code better to do .Range("H3").etc

Regards,
Peter T

"Graham Y" wrote in message
...
Can anyone tell me why sometimes this code should fail on line 5 with

y=0
....
1 Private Sub PopulatecmbGrades()
2 Dim y As Single
3 With Sheets("Sheet5")
4 For y = 3 To .[H3].CurrentRegion.Rows.count
5 cmbGCodes.AddItem .Cells(y, 8)
6 cmbGrades.AddItem .Cells(y, 9)
7 Next
8 End With
9 cmbGCodes.ListIndex = 0
10 cmbGrades.ListIndex = 0
11 End Sub

.[H3].CurrentRegion.Rows.count = 13 currently

I do use y elsewhere and it may be 0 but a) it would be out of scope

here
and b) it should be set by the For loop.