LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Resize array based on number of 'used' elements

One way:

Option Explicit
Sub testme()

'Sample passed as parameter
Dim sWordLists As String
Dim iCtr As Long
Dim wCtr As Long
Dim vUniqueList As Variant
Dim mySplit As Variant
Dim res As Variant

sWordLists = "Carrot%Carrot%Rabbit%3"
mySplit = Split(sWordLists, "%")

ReDim vUniqueList(LBound(mySplit) To UBound(mySplit))

wCtr = LBound(mySplit) - 1

For iCtr = LBound(mySplit) To UBound(mySplit)
res = Application.Match(mySplit(iCtr), vUniqueList, 0)
If IsError(res) Then
'not yet in vuniquelist
wCtr = wCtr + 1
vUniqueList(wCtr) = mySplit(iCtr)
End If
Next iCtr

ReDim Preserve vUniqueList(LBound(mySplit) To wCtr)

End Sub

You want another way?

John Walkenbach shows how to use a collection to get a unique list. He also
shows how that list can be sorted (you may want to do that):
http://j-walk.com/ss/excel/tips/tip47.htm



John wrote:

Hi there,

So here's my second question of the afternoon..................

I'm creating an array of unique words based on another list passed to the
procedure (see code below). This works ok but I'd like to resize the
resulting array (vUniqueList) to be the same number as the elements that are
not empty. So can anyone tell me if there's a way of doing this without
iterating through the array to test for empty elements?

Best regards

John

'--------------------------
'Sample passed as parameter
Dim sWordLists As String
sWordLists = "Carrot%Carrot%Rabbit"
'--------------------------

Dim itm As Variant
Dim vUniqueList As Variant

ReDim vUniqueList(UBound(Split(sWordLists, "%"))) As Variant

'Get unique list
For Each itm In Split(sWordLists, "%")
For i = 0 To UBound(vUniqueList)
If IsEmpty(vUniqueList(i)) = True Then
vUniqueList(i) = itm
Exit For
Else
If itm = vUniqueList(i) Then
Exit For
End If
End If
Next i
Next itm


--

Dave Peterson
 
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
Maximum number of elements in array Juarez Excel Discussion (Misc queries) 1 December 9th 08 07:49 PM
Counting the Number of Elements in an Array Confused_in_Houston[_2_] Excel Discussion (Misc queries) 3 October 1st 08 11:05 PM
Number of elements in an array Neal Zimm Excel Programming 4 August 12th 05 11:30 AM
How do I find the number of elements in an Array of Strings? BeefmanSteve Excel Programming 5 June 17th 05 12:48 AM
Counting the number of elements within an array that have data ExcelMonkey[_125_] Excel Programming 5 May 26th 04 05:14 PM


All times are GMT +1. The time now is 08:56 PM.

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

About Us

"It's about Microsoft Excel"