vba 使用 VB.net 使用“自动过滤器”对 Excel 中的数据进行排序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6604469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:36:40  来源:igfitidea点击:

Sort data in Excel using the 'Auto Filter' using VB.net?

vb.netexcel-vbaexcel-2007vbaexcel

提问by Yugal Jindle

I have AutoFilter in place in my sheet. I want to sort data using the AutoFilter itself and not by normal sort.

我的工作表中有自动过滤器。我想使用自动筛选器本身而不是正常排序对数据进行排序。

I want the client to see the down arrow on the autofilter button, which tells that rows are sorted on this key.

我希望客户端看到自动过滤按钮上的向下箭头,它告诉我们行是按这个键排序的。

Any idea how to do that ?

知道怎么做吗?

I have 'sheet' as an object :

我有“工作表”作为对象:

  sheet.Range("A2").AutoFilter then ??
  Or something else ??

Please help !

请帮忙 !

(Ensure the syntax for VB.Net and not VB Script)

(确保 VB.Net 的语法而不是 VB Script 的语法)

I am a newbie..

我是新手。。

回答by Reafidy

To filter a range A1:A7, try:

要过滤范围 A1:A7,请尝试:

Sheet.Range("$A:$A").AutoFilter(Field:=1, Criteria1:="MyFilter",         Operator:=XlAutoFilterOperator.xlFilterValues)

Make sure your are importing:

确保您正在导入:

Imports Microsoft.Office.Interop.Excel

Edit:

编辑:

sheet.AutoFilter.Sort.SortFields.Clear()
sheet.AutoFilter.Sort.SortFields.Add(Key:=sheet.Range("A1:A7"), SortOn:=XlSortOn.xlSortOnValues, Order:=XlSortOrder.xlAscending, DataOption:=xlSortDataOption.xlSortNormal)

With sheet.AutoFilter.Sort
    .Header = XlYesNoGuess.xlYes
    .MatchCase = False
    .Orientation = Constants.xlTopToBottom
    .SortMethod = XlSortMethod.xlPinYin
    .Apply()
End With