How to filter a range of cells in VBA excel 2013

1

Hello everyone I am new to this and I am having problems in VBA Excel 2013. I need to filter a database on a sheet, according to values I have on another sheet of the same book, these values are in a range of cells. These values are constantly changing, but the range of the cells is not.

First I thought I could use it in the filter code where it says AutoFilter field: = 1 Criteria: = Sheet1.Range ("D4: D10") ... etc

But it does not work, then try declaring an array and using an advanced filter, but I can not make it work.

I need to filter from this BD1 that is on the "fuels" sheet

the values that appear in this BD2 on the "calculations" sheet

Someone to help me please !!!!

    
asked by albert fernandez 11.07.2017 в 06:09
source

1 answer

1

Refreshing a bit because I'm rusty in this VBA, one way is to convert your range into an array of values and then pass it to the filter

starting from this set of data and leaving everything very nailed (you can dynamize it later)

You can try something like the following:

Sub Macro1()

Dim rango As Range, celda As Range
Dim list() As String, lngCount As Long
Set rango = ActiveSheet.Range("J1:J2")


lngCount = 0
For Each celda In rango
    ReDim Preserve list(lngCount)
    list(lngCount) = celda.Text
    lngCount = lngCount + 1
Next

    ActiveSheet.Range("$A$1:$A$10").AutoFilter Field:=1, Criteria1:=list, Operator:=xlFilterValues
End Sub

In my case it was tested in Excel 2016 but I understand that it is the same

    
answered by 11.07.2017 в 07:17