Need Help adapting array match function

pwwato

New Member
Joined
Jun 10, 2017
Messages
40
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:
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.:)
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

Forum statistics

Threads
1,223,912
Messages
6,175,340
Members
452,638
Latest member
Oluwabukunmi

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top