Excel vba正在寻找一种快速方法来每隔一行高亮显示一次
到目前为止,我有这个,它是非常缓慢的大数据集。有什么帮助吗Excel vba正在寻找一种快速方法来每隔一行高亮显示一次,excel,vba,performance,Excel,Vba,Performance,到目前为止,我有这个,它是非常缓慢的大数据集。有什么帮助吗 '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
'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
试试这个。我想这会使事情加快一点。它几乎立刻就为我运行
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
试试这个。我想这会使事情加快一点。它几乎立刻就为我运行
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
使用一张桌子!!它会自动加上色带。使用桌子!!它会自动进行色带处理。一种无条件格式的替代快速(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
选项显式
副标题()
暗我一样长,暗我一样长
调暗hlpCol As范围
将indexArray1()变长,将indexArray2()变长
范围为(“A1:A50000”)
nRows=.Rows.Count'一种不带条件格式的快速(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
选项显式
副标题()
暗我一样长,暗我一样长
调暗hlpCol As范围
将indexArray1()变长,将indexArray2()变长
范围为(“A1:A50000”)
nRows=.Rows.Count'为什么不使用以下公式的条件格式:=MOD(ROW(),2)=1
?您不必键入公式。可以在代码中添加条件格式。录制一个宏以查看您必须执行的操作。是的,您可以一次将格式应用于多个范围,并且运行速度会快得多,但您可以合并的范围数量有限。您可以在某个范围上使用UNION方法(以建立该范围),也可以在范围地址中使用UNION运算符“”,如范围(“1:1,3:3,5:5”),您还应该在代码运行时禁用Application.ScreenUpdate,并在完成后重置它。有关可以修改的示例,请参见此处。为什么不在下列公式中使用条件格式:=MOD(ROW(),2)=1
?您不必键入公式。可以在代码中添加条件格式。录制一个宏以查看您必须执行的操作。是的,您可以一次将格式应用于多个范围,并且运行速度会快得多,但您可以合并的范围数量有限。您可以在某个范围上使用UNION方法(以建立该范围),也可以在范围地址中使用UNION运算符“”,如范围(“1:1,3:3,5:5”),您还应该在代码运行时禁用Application.ScreenUpdate,并在完成后重置它。有关可以修改的示例,请参见此处。OP很可能正在使用VBA for Excel,并且Excel没有色带。不,先生,Excel表格没有默认的色带选项。您可以自己尝试并确认。我这么说是因为Excel中没有表格!Excel是关于单元格的。你一定会被其他Office程序(如Word和PowerPoint)弄糊涂了,它们确实有表格。如果您看到此链接,您可以看到创建带状行实际上使用了此问题的可接受答案中使用的方法,只是此链接通过用户界面执行此操作,而可接受的答案使用VBA@我想你一定使用了20年前的Excel版本,。Excel支持在单击按钮时使用行标注的数据表,。你应该在写回信之前做些调查哦,我没有做太多的调查。。。无论如何,我使用的是Office 365中的Excel。我从来不知道“Insert>Table”选项真的存在,我通常手动创建表并使用“Filter”选项。直到有新的东西!我为“对峙”或其他什么感到抱歉,请原谅我,先生!OP很可能正在使用VBA for Excel,并且Excel没有色带。不,先生,Excel表格没有默认的色带选项。您可以自己尝试并确认。我这么说是因为Excel中没有表格!Excel是关于单元格的。你一定会被其他Office程序(如Word和PowerPoint)弄糊涂了,它们确实有表格。如果您看到此链接,您可以看到创建带状行实际上使用了此问题的可接受答案中使用的方法,只是此链接通过用户界面执行此操作,而可接受的答案使用VBA@我想你一定使用了20年前的Excel版本,。Excel支持在单击按钮时使用行标注的数据表,。你应该在写回信之前做些调查哦,我没有做太多的调查。。。无论如何,我使用的是Office 365中的Excel。我从来不知道“Insert>Table”选项真的存在,我通常手动创建表并使用“Filter”选项。直到有新的东西!我为“对峙”或其他什么感到抱歉,请原谅我,先生!