View Single Post
  #9   Report Post  
Posted to microsoft.public.excel.programming
Graham Y Graham Y is offline
external usenet poster
 
Posts: 66
Default Why does my code randomly ignore variable initialisation?

Peter
I use x & y as coordinates for cell, it's just a shame Excel goes down first
(y,x) and not across like 'ordinary' maths (x,y) coordinates
the cmb's are 2 comboboxes that are on a form that is being initialised, It
sets up about 10 values for the user to choose from.
Thanks for the tip about getting the first row of a range.

I do have some global variables but y is not one of them.

And yes I really must start using longs for cell row counters.
"barnabel" wrote:

This assumes that the region always includes row 1. It is safer to use
Range("H3").CurrentRegion.Row+Range("H3").CurrentR egion.Rows.Count-1

Also, always use Longs for row indexes not Integers. You may not think
anything will ever be beyond 32767 rows but safe practices are better.

As to the original problem, what are cmbGCodes and cmbGrades? I don't see
how anything could be effecting the variable y. Is it possible you have a
global variable also called y that is an integer and the macro is picking up
that one since you are using it as an int? Try changing the variable y to
something more descriptive in this code fragment and see if that helps.

Peter Richardson

"Graham Y" wrote:

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.