Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 271
Default Determine if all items or elements are equal

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.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default Determine if all items or elements are equal

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.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 947
Default Determine if all items or elements are equal

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
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Determine if all items or elements are equal

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" wrote in message
...
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
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
Make sure all items in a Collection equal the same thing RyanH Excel Programming 3 September 24th 08 02:21 AM
Determine if item is in a range of items Mark Excel Programming 5 February 28th 08 01:50 PM
How do I determine which numbers in a list equal a given sum? infraterra Excel Discussion (Misc queries) 11 October 7th 05 11:39 PM
Determine which cells from a specific range equal a certain sum Maxter21 Excel Worksheet Functions 1 July 20th 05 09:44 PM
lower and upper case equal on spreadsheet but not equal in VB don Excel Programming 2 March 13th 05 12:04 AM


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

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

About Us

"It's about Microsoft Excel"