PC Review


Reply
Thread Tools Rate Thread

Arrays problem in new filtering code

 
 
Steve
Guest
Posts: n/a
 
      19th Jun 2009
I use Excel '03. There are two sheets with different projects we are working
on. Column E on both sheets have identical information; an alpha-numeric code
formated xxxxx-xxxxx. What I am trying to make happen is when I click on
Sheet 2, it filters column E to those codes that are currently being
displayed on Sheet 1 through the activate event. Sheet 1 has a seperate
column with project owners names on it that I use to filter the sheet by one
name at a time and look at the information for all of the owners projects.
One code per project.

I am trying to get all the codes from Sheet 1 to store into an array but my
loop exits when I get to the last cell in the range. I am trying to use a
dynamic array that uses range(e65536).end(xlup).address as the upper limit of
the array. Here is the code. Thanks for any help.

Dim CCLimit As Integer
Dim CCIndex() As String
Dim MyRange As Object
Dim EndPoint As Variant
Dim Count As Long, i As Long
Dim R As Object

CCLimit = GetLimit()
ReDim CCIndex(1 To CCLimit)

i = 1
Count = 6
EndPoint = Worksheets("Sheet1").Range("E65536").End(xlUp).Address
Set MyRange = Worksheets("Sheet1")
MyAddress = MyRange.Range("E" & Count).Address
Do
If Sheets("Sheet1").Rows(Count).Hidden = False Then
CCIndex(i) = Left(MyRange.Range("E" & Count).Value, 11)
MsgBox CCIndex(i)
i = i + 1
End If
Count = Count + 1
MyAddress = MyRange.Range("E" & Count).Address
Loop Until MyAddress = EndPoint

All I need to know at this point is how to get the array to work. I tried to
change Loop Until MyAddress = EndPoint
to
Loop Until MyAddress > EndPoint
but that did not work.
 
Reply With Quote
 
 
 
 
Rick Rothstein
Guest
Posts: n/a
 
      19th Jun 2009
Why are you using such an awkward loop structure. You know the limits you
want to iterate between, so why not just use a For..Next loop instead to
loop between them? Starting with your i=1 statement, here is some revised
code for you to consider...

i = 1
Set MyRange = Worksheets("Sheet1")
EndPoint = MyRange.Cells(MyRange.Rows.Count, "E").End(xlUp).Row
For Count = 6 To EndPoint
If Sheets("Sheet1").Rows(Count).Hidden = False Then
CCIndex(i) = Left(MyRange.Range("E" & Count).Value, 11)
i = i + 1
End If
Next

Note that I made a couple of changes (personal preference)... I changed your
EndPoint variable to hold the Row number rather than the address. I then use
that as the upper limit in the For..Next loop that follows (the lower limit
was set at 6 which is what your code showed your starting your count at). I
also changed your Range("E65536") reference to a Cells call and let VB
calculate the 65536 number via Rows.Count (again, personal preference... I
never can remember that number on my own<g>). Also, since you set the
worksheet in the MyRange variable (why that name... it is not a range, it is
a worksheet), I reversed the Set statement and EndPoint assignment statement
in order to take advantage of the Set variable (doing this allows me to use
MyRange instead of having Worksheets("Sheet1") repeated twice).

Anyway, the main point of the code it that I think the For..Next loop is
cleaner and easier to construct/maintain than the Do..Loop you originally
posted. Note that you might be able to eliminate the declaration for
MyAddress if you don't make use of it anywhere else in your code.

--
Rick (MVP - Excel)


