Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default whats wrong with my array?

i have these two arrays both in one of my subs in my
worksheet, why won't they load the values?
Basically, i want it to cycle through the range below for
each, pull the value, and stick them in each of the arrays.
any help would be much appreciated!
thanks guys,
Josh


Dim acell As Variant
Dim mynames(0 To 49)
Dim ncount As Integer
For Each acell In Range("a3:a50")
For ncount = 0 To 49
mynames(ncount) = acell.Value
Next ncount
Next


Dim bcell As Variant
Dim mystatus(0 To 49)
Dim scount As Integer
For Each bcell In Range("b3:b50")
For scount = 0 To 49
mystatus(scount) = bcell.Value
Next scount
Next

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default whats wrong with my array?

You are assigning all the values of each cell to each element of the array,
so you end up with the value of the last cell in each element.



Sub showArray()
Dim acell As Variant
Dim mynames(0 To 47)
Dim ncount As Integer
ncount = 0
For Each acell In Range("a3:a50")
mynames(ncount) = acell.Value
ncount = ncount + 1
Next

For i = 0 To UBound(mynames)
If i Mod 10 = 0 And i < 0 Then
sStr = sStr & mynames(i) & vbNewLine
Else
sStr = sStr & mynames(i) & ","
End If
Next
sStr = Left(sStr, Len(sStr) - 1)
MsgBox sStr
End Sub


or

Dim aCell as Range
Dim myNames as Variant
MyNames = Range("A3:A50").Value

Mynames no holds a two dimensional array

MyNames(1 to 48, 1 to 1

Sub showArray()
Dim aCell As Range
Dim myNames As Variant
myNames = Range("A3:A50").Value

For i = 1 To UBound(myNames)
If i Mod 10 = 0 Then
sStr = sStr & myNames(i, 1) & vbNewLine
Else
sStr = sStr & myNames(i, 1) & ","
End If
Next
sStr = Left(sStr, Len(sStr) - 1)
MsgBox sStr
End Sub


--
Regards,
Tom Ogilvy



"joshashcraft" wrote in message
...
i have these two arrays both in one of my subs in my
worksheet, why won't they load the values?
Basically, i want it to cycle through the range below for
each, pull the value, and stick them in each of the arrays.
any help would be much appreciated!
thanks guys,
Josh


Dim acell As Variant
Dim mynames(0 To 49)
Dim ncount As Integer
For Each acell In Range("a3:a50")
For ncount = 0 To 49
mynames(ncount) = acell.Value
Next ncount
Next


Dim bcell As Variant
Dim mystatus(0 To 49)
Dim scount As Integer
For Each bcell In Range("b3:b50")
For scount = 0 To 49
mystatus(scount) = bcell.Value
Next scount
Next



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 130
Default whats wrong with my array?

joshashcraft wrote:
i have these two arrays both in one of my subs in my
worksheet, why won't they load the values? [snip]


The first (minor) problem is that ncount should go from 0 to 47 because
there are only 48 values in A3:A50.

The major problem is that you load each element of mynames with the
value from a3, then replace each with the value from a4, etc. so that
you end up with mynames having all its elements equal to the value from
A50. Use

Dim acell As Variant
Dim mynames(0 To 47)
Dim ncount As Integer
For Each acell In Range("a3:a50")
mynames(ncount) = acell.Value
ncount = ncount + 1
Next acell

Alan Beban


Dim acell As Variant
Dim mynames(0 To 49)
Dim ncount As Integer
For Each acell In Range("a3:a50")
For ncount = 0 To 49
mynames(ncount) = acell.Value
Next ncount
Next


Dim bcell As Variant
Dim mystatus(0 To 49)
Dim scount As Integer
For Each bcell In Range("b3:b50")
For scount = 0 To 49
mystatus(scount) = bcell.Value
Next scount
Next


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Whats wrong with this array formula Carl_B Excel Worksheet Functions 7 February 16th 10 09:39 PM
Whats wrong with this function? Angelsnecropolis Excel Worksheet Functions 5 March 17th 09 12:22 AM
whats wrong with this? brownti via OfficeKB.com Excel Discussion (Misc queries) 10 February 12th 07 02:39 PM
Whats wrong with this? LucasBuck Excel Discussion (Misc queries) 3 January 12th 06 08:15 PM
Whats Wrong with this?? drvortex Excel Worksheet Functions 1 October 30th 04 06:48 PM


All times are GMT +1. The time now is 12:26 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"