【问题标题】:Sorting rows in a range with specific background colour in Excel using vba使用vba在Excel中对具有特定背景颜色的范围内的行进行排序
【发布时间】:2020-12-18 15:56:34
【问题描述】:

我正在尝试对 Excel 工作表中的一系列行进行排序,这些行都以第一列中的特定绿色背景颜色开头,但我的 vba 代码根本不这样做,我不明白为什么。目标是作为一个例子从中得到:

对此:

Private Sub Sort_Click()
    
    Dim StartRow, EndRow, i As Integer
    Dim row As Range, cell As Range
    
    'Discover the data starting and end rows
    i = 1
    StartRow = 1
    EndRow = 1

    'Check the first cell of each row for the start of background colour
    For Each row In ActiveSheet.UsedRange.Rows
        Set cell = Cells(row.row, 1)
        If i < 3 Then
            If Hex(cell.Interior.Color) = "47AD70" And i = 1 Then
                StartRow = row.row
                i = 2
            ElseIf Hex(cell.Interior.Color) <> "47AD70" And i = 2 Then
                EndRow = row.row - 1
                i = 3
            End If
        End If
    Next row
    
    'Sort the range
    Range("A" & StartRow & ":" & "A" & EndRow).Sort Key1:=Range("A" & StartRow & ":" & "A" & EndRow), Order1:=xlAscending, Header:=xlNo

End Sub

代码应检查“A”列中每一行的第一个单元格,直到它到达第一个绿色背景颜色,在该颜色处将该行号分配给变量 StartRow。循环继续,直到它不再检测到第一个单元格中的绿色背景颜色。然后它将该行号 - 1 分配给变量 EndRow。最后,它使用 StartRow 和 EndRow 作为范围对绿色范围进行数字排序。 Range 语句部分可能无法正常工作。我想知道是否有人可以一起提供解决方案或更好的代码。这些图像展示了手动排序的绿色范围内的行。提前致谢

【问题讨论】:

    标签: excel vba loops sorting


    【解决方案1】:

    你需要使用Find方法SearchFormat的最后一个参数。将其设置为您需要的任何格式:

    Sub FGG()
        Dim rng As Range, rngStart As Range, rngEnd As Range
        '// Clear previous format, if any
        Application.FindFormat.Clear
        '// Set search format
        Application.FindFormat.Interior.Color = Hex("47AD70")
        '// Find first cell with format
        Set rngStart = Range("A:A").Find(What:="*", SearchFormat:=True)
        '// Find last cell with format by using xlPrevious
        Set rngEnd = Range("A:A").Find(What:="*", SearchDirection:=xlPrevious, SearchFormat:=True)
        '// Define final range
        Set rng = Range(rngStart, rngEnd)
        '// Sort range and say that that the range has no header
        rng.Sort Key1:=rng(1), Header:=xlNo
    End Sub
    

    【讨论】:

    • JohnyL 的回答可以很好地对第一列中的数据进行排序,但这让我觉得也许我没有很好地描述目标。因此,在这种情况下,我更改了演示图像以更准确地反映目标。并且还补充说我需要代码来移动属于被排序的单元格的整行。因此,这不仅仅是对第一列中的数据进行排序,而是使用“A”列的数据(在绿色范围内)对整行进行重新排序。
    • @Bob_F 只需将rng 扩展为Set rng = rng.Resize(, 4)
    【解决方案2】:

    好吧,我在这里的这个问题上可能有点傻,但是经过更多阅读后发现,要对完整的行进行排序而不是仅对 A 列进行排序,我所要做的就是实际指定整行而不是单列,在代码的排序部分! 这就是 dpne 通过替换行:

    Range("A" & StartRow & ":" & "A" & EndRow).Sort Key1:=Range("A" & StartRow & ":" & "A" & EndRow), Order1:=xlAscending, Header:=xlNo    
    

    与:

    Range("A" & StartRow & ":" & "D" & EndRow).Sort Key1:=Range("A" & StartRow & ":" & "A" & EndRow), Order1:=xlAscending, Header:=xlNo    
    

    上面发生的只是范围部分中的“A”已更改为“D”以覆盖所有用于对行进行排序的列。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-03
      • 1970-01-01
      • 1970-01-01
      • 2021-04-27
      • 2015-04-23
      • 1970-01-01
      • 2016-11-13
      • 2015-12-27
      相关资源
      最近更新 更多