Excel VBA:范围内的替代行颜色 [英] Excel VBA: Alternate Row Colors in Range

查看:245
本文介绍了Excel VBA:范围内的替代行颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我花了很长时间查找一种方法来替代指定范围内的行颜色。真的没有很多在那里,老实说,我发现只是看起来过于复杂。所以,我决定停止像一个无耻的脚本小子的表现,并将以下示例放在一起:

I spent a VERY long time today looking up a method to alternate row colors within a specified range. There really isn't a lot out there and to be honest what I found just looked over-complicated. So, I decided to stop acting like a shameless 'script-kiddy' and put the below sample together:

Sub AlternateRowColors()
Dim lastRow as Long

lastRow = Range("A1").End(xlDown).Row

For Each Cell In Range("A1:A" & lastRow) ''change range accordingly
    If Cell.Row Mod 2 = 1 Then ''highlights row 2,4,6 etc|= 0 highlights 1,3,5
        Cell.Interior.ColorIndex = 15 ''color to preference
    Else
        Cell.Interior.ColorIndex = xlNone ''color to preference or remove
    End If
Next Cell

End Sub

现在我知道这是有用的,但我想知道是否有一个更简单的方法?

Now I know that works, but I was wondering if there's a simpler method?

如果是这样,请告诉我,因为我现在非常渴望学习简化,因为我现在有写入详细代码的倾向。如果没有,那么这个条目可能会找到它的搜索字词是Google的第1页,因为我花了很长时间才能找到甚至是远程的东西。

If so, please do tell because I'm very eager to learn simplification as I have a tendency to write verbose code at present. If not, then may this entry find it's way to page 1 of Google for it's search term(s), because it took me absolutely ages to find anything even remotely useful.

留下脚本小孩的好处。

如果您的数据不包含预先-exisiting颜色:

The following lines of code may be removed if your data contains no pre-exisiting colors:

    Else
        Cell.Interior.ColorIndex = xlNone


推荐答案

可以使用条件格式化来完成交替行颜色:

Alternating row colors can be done using conditional formatting:

这篇关于Excel VBA:范围内的替代行颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