VBA - Match a value in 2D array with a starting index

  • Thread starter Thread starter Legacy 143009
  • Start date Start date
L

Legacy 143009

Guest
Hi,
I want to match a value in the second dimension of my array but starting from index 10. Is it possible? Something like:
VBA Code:
With Application
        Debug.Print .Match("searchString", Range(.Index(myArr, 10, 2), .Index(myArr, UBound(myArr), 2)), 0)
End With
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
It's unclear to me. Is it that you want to search the 10th row in your array? If so, try...

VBA Code:
With Application
        Debug.Print .Match("searchString", .Index(myArr, 10, 0), 0)
End With

Hope this helps!
 
Upvote 0
It's unclear to me. Is it that you want to search the 10th row in your array? If so, try...

VBA Code:
With Application
        Debug.Print .Match("searchString", .Index(myArr, 10, 0), 0)
End With

Hope this helps!
To be more precise, I want to search in the second column of my array from 10th row to last row.
 
Upvote 0
Try the following...

VBA Code:
Option Explicit

Sub test()

    Dim myArr As Variant
    myArr = Range("A1:C100").Value
    
    Dim mySlice As Variant
    mySlice = getColumnSliceFromArray(myArr, 10, UBound(myArr, 1), 2)
    
    Debug.Print Application.Match("z", mySlice, 0)
    
End Sub

Function getColumnSliceFromArray(ByRef arr As Variant, ByVal firstRow As Long, ByVal lastRow As Long, ByVal columnIndex As Long) As Variant
    getColumnSliceFromArray = Application.Index(arr, Evaluate("ROW(" & firstRow & ":" & lastRow & ")"), columnIndex)
End Function

Note that getColumnSliceFromArray() returns a one-column, two dimensional array. To return a one dimensional array, you can transpose the references returned by ROW in the Evaluate method...

Code:
Function getColumnSliceFromArray(ByRef arr As Variant, ByVal firstRow As Long, ByVal lastRow As Long, ByVal columnIndex As Long) As Variant
    getColumnSliceFromArray = Application.Index(arr, Evaluate("TRANSPOSE(ROW(" & firstRow & ":" & lastRow & "))"), columnIndex)
End Function

Hope this helps!
 
Upvote 0
Solution
Note that getColumnSliceFromArray() returns a one-column, two dimensional array. To return a one dimensional array, you can transpose the references returned by ROW in the Evaluate method...
Very clear. Thanks a lot 👍
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

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