如果值超过500,则将单元格的格式更改为黑体

我正在使用Excel 2010,并尝试添加一堆行列A和列B的总和在列C中。如果总和超过500,那么我会粗体C列中的数字。我的代码下面的工作在math上工作,但不会做粗体格式。 有人能告诉我我做错了什么吗? 谢谢。

Public Sub addMyRows() Dim row As Integer 'creates a variable called 'row' row = 2 'sets row to 2 b/c first row is a title Do Cells(row, 3).Formula = "=A" & row & "+B" & row 'the 3 stands for column C. If ActiveCell.Value > 500 Then Selection.Font.Bold = True row = row + 1 'loops until it encounters an empty row Loop Until Len(Cells(row, 1)) = 0 End Sub 

纯VBA方法:

 Public Sub AddMyRows() Dim LRow As Long Dim Rng As Range, Cell As Range LRow = Range("A" & Rows.Count).End(xlUp).Row Set Rng = Range("C2:C" & LRow) Rng.Formula = "=A2+B2" For Each Cell In Rng Cell.Font.Bold = (Cell.Value > 500) Next Cell End Sub 

截图:

在这里输入图像说明

另一种方法是条件格式。

希望这可以帮助。

注意:块中的公式已被编辑,以反映@ simoco关于重新运行代码的评论。 这使得代码更安全的时候,你需要重新运行它。 🙂