Adding variable string to array and then filtering on that.

RockandGrohl

Well-known Member
Joined
Aug 1, 2018
Messages
790
Office Version
  1. 365
Platform
  1. Windows
Hello.

I'd like to add an indeterminate amount of strings to an array, and then use that array as filter criteria on another sheet.

In order to find what to add to the array, I would be looking at the left(6) of column 1, and then the string on column 29 needs to be added.

Here's an example:

Column 1Column 29Added?
DONKEYA1Y
DONKEY-FA3Y
DONKEY-FA4Y
DONKEY-FA6Y
BADGERA5N
BADGER-FA2N

So if my current row was on DONKEY, I would see that left(6) would result in finding A1, A3, A4 and A6. So I want to add them to an array, then autofilter a range ("A1:BD" & LastrowDF) on field 56 with the CriteriaArray("A1", "A3" etc)

Is this possible? Can you add things to an array piece by piece? Cheers.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
This is where I'm at. Unfortunately, once the RegAr(q) cell value is added, and then q=q+1 happens, I get a "Subscript out of range" error.

VBA Code:
        tref = Left(Cells(ActiveCell.Row, "A").Value, 6)
        
        Dim RegAr() As Variant
        Dim cell As Range, rng As Range
        Dim q As Long
        Set rng = Range("AC3:AC" & LastrowPPL)
        
        On Error Resume Next
        ActiveSheet.ShowAllData
        On Error GoTo 0
        
        Range("A2:AG" & LastrowPPL).AutoFilter field:=1, Criteria1:=tref & "*"
        For Each cell In rng.SpecialCells(xlCellTypeVisible)
            ReDim Preserve RegAr(q)
            RegAr(q) = cell.Value
            q = q + 1
        Next cell

Any ideas? Thanks.
 
Upvote 0
How about
VBA Code:
Sub rockandGrohl()
   Dim Rws As Variant, Ary As Variant
   
   With Range("A2:AC" & Range("A" & Rows.Count).End(xlUp).Row)
      Rws = Filter(Evaluate(Replace("transpose(if(left(@,6)=""Donkey"",row(@)-min(row(@))+1,false))", "@", .Columns(1).Address)), False, False)
      Ary = Application.Index(.Value2, Rws, 29)
   End With
End Sub
 
Upvote 0
Solution
How about
VBA Code:
Sub rockandGrohl()
   Dim Rws As Variant, Ary As Variant
  
   With Range("A2:AC" & Range("A" & Rows.Count).End(xlUp).Row)
      Rws = Filter(Evaluate(Replace("transpose(if(left(@,6)=""Donkey"",row(@)-min(row(@))+1,false))", "@", .Columns(1).Address)), False, False)
      Ary = Application.Index(.Value2, Rws, 29)
   End With
End Sub

Hi Fluff,

I get Type Mismatch when I do this:

VBA Code:
            With Range("A2:AC" & Range("A" & Rows.Count).End(xlUp).Row)
               Rws = Filter(Evaluate(Replace("transpose(if(left(@,6)=""N05C18"",row(@)-min(row(@))+1,false))", "@", .Columns(1).Address)), False, False)
               Ary = Application.Index(.Value2, Rws, 29)
            End With

or this:

VBA Code:
            With Range("A2:AC" & Range("A" & Rows.Count).End(xlUp).Row)
               Rws = Filter(Evaluate(Replace("transpose(if(left(@,6)=" & tref & ",row(@)-min(row(@))+1,false))", "@", .Columns(1).Address)), False, False)
               Ary = Application.Index(.Value2, Rws, 29)
            End With

tref is the value of left-6 of the cell in column 1. Thanks.
 
Upvote 0
It needs to be
VBA Code:
      Rws = Filter(Evaluate(Replace("transpose(if(left(@,6)=""" & tref & """,row(@)-min(row(@))+1,false))", "@", .Columns(1).Address)), False, False)
 
Upvote 0
It needs to be
VBA Code:
      Rws = Filter(Evaluate(Replace("transpose(if(left(@,6)=""" & tref & """,row(@)-min(row(@))+1,false))", "@", .Columns(1).Address)), False, False)
Hi Fluff, so when I do this:

VBA Code:
            Dim ArRows As Variant, Ary As Variant
   
            With Range("A3:AC" & Range("A" & Rows.Count).End(xlUp).Row)
               ArRows = Filter(Evaluate(Replace("transpose(if(left(@,6)=""" & tref & """,row(@)-min(row(@))+1,false))", "@", .Columns(1).Address)), False, False)
               Ary = Application.Index(.Value2, ArRows, 29)
            End With

(Rws is already declared in another point in my code)

It appears that only the string on the current line is added to the array. When I switch to the other sheet to filter:

VBA Code:
REQ = Cells(ActiveCell.Row, "AB").Value
        
        op.Activate
        On Error Resume Next
        ActiveSheet.ShowAllData
        On Error GoTo 0
        LastrowDF = Cells(Rows.Count, "A").End(xlUp).Row
        Range("BD1").Value = "TourRef"
        
        Range("A1:BD" & LastrowDF).AutoFilter field:=18, Criteria1:=Array(Ary)
        Range("A1:BD" & LastrowDF).AutoFilter field:=56, Criteria1:=""

It only filters down to "A4", and not the expected 6 different strings (A1 through to A6)

Thanks.
 
Upvote 0
Try
VBA Code:
Range("A1:BD" & LastrowDF).AutoFilter 18, Ary, xlFilterValues
 
Upvote 0
Fantastic. You come in swinging with another magic Evaluate code and it solves a problem in just a couple lines of code, again. How do you do it?!
 
Upvote 0
PS, do you know why I was getting a Subscript out of range error on post number 2? It seems like a handy bit of code that I'd like to implement in other areas. Thanks.
 
Upvote 0

Forum statistics

Threads
1,215,200
Messages
6,123,611
Members
449,109
Latest member
Sebas8956

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