我在A到Z列中有一组数据,如果F列中的任何单元格都以粗体显示,则应调用以将整个行都以粗体显示。
例如,F3和F80为粗体。 A3:Z3和A80:Z80应加粗。我的代码只能在F列中的单元格加粗之前起作用,无法继续对整行进行加粗。
Sub Bold()
Dim CheckRange As Range
Dim cell As Range
With ActiveSheet
Set CheckRange = .Range("F2:F" & .Cells(.Rows.Count, "F").End(xlUp).Row)
End With
With CheckRange
.FormatConditions.Delete
.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreaterEqual, formula1:="1000000"
With .FormatConditions(1)
.Font.Bold = True
.StopIfTrue = False
End With
For Each cell In CheckRange
If cell(cell.Row, 6).Font.Bold = True Then
cell.EntireRow.Font.Bold = True
End If
Next
End Sub
非常感谢您的帮助。
答案 0 :(得分:1)
可能使用公式表示条件格式更好
Sub Bold()
With ActiveSheet.UsedRange
.FormatConditions.Delete
.Range("A1:Z" & .Cells(Rows.Count, 6).End(xlUp).Row).FormatConditions.Add Type:=xlExpression, Formula1:="=$F1>=1000000"
With .FormatConditions(1)
.Font.Bold = True
.StopIfTrue = False
End With
End With
End Sub
或者为了遵守您的代码,您可以在循环中使用偏移量和调整大小
Sub Bold()
Dim checkRange As Range, cell As Range
With ActiveSheet
Set checkRange = .Range("F2:F" & .Cells(.Rows.Count, "F").End(xlUp).Row)
End With
With checkRange
.FormatConditions.Delete
.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreaterEqual, Formula1:="1000000"
With .FormatConditions(1)
.Font.Bold = True
.StopIfTrue = False
End With
End With
For Each cell In checkRange
If cell.DisplayFormat.Font.Bold = True Then
cell.Offset(, -5).Resize(1, 26).Font.Bold = True
End If
Next cell
End Sub
答案 1 :(得分:0)
代码示例在第一个要运行的示例后缺少end with
。
除此之外,问题出在这里:If cell(cell.Row, 6).Font.Bold
cell
已经是您需要的单元格的范围类型引用,因此您无需查找任何内容,实际上这样做会使它指向该单元格功能的其他位置:例如,这是从在监视窗口中,请注意值的差异:
Watch : : cell.Address : "$F$2" : String : Module1.Bold
Watch : : cell(cell.Row, 6).Address : "$K$3" : Variant/String : Module1.Bold
这是完整的代码:
Sub Bold()
Dim CheckRange As Range
Dim cell As Range
With ActiveSheet
Set CheckRange = .Range("F2:F" & .Cells(.Rows.Count, "F").End(xlUp).Row)
End With
With CheckRange
.FormatConditions.Delete
.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreaterEqual, Formula1:="1000000"
With .FormatConditions(1)
.Font.Bold = True
.StopIfTrue = False
End With
End With
For Each cell In CheckRange
If cell.Font.Bold = True Then
cell.EntireRow.Font.Bold = True
End If
Next
End Sub