Someone please help me I am trying to loop through an array to get unique values to form another array.

15 views
Skip to first unread message

Denice Bollinger

unread,
Jun 18, 2012, 12:38:12 PM6/18/12
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I am very new to VB so please help me to loop through the array of
locations to get unique locations from the data table. I know it
seems strange but the data table contains the locations listed
multiple times. When I add the location to the location report I only
need the location listed once.


Thanks for your help in advance.

Here is my code so far:
Private Sub GetLocationData_Click()

' counts the number of items in the data sheet
Dim i As Integer
Dim row As Integer
Dim count As Integer
Dim loc() As String
Dim row2 As Integer
Dim row3 As Integer



row = 2

For i = 0 To count
Do While Sheets("Data").Cells(row, 4) <> ""

row = row + 1
Loop
count = row - 1
Next

ReDim Preserve loc(count) 'preserves the upperbound of the array



Dim stores() As String
Dim stCount As Integer
Dim uniquecnt As Integer
Dim match As Boolean
Dim strOne As String
Dim strTwo As String

row2 = 2
row3 = 5

' Assigns value to each element in the array
For Each Location In loc
store = Sheets("Data").Cells(row2, 4)
row2 = row2 + 1
Next Location

ReDim Preserve loc(count)

ReDim stores(0)


For stCount = LBound(loc()) To UBound(loc())
match = False

For uniquecnt = LBound(stores()) To UBound(stores())

If stores(uniquecnt) = loc(stCount) Then
match = True
End If
Next uniquecnt

If Not match Then
stores(uniquecnt) = loc(stCount)
ReDim Preserve stores(uniquecnt)
End If

Stephen Russell

unread,
Jun 19, 2012, 7:30:27 AM6/19/12
to dotnetde...@googlegroups.com
On Mon, Jun 18, 2012 at 11:38 AM, Denice Bollinger
<dboll...@newwavelink.com> wrote:
> I am very new to VB so please help me to loop through the array of
> locations to get unique locations from the data table.  I know it
> seems strange but the data table contains the locations listed
> multiple times. When I add the location to the location report I only
> need the location listed once.
---------------------------

Just ask the database again instead

Select distinct location from sameTable order by 1

Yes asking teh db is the slowest single process but not compared to
what you want to do.


--
Stephen Russell
Sr. Analyst
Ring Container Technology
Oakland TN

901.246-0159 cell

Denice Bollinger

unread,
Jun 19, 2012, 7:32:43 AM6/19/12
to dotnetde...@googlegroups.com
Can you give me an example .. Like I said I am new to this and still learning. 

Thanks 

Denice Bollinger
Revenue Controller
NewWave Link
Phone  (662)298-1268
Fax    (662) 429-8254
Cell    (901)497-9595

 dboll...@newwavelink.com





--
You received this message because you are subscribed to the Google
Groups "DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML
Web Services,.NET Remoting" group.
To post to this group, send email to dotnetde...@googlegroups.com
To unsubscribe from this group, send email to
dotnetdevelopm...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/dotnetdevelopment?hl=en?hl=en
or visit the group website at http://megasolutions.net

Stephen Russell

unread,
Jun 20, 2012, 7:09:28 AM6/20/12
to dotnetde...@googlegroups.com
On Tue, Jun 19, 2012 at 6:32 AM, Denice Bollinger <dboll...@newwavelink.com> wrote:
Can you give me an example .. Like I said I am new to this and still learning. 

------------------------------

How do you get data from the database now?  

Just make another call to the db and put it into another container for your list.

 

Denice Bollinger

unread,
Jun 20, 2012, 7:24:25 AM6/20/12
to dotnetde...@googlegroups.com
' counts the number of items in the data sheet
Dim i As Integer
Dim row As Integer
Dim count As Integer
Dim loc() As String
Dim row2 As Integer
Dim row3 As Integer



row = 2

For i = 0 To count
    Do While Sheets("Data").Cells(row, 4) <> ""
        
    row = row + 1
    Loop
    count = row - 1
Next

ReDim Preserve loc(count) 'preserves the upperbound of the array


Now I have the array with the upperbound. Now I need to loop through the array again and pull out only unique values and I am not sure how to loop through the array and form another array of only unique values.  I have tried several things and nothing has worked. 

I want to say that if a string exists in Loc array more than once then only input the value in store array once.  How do I do this?  I am so confused. 

Thanks in advance for your help.   



Denice Bollinger
Revenue Controller
NewWave Link
Phone  (662)298-1268
Fax    (662) 429-8254
Cell    (901)497-9595

 dboll...@newwavelink.com





--

Thava Rajan

unread,
Jun 20, 2012, 7:14:54 PM6/20/12
to dotnetde...@googlegroups.com
http://weblogs.asp.net/gunnarpeipman/archive/2008/05/15/getting-distinct-values-from-arrays.aspx 
just see this link


--
You received this message because you are subscribed to the Google
Groups "DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML
Web Services,.NET Remoting" group.
To post to this group, send email to dotnetde...@googlegroups.com
To unsubscribe from this group, send email to
dotnetdevelopm...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/dotnetdevelopment?hl=en?hl=en
or visit the group website at http://megasolutions.net



--
Everything is possible

Stephen Russell

unread,
Jun 21, 2012, 7:02:04 AM6/21/12
to dotnetde...@googlegroups.com
On Wed, Jun 20, 2012 at 6:24 AM, Denice Bollinger <dboll...@newwavelink.com> wrote:
' counts the number of items in the data sheet
Reply all
Reply to author
Forward
0 new messages