【问题标题】:vba looking for a fast way to highlight every other rowvba 正在寻找一种快速突出显示其他行的方法
【发布时间】:2016-06-10 23:06:37
【问题描述】:

到目前为止,我有这个,它对于大数据集来说非常慢。任何帮助

'For every row in the current selection...
For Counter = 1 To RNG.Rows.Count 'reccnt
    'If the row is an odd number (within the selection)...
    If Counter Mod 2 = 1 Then
        With RNG.Rows(Counter).Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .ThemeColor = xlThemeColorAccent6
                .TintAndShade = 0.799981688894314
                .PatternTintAndShade = 0
        End With
    End If
Next

【问题讨论】:

  • 为什么不使用以下公式的条件格式:=MOD(ROW(),2)=1
  • 您不必输入公式。可以在代码中添加条件格式。录制宏以查看您必须执行的操作。
  • 是的,您可以一次将格式应用到多个范围,它的运行速度会快得多,但有数量限制你可以联合的范围。您可以在范围上使用 UNION 方法(以建立范围),也可以在范围地址中使用联合运算符“,”,例如 Range("1:1,3:3,5:5")
  • 您还应该在代码运行时禁用 Application.ScreenUpdating,并在完成后重置它。
  • 请参阅此处以获取您可以修改的一些示例。 stackoverflow.com/questions/13661965/…

标签: excel vba performance


【解决方案1】:

试试这个。我想它会加快速度。它几乎立即为我运行。

Sub ColorEven()
    Set rng = Rows("1:40000")
    rng.FormatConditions.Add Type:=xlExpression, Formula1:="=MOD(ROW(),2)=0"
    rng.FormatConditions(1).Interior.Pattern = xlSolid
    rng.FormatConditions(1).Interior.PatternColorIndex = xlAutomatic
    rng.FormatConditions(1).Interior.ThemeColor = xlThemeColorAccent6
    rng.FormatConditions(1).Interior.TintAndShade = 0.799981688894314
    rng.FormatConditions(1).Interior.PatternTintAndShade = 0
End Sub

【讨论】:

    【解决方案2】:

    另一种非常快速(立即 50k 行)的方法,无需条件格式:

    Option Explicit
    
    Sub main()
    
        Dim i As Long, nRows As Long
        Dim hlpCol As Range
        Dim indexArray1() As Long, indexArray2() As Long
    
        With Range("A1:A50000")
            nRows = .Rows.Count '<~~ retrieve n° of rows to be processed
            ReDim indexArray1(1 To nRows) '<~~ redim indexArray1 accordingly
            ReDim indexArray2(1 To nRows) '<~~ redim indexArray2 accordingly
    
            ' fill indexArrays
            For i = 1 To nRows
                indexArray1(i) = i 'indexArray1, which stores the initial range order
                indexArray2(i) = IIf(.Cells(i, 1).Row Mod 2 = 1, i, nRows + i) 'indexArray2, "marks" range "even" rows to be "after" "uneven" ones
            Next i
    
            Set hlpCol = .Offset(, .Parent.UsedRange.Columns.Count) '<~~ set a "helper" column ...
            hlpCol.Value = Application.Transpose(indexArray1) '<~~ ... fill it with indexArray1...
            hlpCol.Offset(, 1).Value = Application.Transpose(indexArray2) '<~~ ... and the adjacent one with indexArray2
    
            .Resize(, hlpCol.Column + 1).Sort key1:=hlpCol.Offset(, 1) '<~~ sort range to group range "uneven" rows before "even" ones
    
            ' format only half of the range as wanted
            With .Resize(.Rows.Count / 2).Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .ThemeColor = xlThemeColorAccent6
                .TintAndShade = 0.799981688894314
                .PatternTintAndShade = 0
            End With
    
            .Resize(, hlpCol.Column + 1).Sort key1:=hlpCol '<~~ sort back the range to its initial order
    
        End With
        hlpCol.Resize(, 2).Clear '<~~ clear helper columns
    
    End Sub
    

    【讨论】:

      【解决方案3】:

      使用桌子!!它会自动进行彩色条带化。

      【讨论】:

      • 最有可能是 OP 使用 VBA for Excel,而 Excel 没有色带..
      • 不,先生,Excel 表格没有有默认的条带选项。您可以自己尝试并确认。我这么说是因为 Excel 中没有表格! Excel 完全是关于单元格您一定会与其他具有表格的 Office 程序(如 Word 和 PowerPoint)混淆。如果您看到此链接,您可以看到创建带状行实际上使用了此问题的已接受答案中使用的方法,只是此链接由用户界面执行,而已接受答案使用 VBA。 microknowledge.com/creating-banded-rows-in-excel
      • @Sree 我想你一定在使用 20 年前的 Excel 版本。 Excel 支持在单击按钮时带有行带状的数据表。你应该在写回复之前做你的研究
      • 哦,好吧,我没有做太多研究......无论如何,我使用的是 Office 365 中的 Excel。我从来不知道“插入 > 表格”选项真的存在,我通常创建手动表格并使用“过滤器”选项。直到有新东西!对于“对抗”或任何事情,我很抱歉,请先生原谅我!
      猜你喜欢
      • 2011-02-14
      • 1970-01-01
      • 2014-01-09
      • 2016-06-09
      • 1970-01-01
      • 2020-08-28
      • 1970-01-01
      • 2017-10-03
      • 2011-03-27
      相关资源
      最近更新 更多