Referencing an array of row numbers in a Filter formula.

HighAndWilder

Well-known Member
Joined
Nov 4, 2006
Messages
802
Office Version
  1. 365
Platform
  1. Windows
This code, just put together to demonstrate the issue, works fine but I want to reference the array of row numbers in the
fomula to just select those rows from the list of towns.

Please advise if there is an altogether better way.

Thanks.

VBA Code:
Public Sub subFilterByRow()
Dim arrRows(1 To 5) As Integer
Dim Q As String
Dim arrData() As Variant
Dim strFormula As String

  ActiveWorkbook.Save

  Q = Chr(34)
  
  arrRows(1) = 2
  arrRows(2) = 3
  arrRows(3) = 4
  arrRows(4) = 5
  arrRows(5) = 6
    
  ' I currently write the above array to a sheet and reference the range in the formula
  ' but I want to reference the array directly in the formula.
  Worksheets("Temp").Range("A1:A5").Value = Application.Transpose(arrRows)

  strFormula = "INDEX(LET(d,HSTACK(ROW(tblTowns),tblTowns[Town]),n,Temp!A1:A" & UBound(arrRows) & _
    ",FILTER(d,ISNUMBER(MATCH(INDEX(d,,1),n,0))," & Q & Q & ")),,2)"
  
  ' Populate an array with the results of the formula.
  arrData = Evaluate(strFormula)
  
End Sub

Sample Data.xlsm
A
12
23
34
45
56
6
Temp


Sample Data.xlsm
ABC
1Town
2Abingdon-on-Thames
3Accrington
4Acle
5Acton
6Adlington
7Alcester
8Aldeburgh
9Aldershot
10Alford
11Alfreton
12Alnwick
13Alsager
14Alston
15Alton
16Altrincham
17Amble
18Ambleside
19Amersham
20Amesbury
21Ampthill
22Andover
23Appleby-in-Westmorland
24
Towns
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
To show you

VBA Code:
Sub jec()
 Dim arrData, ar, arrRows(1 To 5) As Long
 ar = Sheet1.ListObjects("tblTowns").DataBodyRange
 
 arrRows(1) = 2
 arrRows(2) = 3
 arrRows(3) = 4
 arrRows(4) = 5
 arrRows(5) = 6
 
 arrData = Application.Index(ar, arrRows, 0)
 
End Sub
 
Upvote 0
Solution
To show you

VBA Code:
Sub jec()
 Dim arrData, ar, arrRows(1 To 5) As Long
 ar = Sheet1.ListObjects("tblTowns").DataBodyRange
 
 arrRows(1) = 2
 arrRows(2) = 3
 arrRows(3) = 4
 arrRows(4) = 5
 arrRows(5) = 6
 
 arrData = Application.Index(ar, arrRows, 0)
 
End Sub
Ah Ha, that's the way to do it.

My life will never be the same again now.

Thanks JEC.
 
Upvote 0
Ah Ha, that's the way to do it.
The marked solution has been changed accordingly. In your future questions, please mark the post as the solution that actually answered your question, instead of your feedback message as it will help future readers. No further action is required for this thread.
 
Upvote 0

Forum statistics

Threads
1,226,050
Messages
6,188,571
Members
453,484
Latest member
jlo1673

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