ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   how do i fill an array? tia (https://www.excelbanter.com/excel-programming/373202-how-do-i-fill-array-tia.html)

JasonK[_2_]

how do i fill an array? tia
 
i know this is probably pretty basic but i can't find it in my dummies
book.

i have an array labeled M.
i've dimensioned it to 100 using the statement:

dim m(100)

i tried to run a for/next loop to fill it but it isn't working

for x = 1 to 100
m(x)=5
next x

this should fill array with 5's but it's giving me an error:
sub or function not defined.

thanks for your help
fred

Alan Beban

how do i fill an array? tia
 
The following works fine; don't know what's giving you an error.

Sub abtest3()
Dim m(100)
For x = 1 To 100
m(x) = 5
Next x
Debug.Print m(50) '<--Prints 5
End Sub

Alan Beban

JasonK wrote:
i know this is probably pretty basic but i can't find it in my dummies
book.

i have an array labeled M.
i've dimensioned it to 100 using the statement:

dim m(100)

i tried to run a for/next loop to fill it but it isn't working

for x = 1 to 100
m(x)=5
next x

this should fill array with 5's but it's giving me an error:
sub or function not defined.

thanks for your help
fred


Alan Beban

how do i fill an array? tia
 
Gary Keramidas wrote:
see if this will work

Sub test()
Dim arr As Variant
Dim x As Integer, n As Integer
ReDim arr(100)

For n = 1 To 100
ReDim Preserve arr(1 To n)'<--What's the point of this?
arr(n) = 5
Next n
End Sub


Alan Beban

NickHK

how do i fill an array? tia
 
I can see why you may get "Variable Not defined" (referring to "x"), if you
have Option Explicit set, as you should.
But not Sub/function not defined.
Unless there is other code that you have not shown.

Also, unless you have "Option Base 1" set, you are actually creating an
array with 101 elements and missing the first element in your loop.
And it is better to explicit declare your data type to avoid the default
Variant.

NickHK

"JasonK" wrote in message
...
i know this is probably pretty basic but i can't find it in my dummies
book.

i have an array labeled M.
i've dimensioned it to 100 using the statement:

dim m(100)

i tried to run a for/next loop to fill it but it isn't working

for x = 1 to 100
m(x)=5
next x

this should fill array with 5's but it's giving me an error:
sub or function not defined.

thanks for your help
fred




JasonK[_2_]

how do i fill an array? tia
 
Gary,
that worked great. i'm not sure why it worked, but it did.
thanks for your time.
fred








On Tue, 19 Sep 2006 12:45:06 -0400, "Gary Keramidas"
<GKeramidasATmsn.com wrote:

see if this will work

Sub test()
Dim arr As Variant
Dim x As Integer, n As Integer
ReDim arr(100)

For n = 1 To 100
ReDim Preserve arr(1 To n)
arr(n) = 5
Next n
End Sub



Gary Keramidas

how do i fill an array? tia
 
see if this will work

Sub test()
Dim arr As Variant
Dim x As Integer, n As Integer
ReDim arr(100)

For n = 1 To 100
ReDim Preserve arr(1 To n)
arr(n) = 5
Next n
End Sub

--


Gary


"JasonK" wrote in message
...
i know this is probably pretty basic but i can't find it in my dummies
book.

i have an array labeled M.
i've dimensioned it to 100 using the statement:

dim m(100)

i tried to run a for/next loop to fill it but it isn't working

for x = 1 to 100
m(x)=5
next x

this should fill array with 5's but it's giving me an error:
sub or function not defined.

thanks for your help
fred




Gary Keramidas

how do i fill an array? tia
 
you don't need to dim the x variable, i forgot to delete it

--


Gary


"JasonK" wrote in message
...
i know this is probably pretty basic but i can't find it in my dummies
book.

i have an array labeled M.
i've dimensioned it to 100 using the statement:

dim m(100)

i tried to run a for/next loop to fill it but it isn't working

for x = 1 to 100
m(x)=5
next x

this should fill array with 5's but it's giving me an error:
sub or function not defined.

thanks for your help
fred




Gary Keramidas

how do i fill an array? tia
 
sorry, i was playing around and forgot to remove that line

--


Gary


"Alan Beban" <unavailable wrote in message
...
Gary Keramidas wrote:
see if this will work

Sub test()
Dim arr As Variant
Dim x As Integer, n As Integer
ReDim arr(100)

For n = 1 To 100
ReDim Preserve arr(1 To n)'<--What's the point of this?
arr(n) = 5
Next n
End Sub


Alan Beban





All times are GMT +1. The time now is 01:49 AM.

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