如何添加过滤器&单元格中的排序按钮?

时间:2015-10-08 07:03:38

标签: excel excel-2013 dropdown

如何在图片中插入过滤器和排序按钮?

Image

2 个答案:

答案 0 :(得分:3)

或更好,点击已填写的单元格,然后点击" Home">>"格式化为表格"。它为您提供了许多关于过滤,排序和配方复制以及一些有趣设计的选项。

Format as Table screenshot

答案 1 :(得分:0)

以下是一些代码,其中附有乔治使用表格的答案。

当过滤器正常工作时,我立即遇到了添加表的麻烦。您不能将“查询”范围转换为表,但是可以添加过滤器。请参阅-> https://stackoverflow.com/a/40492129/5079799和免责声明这是我的博客,我将在其中发布更新的代码https://www.freesoftwareservers.com/display/FREES/Filter+Worksheet+-+Table+-+Query+-+Excel+VBA

Public Sub DatatoTable(ws As Worksheet)
   
    Dim TblStyle As String
    TblStyle = "TableStyleMedium20"
    
    Dim TblName As String
    TblName = ws.Name
     
    Dim TblRng As Range
    Set TblRng = GetUsedRange(ws)
    
    ws.Activate
    TblRng.Select
    Set lo = TblRng.ListObject
    If Not lo Is Nothing Then
         Debug.Print "Table Found"
    Else
     ws.ListObjects.Add(xlSrcRange, TblRng, , xlYes).Name = TblName
     Range(TblName & "[#All]").Select
     ws.ListObjects(TblName).TableStyle = TblStyle
    End If
End Sub
Public Function GetUsedRange(ws As Worksheet) As Range
 Dim lRow As Integer, lCol As Integer, fCol As Integer, fRow As Integer
 fCol = Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlLeft).Column
 lCol = Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
 fRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlDown).Row
 lRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
 Set GetUsedRange = ws.Range(Cells(fRow, fCol), Cells(lRow, lCol))
End Function
Sub Test()
 
 Call DatatoTable(ActiveSheet)
 
End Sub