Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / November 2007

Tip: Looking for answers? Try searching our database.

len of item in an array

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
JT - 29 Nov 2007 17:14 GMT
Is it possible to check the length of each item in a specific field in an
array? Is so, how?

I am building an array but I want to check the length of a specific field
for each item in the array.  For example, each "record" in my array has 4
fields.  I want to check the length of the first field in each "record".  If
it is greater than 0, I will do one action and if it is zero I will do
another action.  

I'm using this in an edit to be verify the user has not selected a record
where the first field is blank.  

Thanks for the help.

Signature

JT

Bernie Deitrick - 29 Nov 2007 17:51 GMT
JT,

Sub JTArrayTest()
Dim i As Integer
Dim j As Integer
Dim myArr(1 To 2, 1 To 4) As String
Dim NotEmpty As String

For i = LBound(myArr, 1) To UBound(myArr, 1)
  For j = LBound(myArr, 2) To UBound(myArr, 2)
     myArr(i, j) = "String " & i & " " & j
  Next j
Next i

myArr(1, 1) = ""

For i = LBound(myArr, 1) To UBound(myArr, 1)
  For j = LBound(myArr, 2) To UBound(myArr, 2)
     If Len(myArr(i, j)) = 0 Then
        MsgBox "array element " & i & " " & j & " is length 0"
     Else
        NotEmpty = NotEmpty & vbLf & i & " " & j
     End If
  Next j
Next i

MsgBox "Not Empty were:" & vbLf & NotEmpty

End Sub

HTH,
Bernie
MS Excel MVP

> Is it possible to check the length of each item in a specific field in an
> array? Is so, how?
[quoted text clipped - 9 lines]
>
> Thanks for the help.
Joel - 29 Nov 2007 17:57 GMT
See VBA help for UBound

UBound Function
     

Returns a Long containing the largest available subscript for the indicated
dimension of an array.

Syntax

UBound(arrayname[, dimension])

The UBound function syntax has these parts:

Part Description
arrayname Required. Name of the array variable; follows standard variable
naming conventions.
dimension Optional; Variant (Long). Whole number indicating which
dimension's upper bound is returned. Use 1 for the first dimension, 2 for the
second, and so on. If dimension is omitted, 1 is assumed.

Remarks

The UBound function is used with the LBound function to determine the size
of an array. Use the LBound function to find the lower limit of an array
dimension.

UBound returns the following values for an array with these dimensions:

Dim A(1 To 100, 0 To 3, -3 To 4)

Statement Return Value
UBound(A, 1) 100
UBound(A, 2) 3
UBound(A, 3) 4

> Is it possible to check the length of each item in a specific field in an
> array? Is so, how?
[quoted text clipped - 10 lines]
> Thanks for the help.
>  
Rick Rothstein (MVP - VB) - 29 Nov 2007 18:44 GMT
Assuming your field delimiter is a comma and that your array is named Record

   LenFirstField = Instr(Record(X), ",") - 1

where X is the loop counter (record number). If you need this to be more
general, here is how to

   LenOfField_F = Len(Split(Record(X), ",")(F - 1))

where F is 1 for the first field, 2 for the second, etc. and X is as defined
above.

Rick

> Is it possible to check the length of each item in a specific field in an
> array? Is so, how?
[quoted text clipped - 10 lines]
>
> Thanks for the help.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.