Auto VBA Filter - excel

Asked

Viewed 1,985 times

1

I need to make a filter with a certain value in a spreadsheet tab,after the filter is done, I need to copy, open a new document, and paste this statement.

I developed the code below but it’s not working.

follows the code

Sub filtrar()

Worksheets(Array("Planilha3", "Planilha2", "Planilha3",)).Copy

ActiveSheet.Range("Planilha3").AutoFilter Field:=Range("E1:E1048576").Column, Criteria1:="Cell 01"


With ActiveWorkbook

    ActiveWorkbook.SaveAs Filename:=fname1, FileFormat:=xlOpenXMLWorkbookMacroEnabled
    ThisWorkbook.Save
    ThisWorkbook.Close



End With
End Sub
  • You use Spreadsheet 3 twice and have a window at the end. So the function expects 4 Sheets. Worksheets(Array("Planilha3", "Planilha2", "Planilha3",)).Copy. The right thing would be Worksheets(Array("Planilha1", "Planilha2", "Planilha3")).Copy

  • @danieltakeshi thanks for the remark, but I was wrong to type

  • o Error shown is : Runtime Error 1004

  • In which line does the error occur? And I believe that ActiveSheet.Range("Planilha3").AutoFilter is wrong. You only want to filter column 3 of the new Datasheet for the data ="Cell 01"?

  • The error occurs in the Activesheet.Range line ("Planilla3"). Autofilter Field:=Range("E1:E1048576"). Column, Criteria1:="Cell 01"

  • and on the filter that’s right, I want to search in the tab ( sheet 3) in column 3 the data = "Cell 01'

Show 1 more comment

1 answer

1


Code

Sub filtrar()
    Dim ws3 As Worksheet
    Dim UltimaLinhaE As Long
    Worksheets(Array("Planilha1", "Planilha2", "Planilha3")).Copy
    Set ws3 = ActiveWorkbook.Worksheets("Planilha3")
    With ws3
        'Limpa os Autofiltros da Planilha para evitar erros
        If .FilterMode Then
            .ShowAllData
        End If
        'Última Linhada colunaE
        UltimaLinhaE = .Cells(.Rows.Count, "E").End(xlUp).Row
        'Autofiltro
        .Range("E1:E" & UltimaLinhaE).AutoFilter Field:=1, Criteria1:="Cell 01"
    End With

    With ActiveWorkbook
        ActiveWorkbook.SaveAs Filename:=fname1, FileFormat:=xlOpenXMLWorkbookMacroEnabled
        ThisWorkbook.Save
        ThisWorkbook.Close
    End With
End Sub

Explanation

The Autofilter Method was being misused.

The method is Range.Autofilter, so a range needs to be inserted.

Thus: .Range("E1:E" & UltimaLinhaE)

Obs.: The variable fname1 is not being specified in this code. And if what you want is to filter before uploading to the new Excel file, use .SpecialCells(xlCellTypeVisible) and create a new temporary sheet to be copied to the new file.

  • thanks for the help, however the filter is being made in the column a , where I change to filter the column and ?

  • How you are using the Range? .Range("E1:E" & UltimaLinhaE).AutoFilter Field:=1, Criteria1:="Cell 01" Because if all the columns in the worksheet are described, change the Field field to Field:=5

  • It worked, thanks @danieltakeshi

Browser other questions tagged

You are not signed in. Login or sign up in order to post.