Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 37
Default Compare the number of elements in two different arrays

Guys,

I have two arrays, namely A and B - both have been dimensioned as follows
for unavoidable reasons:

Dim A(1 to 100) As Variant
Dim B(1 to 100) As Variant

The arrays have different number of elements in them i.e. A has 4 elements,
as in
A(1), A(2), A(3), A(4) and B has 2 elements, as in B(1), B(2). Note that
since I have declared both arrays from 1 to 100, A(5)..A(100) = "" and
likewise B(3)...B(100 = "" i.e. rest aray values are empty strings.

How do I compare the number of elements? i.e. since A has 4 non empty
elements and B has 2 non empty elements, I want to perform this check.

Any pointers are appreciated. Thanks.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Compare the number of elements in two different arrays

As long as your arrays are declared as either String or Variant, you can use
this function to count the number of elements with values assigned to them
(whether those elements are sequentially assigned or not)...

Function NumberOfUsedElements(ArrayIn As Variant) As Long
Dim Elements As String
If VarType(ArrayIn) = vbArray + vbString Or _
VarType(ArrayIn) = vbArray + vbVariant Then
Elements = Join(ArrayIn, Chr(1))
Do While InStr(Elements, Chr(1) & Chr(1))
Elements = Replace(Elements, Chr(1) & Chr(1), Chr(1))
Loop
If Left(Elements, 1) = Chr(1) Then Elements = Mid(Elements, 2)
If Right(Elements, 1) = Chr(1) Then Elements = Left(Elements, _
Len(Elements) - 1)
NumberOfUsedElements = UBound(Split(Elements, Chr(1))) + 1
End If
End Function

--
Rick (MVP - Excel)


"Varun" wrote in message
...
Guys,

I have two arrays, namely A and B - both have been dimensioned as follows
for unavoidable reasons:

Dim A(1 to 100) As Variant
Dim B(1 to 100) As Variant

The arrays have different number of elements in them i.e. A has 4
elements,
as in
A(1), A(2), A(3), A(4) and B has 2 elements, as in B(1), B(2). Note that
since I have declared both arrays from 1 to 100, A(5)..A(100) = "" and
likewise B(3)...B(100 = "" i.e. rest aray values are empty strings.

How do I compare the number of elements? i.e. since A has 4 non empty
elements and B has 2 non empty elements, I want to perform this check.

Any pointers are appreciated. Thanks.


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 225
Default Compare the number of elements in two different arrays

It depends on the purpose of your comparison...

Normally you write a loop which 'compares' corresponding elements...

In this case you can try something like
i = 1
While (A(i) < "" Or B(i) < "")
'...Compare
i = i + 1
Wend

"Varun" wrote:

Guys,

I have two arrays, namely A and B - both have been dimensioned as follows
for unavoidable reasons:

Dim A(1 to 100) As Variant
Dim B(1 to 100) As Variant

The arrays have different number of elements in them i.e. A has 4 elements,
as in
A(1), A(2), A(3), A(4) and B has 2 elements, as in B(1), B(2). Note that
since I have declared both arrays from 1 to 100, A(5)..A(100) = "" and
likewise B(3)...B(100 = "" i.e. rest aray values are empty strings.

How do I compare the number of elements? i.e. since A has 4 non empty
elements and B has 2 non empty elements, I want to perform this check.

Any pointers are appreciated. Thanks.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 37
Default Compare the number of elements in two different arrays

Thanks Guys.

I do not want to compare every element of the arrays. Instead, I want to
compare the "number" of elements in the two arrays.

How would I do that.

Thanks.

"Sheeloo" wrote:

It depends on the purpose of your comparison...

Normally you write a loop which 'compares' corresponding elements...

In this case you can try something like
i = 1
While (A(i) < "" Or B(i) < "")
'...Compare
i = i + 1
Wend

"Varun" wrote:

Guys,

I have two arrays, namely A and B - both have been dimensioned as follows
for unavoidable reasons:

Dim A(1 to 100) As Variant
Dim B(1 to 100) As Variant

The arrays have different number of elements in them i.e. A has 4 elements,
as in
A(1), A(2), A(3), A(4) and B has 2 elements, as in B(1), B(2). Note that
since I have declared both arrays from 1 to 100, A(5)..A(100) = "" and
likewise B(3)...B(100 = "" i.e. rest aray values are empty strings.

How do I compare the number of elements? i.e. since A has 4 non empty
elements and B has 2 non empty elements, I want to perform this check.

Any pointers are appreciated. Thanks.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Compare the number of elements in two different arrays

The function I gave you will return the number of elements in a String or
Variant array passed into it... if I understand what you want correctly,
just pass each array into it separately and add the results together...

TotalInUseElements = NumberOfUsedElements(A) + NumberOfUsedElements(B)

--
Rick (MVP - Excel)


"Varun" wrote in message
...
Thanks Guys.

I do not want to compare every element of the arrays. Instead, I want to
compare the "number" of elements in the two arrays.

How would I do that.

Thanks.

"Sheeloo" wrote:

It depends on the purpose of your comparison...

Normally you write a loop which 'compares' corresponding elements...

In this case you can try something like
i = 1
While (A(i) < "" Or B(i) < "")
'...Compare
i = i + 1
Wend

"Varun" wrote:

Guys,

I have two arrays, namely A and B - both have been dimensioned as
follows
for unavoidable reasons:

Dim A(1 to 100) As Variant
Dim B(1 to 100) As Variant

The arrays have different number of elements in them i.e. A has 4
elements,
as in
A(1), A(2), A(3), A(4) and B has 2 elements, as in B(1), B(2). Note
that
since I have declared both arrays from 1 to 100, A(5)..A(100) = "" and
likewise B(3)...B(100 = "" i.e. rest aray values are empty strings.

How do I compare the number of elements? i.e. since A has 4 non empty
elements and B has 2 non empty elements, I want to perform this check.

Any pointers are appreciated. Thanks.




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 225
Default Compare the number of elements in two different arrays

You can fine the total number of elements in the array by
noOfElements = UBound(A) - LBound(A)

Following will give you the count of elements < ""
i=0
For j = LBound(A) to UBound(A)
i = i + 1
Next

"Varun" wrote:

Thanks Guys.

I do not want to compare every element of the arrays. Instead, I want to
compare the "number" of elements in the two arrays.

How would I do that.

Thanks.

"Sheeloo" wrote:

It depends on the purpose of your comparison...

Normally you write a loop which 'compares' corresponding elements...

In this case you can try something like
i = 1
While (A(i) < "" Or B(i) < "")
'...Compare
i = i + 1
Wend

"Varun" wrote:

Guys,

I have two arrays, namely A and B - both have been dimensioned as follows
for unavoidable reasons:

Dim A(1 to 100) As Variant
Dim B(1 to 100) As Variant

The arrays have different number of elements in them i.e. A has 4 elements,
as in
A(1), A(2), A(3), A(4) and B has 2 elements, as in B(1), B(2). Note that
since I have declared both arrays from 1 to 100, A(5)..A(100) = "" and
likewise B(3)...B(100 = "" i.e. rest aray values are empty strings.

How do I compare the number of elements? i.e. since A has 4 non empty
elements and B has 2 non empty elements, I want to perform this check.

Any pointers are appreciated. Thanks.

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Compare the number of elements in two different arrays

If I were you I'd keep track of what was used when I added things to them.

Dim aCtr as long
dim bCtr as long
dim A(1 to 100) as variant
dim B(1 to 100) as variant

actr = 0
bctr = 0

'some kind of check to add elements to A
for each something in something.things
if something is true then
actr = actr + 1
a(actr) = something
end if
next something

'some kind of check to add elements to B
for each something in something.things
if something is true then
bctr = bctr + 1
b(actr) = something
end if
next something

if actr = 0 then
'nothing added to A
else
redim preserve A(1 to actr)
end if

if bctr = 0 then
'nothing added to B
else
redim preserve B(1 to bctr)
end if

if actr = bctr then
'same number of elements (may be 0, too!)
else
'different number
end if


Varun wrote:

Guys,

I have two arrays, namely A and B - both have been dimensioned as follows
for unavoidable reasons:

Dim A(1 to 100) As Variant
Dim B(1 to 100) As Variant

The arrays have different number of elements in them i.e. A has 4 elements,
as in
A(1), A(2), A(3), A(4) and B has 2 elements, as in B(1), B(2). Note that
since I have declared both arrays from 1 to 100, A(5)..A(100) = "" and
likewise B(3)...B(100 = "" i.e. rest aray values are empty strings.

How do I compare the number of elements? i.e. since A has 4 non empty
elements and B has 2 non empty elements, I want to perform this check.

Any pointers are appreciated. Thanks.


--

Dave Peterson
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 806
Default Compare the number of elements in two different arrays

Hello Dave,

Hmm, and if he often deletes random elements? Then your approach seems
to be dying with garbage collection (= rearranging elements), right?

Regards,
Bernd
  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 37
Default Compare the number of elements in two different arrays

Dave and All, thanks very much for the pointers. For now, I'll go with
Dave's trick. Thanks again.

"Bernd P" wrote:

Hello Dave,

Hmm, and if he often deletes random elements? Then your approach seems
to be dying with garbage collection (= rearranging elements), right?

Regards,
Bernd

  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Compare the number of elements in two different arrays

If elements are deleted, one could define a new array, reorganize into that and
erase the original.

But to be honest, I can't remember do that kind of thing as often as inspecting
some kind of list and extracting just the things I wanted to create my array.

Bernd P wrote:

Hello Dave,

Hmm, and if he often deletes random elements? Then your approach seems
to be dying with garbage collection (= rearranging elements), right?

Regards,
Bernd


--

Dave Peterson


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
Compare and count if data elements on two worksheets are similar aascasacspas Excel Worksheet Functions 1 May 21st 09 04:59 AM
Compare and Update elements from Sheet1 with Sheet2 [email protected] New Users to Excel 3 November 27th 07 04:27 PM
counting elements of two arrays Geoff[_14_] Excel Programming 6 August 24th 07 09:15 AM
Elements and Arrays in Excel Lighthouseman Excel Worksheet Functions 6 February 1st 06 10:06 AM
comparing elements from 2 arrays using VBA code lopsided Excel Programming 3 September 23rd 03 04:34 PM


All times are GMT +1. The time now is 09:20 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"