【问题标题】:Excel VBA - Apply auto filter and Sort by specific colourExcel VBA - 应用自动过滤器并按特定颜色排序
【发布时间】:2015-07-14 08:38:32
【问题描述】:

我有一个自动过滤的数据范围。自动过滤器由以下 VB 代码创建:

Sub Colour_filter()

Range("A4").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Selection.AutoFilter

End Sub

我想按以下颜色( Color = RGB(255, 102, 204) )对列“A”(数据实际上从单元格“A4”开始)中的值进行排序,以便所有具有该颜色的单元格排序到顶部。

如果可以将额外的代码添加到我现有的代码中,那就太好了?

我的办公室真的很吵,我的 VB 也不是最好的。大笑、聊天的女士们会加倍困难。任何帮助都将是缓解压力的天堂!! (附言:不要嘲笑女士们,只是我的办公室 95% 是女性)。


@ScottHoltzman 根据请求编辑。

我请求的代码构成了一个较大代码的一部分,这会使事情变得混乱,尽管这是我目前需要的方面的精简版本。

Sub Colour_filter()
' Following code( using conditional formatting) adds highlight to 'excluded' courses based
'on 'course code' cell value matching criteria. Courses codes matching criteria are highlighted
'in 'Pink'; as of 19-Nov-2012 the 'excluded' course codes are
'(BIGTEST, BIGFATCAT).

' <====== CONDITIONAL FORMATTING CODE STARTS HERE  =======>
    Columns("A:A").Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
Formula1:="=""BIGTEST"""
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
    .Color = 13395711
   End With

Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
Formula1:="=""BIGFATCAT"""
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
    .Color = 13395711

End With

' <====== CONDITIONAL FORMATTING CODE ENDS HERE  =======>

' Following code returns column A:A to Font "Tahoma", Size "8"
  Columns("A:A").Select
    With Selection.Font
        .Name = "Tahoma"
        .FontStyle = "Regular"
        .Size = 8
        .ThemeColor = xlThemeColorLight1
        .ThemeFont = xlThemeFontNone

     End With
    With Selection
        .HorizontalAlignment = xlLeft
        .VerticalAlignment = xlTop
        .WrapText = False
    End With

' Following code adds border around all contiguous cells ion range, similar to using keyboard short cut "Ctrl + A".
Range("A4").Select
ActiveCell.CurrentRegion.Select


With Selection
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    End With
With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
    End With



' Following code adds 'Blue' cell colour to all headers in Row 4 start in Cell "A4".

 Range("A4").Select
    Range(Selection, Selection.End(xlToRight)).Select
    With Selection
        .HorizontalAlignment = xlLeft
        .VerticalAlignment = xlTop
        .WrapText = True
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorLight2
        .TintAndShade = 0.399975585192419
        .PatternTintAndShade = 0
    End With
    With Selection.Font
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0
    End With
    Selection.Font.Bold = True

'<== adds auto-filter to my range of cells ===>

Range("A4").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Selection.AutoFilter

End Sub

【问题讨论】:

  • 如果您show us the code you've tried to get the sorting accomplished,您将更快地获得更多帮助。
  • @moshjosh 所以不管值如何,您只想按cell colour 对范围进行排序?
  • @moshjosh 你能回答请求吗?并请在问题中附上A 列的示例。
  • 是的,没错。 @bonCodigo
  • @moshjosh 你见过this吗?

标签: sorting excel autofilter vba


【解决方案1】:

这里有一个小的Sub,它按照显示的图像进行以下排序。大多数值(如尺寸/范围大小)都是非常静态的,因为这是一个示例。您可以将其改进为动态的。 请评论此代码是否朝着正确的方向发展,以便我可以更新最终排序。

使用双排序键编辑的代码

代码: 显式选项

Sub sortByColor() 调暗为范围
将 i 调暗为整数 将 inputArray 调暗为 Variant, colourSortID 作为 Variant Dim colourIndex As Long

Set rng = Sheets(1).Range("D2:D13")
colourIndex = Sheets(1).Range("G2").Interior.colorIndex

 ReDim inputArray(1 To 12)
 ReDim colourSortID(1 To 12)

For i = 1 To 12
    inputArray(i) = rng.Cells(i, 1).Interior.colorIndex
    If inputArray(i) = colourIndex Then
        colourSortID(i) = 1
    Else
        colourSortID(i) = 0
    End If
Next i

'--output the array with colourIndexvalues and sorting key values
 Sheets(1).Range("E2").Resize(UBound(inputArray) + 1) = _ 
                   Application.Transpose(inputArray)
 Sheets(1).Range("F2").Resize(UBound(colourSortID) + 1) = _ 
                   Application.Transpose(colourSortID)

 '-sort the rows based on the interior colour
 Application.DisplayAlerts = False
 Set rng = rng.Resize(, 3)

    rng.Sort Key1:=Range("F2"), Order1:=xlDescending, _
    Key2:=Range("E2"), Order1:=xlAscending, Header:=xlNo, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal

 Application.DisplayAlerts = True

 End Sub

输出:

【讨论】:

  • 谢谢你,虽然这不是我需要的。我想要颜色 = RGB(255, 102, 204); 的单元格即粉红色的坟墓排序,所以所有的都跳到顶部。
  • 给出的是一个示例,因此请设置您自己的颜色以使用RGB 或仅对颜色进行索引进行排序。
  • @moshjosh 我的意思是你可以简单地用你需要的RGBH2 单元格着色。然后运行我给你的代码。此更新后的代码在两个键上执行double sort:每个单元格的colourIndex 和使用您选择的coloutIndex 分配的另一个自定义index。如果您愿意,您可以稍后隐藏该列。我不是故意要苛刻 ;) 所以你可以根据你的限制改进代码。自动过滤将隐藏与颜色不匹配的行。在这种情况下,一切仍然显示良好。
猜你喜欢
  • 2022-01-26
  • 1970-01-01
  • 2021-09-03
  • 2017-04-23
  • 1970-01-01
  • 2014-02-22
  • 1970-01-01
  • 2013-07-30
  • 2019-03-03
相关资源
最近更新 更多