简体   繁体   English

Excel筛选器-VBA自动化

[英]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, 我希望我使用VBA的输出是这样的,

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. 我需要一个VBA SUB来接收这些数据并产生上面的输出。 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. 一方面,很难有人为您编写代码,另一方面,这是个很好的新功能,您可以使用内置的Excel表轻松完成此操作。 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. 然后,您将看到每列都有一个过滤图标,使用“标记”列并转到“数字”过滤器,然后选择“大于或等于70”以得到显示的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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