简体   繁体   中英

Excel Filter - VBA to automate

I have a requirement which needs me to select only the values that are greater than zero. It has got texts and numbers as well. Here is the sample data,

Name    Marks   Remarks
ab      90         Good
bc      0          Bad
cd     70          Average
de      0          Bad
fg    didn'tattend  Poor

and I want my output using VBA to be like this,

Name    Marks
ab      90
cd      70

and the output is on the different sheet as well. Kindly tell me which one is the best for this requirement.

I need a VBA SUB that takes these data and produce the above output. I am trying. But its not getting there.

The code that I tried,

Private Sub OpenExcelFile_Click()
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim Sheet As Worksheet
Dim PasteStart As Range

Set wb1 = ActiveWorkbook
Set PasteStart = [Control!A1]

Sheets("Section1").Select
    Cells.Select
    Selection.ClearContents

FileToOpen = Application.GetOpenFilename _
(Title:="Please choose the required file", _
FileFilter:="Report Files *.xls (*.xls),")

If FileToOpen = False Then
    MsgBox "No File Specified.", vbExclamation, "ERROR"
    Exit Sub
Else
    Set wb2 = Workbooks.Open(Filename:=FileToOpen)
    For Each Sheet In wb2.Sheets
        With Sheet.UsedRange
            .Copy PasteStart
            Set PasteStart = PasteStart.Offset(.Rows.Count)
        End With
    Next Sheet
End If
    wb2.Close

End Sub

On one hand it will be difficult that somebody writes the code for you, and on the other hand, which is good new, you can do this easily with the built-in Excel table. Just simply click on one of the cells of your data, go to Insert tab, choose Insert a Table and check mark my table has headers. Then you will see each column has a filtering icon, use the Marks column and go to Number filters and choose Greater than or Equal to 70 to get the result you showed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM