ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Arrey VAriable - How to initialise (https://www.excelbanter.com/excel-programming/449005-arrey-variable-how-initialise.html)

Madiya

Arrey VAriable - How to initialise
 
I am trying to use a arrey variable as I am not sure of the no of strings I will get back.
Here is my code sample.
Sub Macro1()
Dim p() As Integer
For i = 1 To 10
p(i) = i * 10 '<<<<<< ERROR LINE
Next
For i = 1 To 10
Debug.Print p(i)
Next
End Sub

I am getting subscript out of range error on line marked above.
However if I use Dim p(100) as integer, I do not get error.
As far as possible, I would like to use arrey without specifying any upper limit.

Pl help.

Regards,
Madiya

Claus Busch

Arrey VAriable - How to initialise
 
Hi Madiya,

Am Tue, 16 Jul 2013 03:32:03 -0700 (PDT) schrieb Madiya:

I am getting subscript out of range error on line marked above.
However if I use Dim p(100) as integer, I do not get error.
As far as possible, I would like to use arrey without specifying any upper limit.


an array starts with index 0.
You have to specify a upper limit. If you don't do it in declaration
part, you have to do it into the code part.
Try:

Sub Macro1()
Dim p() As Integer
Dim i As Integer
Dim myMax As Integer

myMax = 9
ReDim p(myMax)
For i = 0 To myMax
p(i) = (i + 1) * 10
Next
For i = LBound(p) To UBound(p)
Debug.Print p(i)
Next
End Sub


Regards
Claus B.
--
Win XP PRof SP2 / Vista Ultimate SP2
Office 2003 SP2 /2007 Ultimate SP2


All times are GMT +1. The time now is 02:49 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com