基于下一个单元格值的 Excel VBA 条件格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10802281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel VBA conditional formatting based on next cell value
提问by AdRock
I am trying to do some conditional formatting in Excel for the font size but seeing as it can't be done from the format menu, it needs to be done with VBA.
我正在尝试在 Excel 中对字体大小进行一些条件格式设置,但由于无法从格式菜单中完成,因此需要使用 VBA 完成。
I have a range B6 to however many rows and I want to look at the cell next to it and see if it's blank (column C). If it is then format the cell to Bold and 11pt. If it's not blank then it needs to be normal and 9pt.
我有一个范围 B6 到多行,我想查看它旁边的单元格,看看它是否为空白(C 列)。如果是,则将单元格格式化为粗体和 11pt。如果它不是空白,那么它需要是正常的和 9pt。
My code at the minute only makes the last row Bold and 11pt and the rest of the column, even if column C is empty will be normal 9pt.
我当时的代码只使最后一行粗体和 11pt 以及列的其余部分,即使列 C 为空也将是正常的 9pt。
What is going wrong? BTW I'm using Excel 2003
出了什么问题?顺便说一句,我使用的是 Excel 2003
Dim c As Range, rng
Dim LASTROW As Long
LASTROW = Cells(Rows.Count, 1).End(xlUp).Row
Set rng = Range("B6:B" & LASTROW)
For Each c In rng
If Len(c.Offset(1, 0)) = 0 Then
c.Font.Bold = True
c.Font.Size = 11
Else
c.Font.Bold = False
c.Font.Size = 9
End If
Next c
回答by Mark M
Your Offset parameters are backwards. You are checking the cell below the current one.
您的 Offset 参数向后。您正在检查当前单元格下方的单元格。
回答by smirkingman
Note the trick is to use a single rule, coded for the top-left cell
请注意,诀窍是使用单个规则,为左上角单元格编码
回答by Widor
This doesn't need a macro - you can do it using a formula in Conditional Formatting.
这不需要宏 - 您可以使用条件格式中的公式来完成。
Say you wanted to highlight the adjacent cell in column B red when the cell in column C had a value of "Red":
假设您想在 C 列中的单元格的值为“红色”时突出显示 B 列中的相邻单元格为红色:
=IF(C6="Red",TRUE,FALSE)
then just fill down with the fill handle as usual.
然后像往常一样用填充手柄填充。
Rule editor (2007):
规则编辑器(2007):