View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
IanKR IanKR is offline
external usenet poster
 
Posts: 97
Default Is there a way of asking "is a member of" in VBA?

Many thanks Jim - what you say makes absolute sense and you've explained it
very clearly. I just wish the VB books I've read had done the same! Perhaps
you should write a book yourself...

Some comments that I hope make sense...
In time critical code you should try alternative methods and time
them. Sometimes you can be surprised.

The lower bound is simply the starting row or column and
the upper bound is the ending row or column...
Arr( 2 to 10, 3 to 4) has rows numbered from 2 to 10 (9 rows) and
columns numbered from 3 to 4 (two columns).
Arr(2, 3) is the top left corner and Arr(10, 4) is the bottom right
corner.

A range can be considered as an array of values, for example ...
'Run this one and see what you get.
Sub RangeIsAnArray()
Dim rng As Excel.Range
Set rng = Range("A10:B55")
'(10, 2) the value in the 10th row / 2nd col of the range (not the
worksheet) MsgBox rng(10, 2)
End Sub

A Variant is a data type that can contain almost anything.
(it also requires more memory than any other data type)
So...
Dim Arr as Variant
When the range values are assigned to it ... Arr =
Range("A1:B50").Value
Arr ends up as a variant containing an array.
Arr(1, 2) holds the value (in this case) of cell B1.

To my simple way of thinking an Array is simply a memory structure
that allows you to store and retrieve data. In order to use it, you
have
to tell Excel that you want an array and what kind of data it will
hold...
Dim strArry() as String

Then you have to tell Excel how much room you need...
ReDim strArry(1 to 10, 1 to 1)
(you need 10 rows and 1 column)

'Then assign something to the first element of the array.
strArry(1, 1) = "Ian"
'Retrieve the value...
MsgBox strArry(1, 1)