Store matching values in an array with VBA

Retroshift

Board Regular
Joined
Sep 20, 2016
Messages
119
Office Version
  1. 2019
Platform
  1. Windows
Hello,
Column A is filled with string values. Column B is sometimes filled with the value "x".
I would like to write VBA code that returns all the string values from column A into an array if they match the "x" value from column B.
Anyone knows how to do this?
Thanks
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Try...

VBA Code:
Sub test()

    Dim rng As Range
    Set rng = Range("A2:B" & Cells(Rows.Count, "A").End(xlUp).Row)
 
    Dim arr As Variant
    With Application
        arr = .Filter(rng.Columns(1), .Evaluate(rng.Columns(2).Address & "=""x"""))
    End With
 
    Dim i As Long
    For i = LBound(arr) To UBound(arr)
        Debug.Print arr(i, 1)
    Next i

End Sub

Alternatively, we can avoid the use of the Evaluate method and use XLookup instead...

VBA Code:
Sub test2()

    Dim rng As Range
    Set rng = Range("A2:B" & Cells(Rows.Count, "A").End(xlUp).Row)
 
    Dim arr As Variant
    With Application
          arr = .Filter(rng.Columns(1), .XLookup(rng.Columns(2), "x", True, False))
    End With
 
    Dim i As Long
    For i = LBound(arr) To UBound(arr)
        Debug.Print arr(i, 1)
    Next i

End Sub

Hope this helps!
 
Upvote 0
Hi Domenic. Unfortunately, the filter function is not available in my version of Excel (Office 2019). Are there any alternatives to your code?
 
Upvote 0
In that case, try the following instead...

VBA Code:
Sub test()

    Dim rng As Range
    Set rng = Range("A2:B" & Cells(Rows.Count, "A").End(xlUp).Row)
    
    Dim cnt As Long
    cnt = Application.CountIf(rng.Columns(2), "X")
    
    ReDim resultArray(1 To cnt)

    Dim i As Long
    Dim j As Long
    j = 1
    With rng
        For i = 1 To .Rows.Count
            If UCase(.Cells(i, 2).Value) = "X" Then
                resultArray(j) = .Cells(i, 1).Value
                j = j + 1
            End If
        Next i
    End With
    
    For i = LBound(resultArray) To UBound(resultArray)
        Debug.Print resultArray(i)
    Next i
    
End Sub

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

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