"Steve" <(E-Mail Removed)> wrote in message
news:537FB7C5-AE5E-47F3-91D8-(E-Mail Removed)...
>I use Excel '03. There are two sheets with different projects we are
>working
> on. Column E on both sheets have identical information; an alpha-numeric
> code
> formated xxxxx-xxxxx. What I am trying to make happen is when I click on
> Sheet 2, it filters column E to those codes that are currently being
> displayed on Sheet 1 through the activate event. Sheet 1 has a seperate
> column with project owners names on it that I use to filter the sheet by
> one
> name at a time and look at the information for all of the owners projects.
> One code per project.
>
> I am trying to get all the codes from Sheet 1 to store into an array but
> my
> loop exits when I get to the last cell in the range. I am trying to use a
> dynamic array that uses range(e65536).end(xlup).address as the upper limit
> of
> the array. Here is the code. Thanks for any help.
>
> Dim CCLimit As Integer
> Dim CCIndex() As String
> Dim MyRange As Object
> Dim EndPoint As Variant
> Dim Count As Long, i As Long
> Dim R As Object
>
> CCLimit = GetLimit()
> ReDim CCIndex(1 To CCLimit)
>
> i = 1
> Count = 6
> EndPoint = Worksheets("Sheet1").Range("E65536").End(xlUp).Address
> Set MyRange = Worksheets("Sheet1")
> MyAddress = MyRange.Range("E" & Count).Address
> Do
> If Sheets("Sheet1").Rows(Count).Hidden = False Then
> CCIndex(i) = Left(MyRange.Range("E" & Count).Value, 11)
> MsgBox CCIndex(i)
> i = i + 1
> End If
> Count = Count + 1
> MyAddress = MyRange.Range("E" & Count).Address
> Loop Until MyAddress = EndPoint
>
> All I need to know at this point is how to get the array to work. I tried
> to
> change Loop Until MyAddress = EndPoint
> to
> Loop Until MyAddress > EndPoint
> but that did not work.


 
Reply With Quote
 
Steve
Guest
Posts: n/a
 
      19th Jun 2009
Thanks Rick. This is my first attempt at using an array. I have only been
programming spreadsheets for a while. I tried a few other ways around it but
things ran slow or did not work. I knew more or less what an array was, but I
wasn't sure the best way t use them. I know that Do...Loop looked funny, it
as mostly because I was more concerned with making my array work right. If
you noticed the GetLimit() function in there, that is another loop I used
that looks just as bad as the one you saw. All it does is allow me to ReDim
my array, I'm use you gathered that though. At one point both loops used For
loops, but then all sense left me as I tried to get that array to fill up the
way I wanted.
 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      19th Jun 2009
You can nest For..Next loops if needed. For example, consider this example
macro which fills in cells on the active sheet showing which loop variable
is active at which time through the loops...

Sub test()
Dim X As Long
Dim Y As Long
For X = 1 To 5
For Y = 100 To 110
Range("A1").Offset(X - 1, Y - 100).Value = "X = " & X & ", Y = " & Y
Next
Next
End Sub

--
Rick (MVP - Excel)


"Steve" <(E-Mail Removed)> wrote in message
news:B437D44A-3D2A-4AEA-9176-(E-Mail Removed)...
> Thanks Rick. This is my first attempt at using an array. I have only been
> programming spreadsheets for a while. I tried a few other ways around it
> but
> things ran slow or did not work. I knew more or less what an array was,
> but I
> wasn't sure the best way t use them. I know that Do...Loop looked funny,
> it
> as mostly because I was more concerned with making my array work right. If
> you noticed the GetLimit() function in there, that is another loop I used
> that looks just as bad as the one you saw. All it does is allow me to
> ReDim
> my array, I'm use you gathered that though. At one point both loops used
> For
> loops, but then all sense left me as I tried to get that array to fill up
> the
> way I wanted.


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Filtering arrays David Macdonald Microsoft Excel Programming 8 8th Sep 2009 08:37 PM
Jagged Arrays Problem - How to Assign Arrays to an Array Zigs Microsoft Excel Programming 3 11th Apr 2007 01:39 AM
How Populating Arrays/Range in VBA code robinson.william@gmail.com Microsoft Excel Programming 3 16th Nov 2006 09:19 PM
Passing arrays into unmanaged code Steve Baer Microsoft VC .NET 1 5th Jul 2004 06:39 PM
Re: passing arrays to non-managed code Nicholas Paldino [.NET/C# MVP] Microsoft C# .NET 1 4th May 2004 10:41 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:57 AM.