Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array counts 1 less than supposed to

Status
Not open for further replies.

dstrat6

Programmer
Mar 24, 2008
29
US
Hi everyone, I have an array that is supposed to get 34 values but is only getting 33. I've tried everything that I can think of would anyone have any suggestions.

Code:
       For i = 1 to 34
          Print #FileNum, Cells(44 + i, 3)
       Next i
 
Try
Code:
For i = [red]0[/red] to [red]33[/red]
Your array is probably zero-based.
 





Hi,

"I have an array..."

WHERE? Your code did not refer to any array.

You can use the LBound and UBound functions to determine the index limits of an array, and never have to use a hard number in a loop.

Skip,

[glasses]Have you heard that the roundest knight at King Arthur's round table was...
Sir Cumference![tongue]
 
Hey Golom, I tried what you suggested but recieve the same result. Strange, I have another array thats pretty much the same that works fine. I'm puzzled.
 
Go with SkipVought's idea. Just before the loop insert
Code:
MsgBox LBound(Cells(1)) & " - " & UBound(Cells(1))
Which should tell you what the array bounds are.


 
The problem was my call procedure. I created a button called cmdRead that will print the values into the file and works fine, so I'll just rearrange my code. Thanks for the replies.
 
for reference:

Option base 1

For i = 1 to last_element


Option base 0

For i = 0 to last_element

Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 
Generally speaking, I am with Skip on this. It is better to use LBound and UBound and never have to bother with the specific (hard) count number.

For i = 0 To Ubound(array_name)

if zero-based.

As Skip points out, in the original code, there IS no array. They are explicit numbers.

faq219-2884

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top