Filter 2D array

1. Junior Member
Join Date
Dec 1969
Posts
10

## Filter 2D array

Hi all,<BR><BR>In http://www.4guysfromrolla.com/webtech/111300-1.shtml Bill Wilkinson provides a useful little function to create an array from a column in a 2D array. <BR><BR>I&#039;d like to be able to create a 2D array from another 2D array, in essence, to be able to Filter a 2D array on a value, just like the VBScript Filter Function does for a single dimension array.<BR><BR>Does anyone have such a function? I&#039;ve written one which kind of works, but I&#039;m not happy with it, see below code...<BR><BR>Thank you,<BR><BR>Anthony<BR><BR>Function Filter2DArray(the2DArray, theValue, theColumn)<BR>Dim intRowCounter, intColumnCounter, intCols, intRows<BR> Dim theNewArray(), intNewArrayRowCounter<BR> intCols = Ubound(the2DArray,1)<BR> intRows = Ubound(the2DArray,2) <BR> Redim theNewArray(intCols,intRows) &#039; too big<BR> For intRowCounter = 1 To intRows <BR> If Trim(the2DArray(theColumn, intRowCounter)) = Trim(theValue) Then<BR> intNewArrayRowCounter = intNewArrayRowCounter + 1<BR> For intColumnCounter = 0 To intCols<BR> theNewArray(intColumnCounter, intNewArrayRowCounter) = _<BR> the2DArray(intColumnCounter, intRowCounter)<BR> Next<BR> End If<BR> Next<BR><BR> Filter2DArray = theNewArray<BR>End Function<BR>

2. Junior Member
Join Date
Dec 1969
Posts
10

## RE: Filter 2D array

With a little more thought, I came up with this. It seems to work OK...<BR><BR>Function Filter2DArray(the2DArray, theValue, theColumn)<BR> Dim intRowCounter, intColumnCounter, intCols, intRows<BR> Dim theNewArray(), intNewArrayRowCounter<BR> intCols = Ubound(the2DArray,1)<BR> intRows = Ubound(the2DArray,2)<BR> intNewArrayRowCounter = -1 <BR> Redim theNewArray(intCols,intRows) <BR> For intRowCounter = 0 To intRows <BR> If Trim(the2DArray(theColumn, intRowCounter)) = Trim(theValue) Then<BR> intNewArrayRowCounter = intNewArrayRowCounter + 1<BR> For intColumnCounter = 0 To intCols<BR> theNewArray(intColumnCounter, intNewArrayRowCounter) = _<BR> the2DArray(intColumnCounter, intRowCounter)<BR> Next<BR> End If<BR> Next<BR> <BR> ReDim Preserve theNewArray(intCols, intNewArrayRowCounter+1)<BR><BR> Filter2DArray = theNewArray<BR>End Function

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•