PC Review


Reply
Thread Tools Rate Thread

Determine if all items or elements are equal

 
 
Shawn
Guest
Posts: n/a
 
      17th Jul 2009
Does anyone have a good way in VBA to determine if every element in an array
is the same/equal? Or if each item in a range is the same/equal? ie. are
('one','one','one') equal? Yes. Or are ('one','one','two') equal? No.
 
Reply With Quote
 
 
 
 
Mike H
Guest
Posts: n/a
 
      17th Jul 2009
Hi,

You could read through the array and check

Sub Sonic()
Dim ctrlArray As Variant
ctrlArray = Application.Transpose(Range("a1:a10").Value)
For x = 1 To UBound(ctrlArray)
If ctrlArray(x) <> ctrlArray(WorksheetFunction.Min(UBound(ctrlArray), x +
1)) Then
MsgBox "Array elements aren't the same"
GoTo getmeout
End If
Next
getmeout:
End Sub

Mike

"Shawn" wrote:

> Does anyone have a good way in VBA to determine if every element in an array
> is the same/equal? Or if each item in a range is the same/equal? ie. are
> ('one','one','one') equal? Yes. Or are ('one','one','two') equal? No.

 
Reply With Quote
 
Dana DeLouis
Guest
Posts: n/a
 
      17th Jul 2009
Shawn wrote:
> Does anyone have a good way in VBA to determine if every element in an array
> is the same/equal? Or if each item in a range is the same/equal? ie. are
> ('one','one','one') equal? Yes. Or are ('one','one','two') equal? No.


Hi. One idea to test if A1:A10 are the same.

Sub Demo()
Dim B As Boolean
B = WorksheetFunction.CountIf([A1:A10], [A1]) = 10
End Sub

= = = = =
HTH :>)
Dana DeLouis
 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      18th Jul 2009
Another way IF your array is a String array (which your example seems to
indicate), this method will not work with a non-String array is like this...

If Replace(Join(YourArray, ""), YourArray(LBound(YourArray))) = "" Then

if you want case sensitive equality (one <> One); or like this...

If Replace(Join(YourArray, ""), YourArray(LBound( _
YourArray)), "", , , vbTextCompare) = "" Then

if you want case insensitive equality (one = One)

--
Rick (MVP - Excel)


"Shawn" <(E-Mail Removed)> wrote in message
news:AC11DF48-EBF6-45BB-AF45-(E-Mail Removed)...
> Does anyone have a good way in VBA to determine if every element in an
> array
> is the same/equal? Or if each item in a range is the same/equal? ie. are
> ('one','one','one') equal? Yes. Or are ('one','one','two') equal? No.


 
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
Checking if all elements in an array are equal jimmy Microsoft VB .NET 2 7th Apr 2007 03:10 PM
accessing dictionary elements less than or equal to key Bob Microsoft C# .NET 0 8th Feb 2007 06:13 PM
How do I determine which numbers in a list equal a given sum? =?Utf-8?B?aW5mcmF0ZXJyYQ==?= Microsoft Excel Misc 10 7th Oct 2005 11:39 PM
Determine which cells from a specific range equal a certain sum =?Utf-8?B?TWF4dGVyMjE=?= Microsoft Excel Worksheet Functions 1 20th Jul 2005 09:44 PM
How do I determine if the two rightmost elements of my string are ." Dave Microsoft VB .NET 6 14th Jan 2005 01:37 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:48 PM.