[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 152
  • Last Modified:

Array Dimensions ?

From this example below I know that if I put in the subscripts 4,15 into this array that it returns a value of 1.

txtFields_Array(4, 15) = 1

The question is whether I can put in the value :

txtFields_Array (Subscript1,Subscript2) =  1
without the subscripts......

and return the subscript values 4 and 15 ?

Is this possible? I guess I am thinking in math terms and matrices.

  • 2
1 Solution
You can use a function like this...

  Function FindValue(x() As Long, Value as Long, a As Long, b As Long) As Boolean

    For a = LBound(x, 1) To UBound(x, 1)
      For b = LBound(x, 2) To UBound(x, 2)
        If (x(a, b) = Value) Then
          FindValue = True
          Exit Function
        End If

  End Function

You use it with code like...

  Dim Subscript1 As Long
  Dim Subscript2 As Long

  If FindValue(txtFields_Array, 1, Subscript1, Subscript2) Then
    Msgbox "Value found at position " & Subscript1 & "," & Subscript2
  End If
You will have to change some definitions depending on what kind of array you have, for example if it's a array with integers, just replace 'Value As Long' To 'Value As Integer', and 'x() As Long' to x() As Integer'.
Green Boy --
Are you trying to determine which element in a two-dimensional array is equal to 1?

txtFields_Array(Subscript1,Subscript2) = 1
won't do it.

You'll need to loop through your array dimensions to determine this, and this gets more complicated if you have more than one element in your array equal to 1.

Dim intCount1 As Integer
Dim intCount2 As Integer
Dim intResults as Integer
Dim intSub1() as Integer
Dim intSub2() as Integer

Redim intSub1(1)
Redim intSub2(1)
IntResults = 0
For intCount1 = lbound(txtFields_Array,1) to Ubound(txtFields_Array,1)
    For intCount2 = Lbound(txtFields_Array,2) to Ubound(txtFields_Array,2)
        If txtFields_Array(intCount1, intCount2) = 1 then
            IntResults = intResults + 1
            IntSub1(intResults) = intCount1
            IntSub2(intResults) = intCount2
            Redim Preserve intSub1(intResults + 1)
            Redim Preserve intSub2(intResults + 1)
        End If
    Next intCount2
Next intCount1

Rem The above code will take the positions of each instance of 1 in your matrix
Rem and put that position in an array.

For intCount1 = 1 to intResults
    Me.Print intSub1(intCount1) & ", " & intSub2(intCount1)
Next intCount1

Rem The above code prints each ordered pair on your form.

Note: This code leaves the intSub1() and intSub2() arrays larger than they should be -- there are empty elements at intSub1(0), intSub2(0), intSub1(UBound(intSub1)), and intSub2(UBound(intSub2)), which you can clean up after the fact.

You should also check before the last part of the code that intResults isn't zero, which would indicate that "1" wasn't found anywhere in your matrix.

If you can be sure "1" won't be repeated anywhere in your array, or if you want to just fine one instance, this is overkill.
Green_BoyAuthor Commented:
Thanks Barry ......

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now