Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / September 2007

Tip: Looking for answers? Try searching our database.

Arrays [Why does this not work?]

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
AshofMind@gmail.com - 12 Sep 2007 22:14 GMT
Dim count ' counter
Dim people() As String 'array

  'find how many people listed in sheet 2 column A
   Sheets("Sheet2").Select
   Range("a1").Select
   Selection.CurrentRegion.Select
   row_count = Selection.Rows.count - 1 'Subtract header

Max = row_count 'array is this big
ReDim people(1 To Max) 'redim array

For count = 1 To Max
While Not IsEmpty(ActiveCell)
count = count + 1
ActiveCell.Offset(1, 0).Select
people(i) = ActiveCell.Value
Wend
Next count
Gary Keramidas - 12 Sep 2007 22:43 GMT
don't know what else you want to accomplish, but this may work for you:

Sub test()
Dim arr As Variant
Dim lastrow As Long
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
lastrow = ws.Cells(Rows.Count, "A").End(xlUp).Row
arr = ws.Range("A2:A" & lastrow)

MsgBox UBound(arr)
End Sub

Signature

Gary

> Dim count ' counter
> Dim people() As String 'array
[quoted text clipped - 15 lines]
> Wend
> Next count
AshofMind@gmail.com - 12 Sep 2007 22:58 GMT
Thank you for the help.  Thats running correctly now

I was attempting to create an array filled with a list of names( list
can get bigger or smaller)
Then I am going to be passing the names through an autofilter to parse
the list

I got the parsing section working but have had no luck on getting data
into an array

> don't know what else you want to accomplish, but this may work for you:
>
[quoted text clipped - 36 lines]
> > Wend
> > Next count
Dave Peterson - 12 Sep 2007 23:06 GMT
If you want a list of unique names, you could use data|filter|advanced (on
another sheet???).

Or John Walkenbach shows how to loop through a range to get the unique values
here:
http://j-walk.com/ss/excel/tips/tip47.htm

> Thank you for the help.  Thats running correctly now
>
[quoted text clipped - 46 lines]
> > > Wend
> > > Next count

Signature

Dave Peterson

Gary Keramidas - 12 Sep 2007 23:30 GMT
wouldn't this variation of my original post  give a list of unique names?

Sub test()
Dim unique_names  As New Collection
Dim lastrow As Long
Dim ws As Worksheet
Dim i As Long
Set ws = Worksheets("Sheet1")
lastrow = ws.Cells(Rows.Count, "A").End(xlUp).Row
On Error Resume Next

For i = 2 To lastrow
unique_names.Add ws.Range("A" & i), CStr(ws.Range("A" & i))
Next

On Error GoTo 0
MsgBox unique_names.Count
End Sub

Signature

Gary

> If you want a list of unique names, you could use data|filter|advanced (on
> another sheet???).
[quoted text clipped - 53 lines]
>> > > Wend
>> > > Next count
Dave Peterson - 13 Sep 2007 02:18 GMT
Probably (I didn't test, but it looks very much like John Walkenbach's example
to me).

But you may not want to do this:
Dim unique_names As New Collection

Chip Pearson explains why:
http://www.cpearson.com/excel/variables.htm
Look for:
Don't Use The New Keyword In A Dim Statement

> wouldn't this variation of my original post  give a list of unique names?
>
[quoted text clipped - 80 lines]
> >
> > Dave Peterson

Signature

Dave Peterson

Gary Keramidas - 13 Sep 2007 02:36 GMT
so, is he saying to use something like this?

Dim unique_names  As Collection
Set unique_names = New Collection

Signature

Gary

> Probably (I didn't test, but it looks very much like John Walkenbach's example
> to me).
[quoted text clipped - 92 lines]
>> >
>> > Dave Peterson
Dave Peterson - 13 Sep 2007 02:41 GMT
Yep.

> so, is he saying to use something like this?
>
[quoted text clipped - 105 lines]
> >
> > Dave Peterson

Signature

Dave Peterson

Gary Keramidas - 13 Sep 2007 03:10 GMT
ok, thanks dave.

Signature

Gary

> Yep.
>
[quoted text clipped - 110 lines]
>> >
>> > Dave Peterson
Dave Peterson - 12 Sep 2007 22:59 GMT
You're looping within your "for/next" loop.

I wouldn't use a variable named count, either.  It looks too much like the
.count property.  It may not confuse excel, but it would confuse me:

Option Explicit
Sub testme()

   Dim myCount ' counter
   Dim people() As String 'array
   Dim Row_Count As Long
   Dim Max As Long
   Dim i As Long

  'find how many people listed in sheet 2 column A
   Sheets("Sheet2").Select
   Range("a1").Select
   Selection.CurrentRegion.Select
   Row_Count = Selection.Rows.count - 1 'Subtract header
   
   Max = Row_Count 'array is this big
   ReDim people(1 To Max) 'redim array
   i = 0
   For myCount = 1 To Max
       If IsEmpty(ActiveCell.Value) Then
           'skip it
       Else
           i = i + 1
           people(i) = ActiveCell.Value
       End If
       ActiveCell.Offset(1, 0).Select
   Next myCount
   
   If i = 0 Then
       MsgBox "no cells added"
   Else
       ReDim Preserve people(1 To i)
   End If

End Sub

> Dim count ' counter
> Dim people() As String 'array
[quoted text clipped - 15 lines]
> Wend
> Next count

Signature

Dave Peterson

AshofMind@gmail.com - 12 Sep 2007 23:31 GMT
Thank you so much thats exactly what I was attempting to do

> You're looping within your "for/next" loop.
>
[quoted text clipped - 62 lines]
>
> Dave Peterson
Mike Fogleman - 13 Sep 2007 00:03 GMT
This should do the trick:

Sub test()
Dim people() As String 'array
Dim Max As Long
Dim i As Integer

  'find how many people listed in sheet 2 column A
   'Sheets("Sheet1").Select
   'Range("a1").Select
   'Selection.CurrentRegion.Select
Max = Sheet1.Cells(Rows.count, 1).End(xlUp).Row - 1 'Subtract header
'array is this big
ReDim people(1 To Max) 'redim array
For i = 1 To Max
people(i) = Cells(i + 1, 1)
Next i

'this will put the array back in col C
For i = 1 To Max
   Range("C" & i + 1).Value = people(i)
Next
End Sub

Mike F
> Dim count ' counter
> Dim people() As String 'array
[quoted text clipped - 15 lines]
> Wend
> Next count
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.