Hi
Thanks for taking the time to look at this and any help will be appreciated.
I have a public function that works great, depending on a variable DO2T (do to Target ) it does 2 things
DO2T = 1 creates an array TARRAY from the variables specified TBOOK,TSHEET,TRANGE
DO2T = 2 creates an array TARRAY from the variables specified TBOOK,TSHEET,TRANGE, then searches throught the array for the TMATCH variable and redims TARRAY to only include the rows where a match is found.
This works great but i would like to adapt the code to give a third option where it searches through a specifed column in the array and redims TARRAY to only include the rows where a match is found. only slightly different i know (basicaly searching only one column instead of the whole array but returning same results) but will make a difference going forward to a userform i am creating.
Heres my code:
Again thanks for any input.
Thanks for taking the time to look at this and any help will be appreciated.
I have a public function that works great, depending on a variable DO2T (do to Target ) it does 2 things
DO2T = 1 creates an array TARRAY from the variables specified TBOOK,TSHEET,TRANGE
DO2T = 2 creates an array TARRAY from the variables specified TBOOK,TSHEET,TRANGE, then searches throught the array for the TMATCH variable and redims TARRAY to only include the rows where a match is found.
This works great but i would like to adapt the code to give a third option where it searches through a specifed column in the array and redims TARRAY to only include the rows where a match is found. only slightly different i know (basicaly searching only one column instead of the whole array but returning same results) but will make a difference going forward to a userform i am creating.
Heres my code:
Code:
Public Function create_array(TBook As String, TSheet As String, ByRef TRange As Range, TMatch As String, DO2T As Integer, ByRef TArray As Variant) As Variant
Dim matchArrIndex As Variant, splitArr As Variant
Dim i As Integer, j As Integer, outerindex As Integer, innerIndex As Integer, tempArrayIndex As Integer, CurrIndex As Integer, stringLength As Integer, MType As Variant
Dim increaseIndex As Boolean
Dim actualStr As String
Dim lastrow As String
Application.ScreenUpdating = False
Application.Workbooks(TBook).Sheets(TSheet).Activate
With ActiveSheet
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
Columns.EntireColumn.Hidden = False
Rows.EntireRow.Hidden = False
Set TRange = TRange.Resize(lastrow, TRange.Columns.Count)
On Error GoTo errorHandler
If DO2T = 1 Then 'create 2D array
TArray = TRange
Exit Function
ElseIf DO2T = 2 Then
TArray = TRange
actualStr = TMatch
'start index
i = LBound(TArray, 1)
'resize array for matched values
ReDim matchArrIndex(LBound(TArray, 1) To UBound(TArray, 1)) As Variant
'outer loop
For outerindex = LBound(TArray, 1) To UBound(TArray, 1)
'inner loop
For innerIndex = LBound(TArray, 2) To UBound(TArray, 2)
'if string matches with array elements
If TArray(outerindex, innerIndex) = actualStr Then
increaseIndex = True
matchArrIndex(i) = outerindex
End If
Next
If increaseIndex Then
tempArrayIndex = tempArrayIndex + 1
increaseIndex = False
i = i + 1
End If
Next
'if no matches found, exit the function
If tempArrayIndex = 0 Then
MsgBox ("No Match Found For ") & actualStr
Exit Function
End If
If LBound(TArray, 1) = 0 Then
tempArrayIndex = tempArrayIndex - 1
End If
'resize temp array
ReDim temparray(LBound(TArray, 1) To tempArrayIndex, LBound(TArray, 2) To UBound(TArray, 2)) As Variant
CurrIndex = LBound(TArray, 1)
j = LBound(matchArrIndex)
'store values in temp array
For i = CurrIndex To UBound(temparray)
For innerIndex = LBound(TArray, 2) To UBound(TArray, 2)
temparray(i, innerIndex) = TArray(matchArrIndex(j), innerIndex)
Next
j = j + 1
Next
TArray = temparray
Exit Function
errorHandler:
MsgBox "Error :" & Err.Description
End If
End With
matchArrIndex = "0"
splitArr = "0"
i = "0"
outerindex = "0"
innerIndex = "0"
tempArrayIndex = "0"
CurrIndex = "0"
stringLength = "0"
MType = "0"
increaseIndex = "0"
actualStr = ""
End Function
Again thanks for any input.